ElasticSearch: inner_hits and hightlight_query
A good question looks like highlight-query is not supported withing the inner-hits if it in a query context.Not sure why this is so most probably is a bug.
A workaround is to wrap the highlighting nested query in the final highlightquery. However it would require the original nested query to be the filter:Example workaround for the highlighting in the OP is shown below :
put test/docs/1{ "doctext": "I will do a presentation. I talk about lions and show images of zebras. I hope it will be fun.", "sentences": [ { "text": "I will do a presentation." }, { "text": "I talk about lions and show images of zebras." }, { "text": "I hope it will be fun for lions" } ]}post test/_search{ "query": { "bool": { "should": [ { "match": { "doctext": "zebras" } }, { "nested": { "path": "sentences", "query": { "match": { "sentences.text": "zebras" } }, "inner_hits": { "highlight": { "fields": { "sentences.text": {} } } } } } ] } }, "_source": false, "highlight": { "fields": { "*": { "highlight_query": { "bool": { "should": [ { "match": { "doctext": "lions" } }, { "nested": { "path": "sentences", "query": { "filtered": { "query": { "match": { "sentences.text": "lions" } }, "filter": { "query": { "match": { "sentences.text": "zebras" } } } } }, "inner_hits": { "highlight": { "fields": { "sentences.text": {} } } } } } ] } } } } }}
Result:
{ "took": 1, "timed_out": false, "_shards": { "total": 1, "successful": 1, "failed": 0 }, "hits": { "total": 1, "max_score": 0.6360315, "hits": [ { "_index": "test", "_type": "docs", "_id": "1", "_score": 0.6360315, "highlight": { "doctext": [ "I will do a presentation. I talk about <em>lions</em> and show images of zebras. I hope it will be fun." ] }, "inner_hits": { "sentences": { "hits": { "total": 1, "max_score": 0.40240064, "hits": [ { "_index": "test", "_type": "docs", "_id": "1", "_nested": { "field": "sentences", "offset": 1 }, "_score": 0.40240064, "_source": { "text": "I talk about lions and show images of zebras." }, "highlight": { "sentences.text": [ "I talk about <em>lions</em> and show images of zebras." ] } } ] } } } } ] }}