Tanapat Sainak
Tanapat Sainak

Reputation: 754

Elasticsearch highlight with multiple queries not work as expected

Here I have 2 data put into elasticsearch

PUT /a/b/1
{
   "message": "a b"
}

PUT /a/b/2
{
   "message": "b c"
}

Then, I tried to get data with this query

GET /a/b/_search
{
  "from": 0,
  "highlight": {
    "fields": {
      "message": {}
    }
  },
  "query": {
    "bool": {
      "should": [
        {
          "query_string": {
            "_name": "query_1",
            "default_operator": "AND",
            "fields": [
              "message"
            ],
            "query": "a"
          }
        },
        {
          "query_string": {
            "_name": "query_2",
            "default_operator": "AND",
            "fields": [
              "message"
            ],
            "query": "b AND c"
          }
        }
      ]
    }
  }
}

What I have expected is, elasticsearch should highlight "a" at data id 1 because it matchs with query_1, but should not highlight "b" in data id 1 because we going to select "b" whenever the message include "c".

This is what I expected

"<em>a</em> b"

What I actually got

"<em>a</em> <em>b</em>"

Is there anyway I can achieve what I expected?

Upvotes: 1

Views: 722

Answers (1)

bittusarkar
bittusarkar

Reputation: 6357

AFAIK, this is not possible in Elasticsearch. It will highlight all matching terms irrespective of which sub query the terms belong to. Something that comes close to what you need is highlight_query but that won't work either. The only option I can think of is querying twice - once with "query_1" and then with "query_2" and then combine the results in the client. But doing this will of course affect the scores of the document.

Upvotes: 1

Related Questions