Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the matched text positions in Elasticsearch?

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,            
        }
    ]
}
like image 936
k7m Avatar asked Sep 02 '25 10:09

k7m


1 Answers

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.

like image 200
Opster Elasticsearch Expert Avatar answered Sep 05 '25 03:09

Opster Elasticsearch Expert