简体   繁体   中英

Is it possible to highlight only match_phrase query?

I have a bool query like this

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "type": [
              "bill",
              "press release"
            ]
          }
        },
        {
          "match_phrase": {
            "title": "health"
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "policy_content": {}
    }
  },
  "sort": [
    {
      "publishdate": {
        "order": "desc"
      }
    }
  ]
}

User is searching for keyword "health" and the keyword gets highlighted but 'bill' and 'press release' also gets highlighted which I don't want. How should I address this problem?

You can specify the query to use for highlights via highlight_query .

So you can take advantage of that by updating the highlight aspect of your query to something like this:

"highlight" : {
    "fields" : {
        "policy_content" : {
            "highlight_query" : {
                "match_phrase" : {
                    "policy_content" : "health"
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM