Bool query with boost for should clause

How can you add boost to specific part of a should query. For example:

{
  "query": {
    "bool": {
      "should": [
        {"term":{"fruit.keyword":"apple"}},
        {"term":{"fruit.keyword":"banana"}},
        {"term":{"fruit.keyword":"orange"}},
      ],
      "minimum_should_match": 2
    }
  }
}

If say I want to boost apple by a factor of 2 — meaning apple is twice as important as banana or orange. Is there a way to do it? I tried to use compound bool with boost but it didn’t work (i.e. nesting a bool inside each should).

The docs suggest that I should be able to do it with query string:

{
  "query": {
    "query_string": {
      "query": "fruit.keyword:apple^2 OR fruit.keyword:banana OR fruit.keyword:orange",
      "minimum_should_match": 2
    }
  }
}

But I would ideally like to do it with bool since it makes code more manageable. If it’s possible, how?

I’m using ES 6.8

Hi @seeminglee

You tried using boost like this:

{
          "term": {
            "fruit.keyword": {
              "value": "apple",
              "boost": 2
            }
          }
        }

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.