Surrounding multiple words with tags - ElasticSearch Highlighter in Java
I found what was the problem. I was using plain highlighter instead of fast vector highlighter. Just adding fast vector highlighter solved my problem.
To enable fast vector highlighter, I added specific mapping for type on which I performed search.
Precisely, i added
.field("term_vector", "with_positions_offsets")
on the field on which I wanted to perform highlighting.
// Create mappingXContentBuilder xb = XContentFactory.jsonBuilder() .startObject() .startObject("document") // - document .startObject("properties") // - properties // .startObject("content") .field("type", "string") .field("term_vector", "with_positions_offsets") .endObject() // // .startObject("contentType") .field("type", "string") .endObject() // // .startObject("fileName") .field("type", "string") .endObject() // // .startObject("fileSize") .field("type", "long") .endObject() // // .startObject("openable") .field("type", "string") .endObject() // // .startObject("ownerUserId") .field("type", "string") .endObject() // // .startObject("privacy") .field("type", "string") .endObject() // // .startObject("searchable") .field("type", "string") .endObject() // // - end properties .endObject() // - end document .endObject() .endObject();// Prepare mappingPutMappingRequestBuilder pmrb = client.admin().indices() .preparePutMapping("user") .setType("document");pmrb.setSource(xb);// Create type and mappingPutMappingResponse response = pmrb.execute().actionGet();if (!response.isAcknowledged()) { LOG.info("Error while creating mapping for user document.");} else { LOG.info("Mapping created for user document.");}
Some info on that subject: link