1

I have the following query, but highlight is not working.

{
  "query": {
    "filtered" : {
      "filter" : {
        "or" : {
          "filters" : [
            {
            "query": { 
              "multi_match":{
                "query":"time",
                "fields":[
                        "display_name_en","display_name_pa","display_name_pr",
                        "icon_class","in_sidemenu","model_name","name",
                        "table_name"
                ],
                "operator":"OR"
              } 
            }
          },
          {
            "terms":{
              "created_by.id":["11","13","14","16"],
              "_name" : "created_by"
            }       
          },
          {
            "range":{
              "created_at":{
                "gte":"2016-01-27",
                "lte":"2016-03-21",
                "format":"YYYY-MM-dd"
              }
            }
          } 
        ],
        "_name" : "or"
      }
    } 
  }
},
"highlight": {
    "fields" : {
        "name" : {}
    }
}
}

And the result is like this:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
 },
 "hits": {
  "total": 1,
  "max_score": 1,
  "hits": [
     {
        "_index": "promote_kmp",
        "_type": "resources",
        "_id": "569e0d84684cc",
        "_score": 1,
        "_source": {
           "id": 106,
           "name": "Last time First Update",
           "display_name_en": "Last time",
           "display_name_pr": "Last time",
           "display_name_pa": "Last time",
           "table_name": "Last time",
           "model_name": "Last time",
           "in_sidemenu": "0",
           "icon_class": "Last time",
           "created_at": "2016-01-18 09:40:51",
           "created_by": null,
           "updated_at": "2016-01-19 14:48:44",
           "updated_by": {
              "id": 6,
              "first_name": "Laili",
              "last_name": "Hamta",
              "last_activity": "2016-01-19 14:48:44",
              "roles": [
                 {
                    "id": 1,
                    "name": "admin",
                    "created_at": "2015-09-06 15:19:15",
                    "updated_at": "2015-09-06 15:19:15",
                    "pivot": {
                       "user_id": 6,
                       "role_id": 1
                    }
                 }
              ]
           }
        },
        "matched_queries": [
           "or"
        ]
     }
  ]
 }
}

As you see there is no any highlight keyword inside result, So what is the mistake with this query, and why highlight is not working? But if I put the multi_match part before filter:{} it is working, and on that case how I can use with or operator? for any help thanks.

1 Answer 1

6

The problem with query is that you are only filtering the results, highlight works on queries only. You can also notice that every document has score of 1 because of applying only filters. You need to rewrite your query as something like this

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "time",
            "fields": [
              "display_name_en",
              "display_name_pa",
              "display_name_pr",
              "icon_class",
              "in_sidemenu",
              "model_name",
              "name",
              "table_name"
            ]
          }
        },
        {
          "terms": {
            "created_by.id": [
              "11",
              "13",
              "14",
              "16"
            ],
            "_name": "created_by"
          }
        },
        {
          "range": {
            "created_at": {
              "gte": "2016-01-27",
              "lte": "2016-03-21",
              "format": "YYYY-MM-dd"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {}
  }
}

convert or filters to bool should clause and highlighting will work now.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank You it is working. But one another question If I want to highlight all the fields that matched how can i do that?
what do you mean by all the fields? what version of ES you are using?
According to example I want to search time term inside the multi_match fields, now suppose time string is found in name,display_name_pr so without specifying those fields in highlight section, how i can say query to highlight only those fields, that time string if founded in? And I use from 1.6
what is happening now? try something like "highlight": { "fields": { "*": {} }, "require_field_match": true } or you can also use highlight query option to specifically specify particular query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.