4

Can Elasticsearch or Elastic Cloud return response with matched position?

Example request

{ "query": { "match": { "body": "apple" } } }

Ideal response

{
    "_source": {
        "body": "The red apple is a good apple."
    },
    "matched_position": [
        {
            "start": 8,
            "end": 12,
        },
        {
            "start": 24,
            "end": 28,            
        }
    ]
}
k7m
  • 47
  • 2
  • check this dicussion https://discuss.elastic.co/t/fetching-position-of-keyword-in-matched-document/94291 – jaspreet chahal Oct 02 '19 at 05:56
  • I didn't notice there is an API for highlighting... thank you!! https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-highlighting – k7m Oct 10 '19 at 05:39

1 Answers1

3

If you need the offset and not only the highlighted text, you will need to implement custom highlighter (plugin).

An example highlighter plugin: https://github.com/wikimedia/search-highlighter - note that you would have to adjust it to 7.X version.