Return Elasticsearch highlight results in position order? Return Elasticsearch highlight results in position order? elasticsearch elasticsearch

Return Elasticsearch highlight results in position order?


So after doing a bit of playing around, I discovered that the fast-vector-highlighter will natively sort the fragments in order of appearance in the original document. To enable this, I needed to add "term_vector" : "with_positions_offsets" to my synopsis field mapping.

{  "properties" : {    "synopsis" : {      "type" : "string",      "term_vector": "with_positions_offsets"    }  }}

and then use my highlight query as so:

{  "query": {    "match": {      "synopsis": "foo"    }  },  "highlight": {    "fields": {      "synopsis": {        "type": "fvh",        "fragment_size": 150,        "number_of_fragments": 4      }    }  }}

NOTE: Using "order" : "score" would cause the ordering to follow the the scoring schema, which does not necessarily follow start position offset order. I believe the exact code for this comparator can be found here, which seems to base it on the fragment's boost and then its startoffset.