highlighting based on term or bool query match in elasticsearch
1) Only documents with should query are getting highlighted because you are highlighting against only text
field which is basically your should clause. Although you are using matched_fields
, you are considering only text
field.
From the Docs
All matched_fields must have term_vector set to with_positions_offsets but only the field to which the matches are combined is loaded so only that field would benefit from having store set to yes.
Also you are combining two very different fields, 'matched_fields':['metadata.doc','text']
, this is hard to understand, again from the Docs
Technically it is also fine to add fields to matched_fields that don’t share the same underlying string as the field to which the matches are combined. The results might not make much sense and if one of the matches is off the end of the text then the whole query will fail.
2) You can write highlight condition specific to term query with Highlight Query
Try this in your highlight
part of the query
{ "query": { ...your query... }, "highlight": { "fields": { "text": { "type": "fvh", "matched_fields": [ "text", "metadata.doc" ] }, "metadata.doc": { "highlight_query": { "terms": { "metadata.doc": [ "prince", "queen" ] } } }, "metadata.loc": { "highlight_query": { "terms": { "metadata.loc": [ "ten", "twenty" ] } } } } }}
Does this help?