Return Elasticsearch highlight results in position order?

ぃ、小莉子 提交于 2019-12-24 05:08:06

问题


I'm currently using the highlighting feature that elasticsearch offers in my query. However, the one thing I'm not quite clear on is about how the results are ordered. I would prefer they come back in the order that they appear in a paragraph instead of importance/score. This is so I can concatenate them with ...'s in the same order as they are in the original document (similar to Google results). However, they are currently returning in some weighted order based on best match?

Is there a way to accomplish this without having to do additional post processing on the field after seeing the highlight results.

I see there is a "order" : "score" option for a highlight, but there doesn't seem to be any other documented options to change the return order. (And as an aside, I don't understand the difference between the default order and the scoring order).

Here's a snippet of the highlight portion of my query.

  "highlight": {
    "fields": {
      "synopsis": {
        "fragment_size": 150,
        "number_of_fragments": 4
      }
    }
  }

回答1:


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.



来源:https://stackoverflow.com/questions/32489012/return-elasticsearch-highlight-results-in-position-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!