Scheme:

{ "mappings": { "test_type": { "properties": { "sentences": { "type": "text", "position_increment_gap": 99, } } } } } 

Data:

 { "sentences": ["This is a brown fox", "This is white dog"] } 

Request:

 { "query": { "bool": { "must": { "span_near": { "clauses": [ { "span_term": { "sentences": "fox" } }, { "span_term": { "sentences": "dog" } } ], "slop": 199, "in_order": False } }, } }, "highlight" : { "fields" : { "sentences": { } } } } 

Result:

 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.009126444, "hits" : [ { "_index" : "test_index", "_type" : "test_type", "_id" : "1", "_score" : 0.009126444, "_source" : { "sentences" : [ "This is a brown fox", "This is white dog" ] } } ] } } 

The backlight is ignored in this case. How to make the backlight work when I search for words in different sentences? If I search for words in one sentence, then the backlight works.

    1 answer 1

    Elasticsearch is currently not able to highlight the found text in array elements. It was discussed here .

    If you definitely need a backlight, then it is better to use nested objects.

     { "mappings": { "test_type": { "properties": { "sentences": { "type": "nested", "properties": { "value": {"type": "string"} } } } } } } 
    • one
      I found the option how to highlight the text in the array elements. It is necessary to specify in the scheme "index_options": "offsets". And then everything works. - Dmitry Kharitonov
    • Did not know about this option. Thanks, I'll keep it in mind. - Andrey Morozov