4

I have two queries.

{'bool':
    {'must':
        { 'terms': 'metadata.loc':['ten','twenty']}
        { 'terms':  'metadata.doc':['prince','queen']}
     }
     {'should':         
         { 'match': 'text':'kingdom of dreams'}
     }
},
{'highlight':
     {'text':
         {'type':fvh,
          'matched_fields':['metadata.doc','text']
         }
      }
 }

There are two questions ?

  1. Why the documents with should query match are getting highlighted whereas documents with only must term match are not getting highlighted.

  2. Is there any way to mention highlight condition specific to term query above ?

This means highlight condition for { 'terms': 'metadata.loc':['ten','twenty']}

and a seperate highlight condition for { 'terms': 'metadata.doc':['prince','queen']}

Val
  • 207,596
  • 13
  • 358
  • 360
Prannoy Mittal
  • 1,525
  • 5
  • 21
  • 32

1 Answers1

1

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?

ChintanShah25
  • 12,366
  • 3
  • 43
  • 44
  • chintan....{'highlight': {'text': {'type':fvh, 'matched_fields':['metadata.doc','text'] } } }... This should highlight must query match of metadata.doc content in text field or not? – Prannoy Mittal Dec 07 '15 at 11:43
  • I think `metadata.doc` values need to be inside `text` field in order to be highlighted, so _prince_ , _queen_ should be part of `text`, also both these fields are `analyzed` differently and as quoted the docs, it might not make sense to group them. – ChintanShah25 Dec 07 '15 at 14:02