RPM1984
RPM1984

Reputation: 73123

Elasticsearch "boost" not working when inside "filter"

I'm trying to boost matches on a certain field over another.

This works fine:

{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "boost": 2,
            "mainField": "foo"
          }
        },
        {
          "terms": {
            "otherField": "foo"
          }
        }
      ]
    }
  }
}

When i see the documents matched on mainField, i see they have a _score of 2.0 as expected.

But when i wrap this same query in a filter:

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "terms": {
                  "boost": 2,
                  "mainField": "foo"
                }
              },
              {
                "terms": {
                  "otherField": "foo"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

The _score for all documents is 0.0.

The same thing happens for multi_match. By itself (e.g inside a query) it works fine, but inside a bool + filter, it doesn't work.

Can someone explain why this is the case? I need to wrap in a filter due to the way my app composes queries.

Some context might also help: I'm trying to return documents that match on either mainField or otherField, but sort the ones matching on mainField first, so i figured boost would be the most appropriate choice here. But let me know if there is a better way.

Upvotes: 0

Views: 496

Answers (1)

Bhavya
Bhavya

Reputation: 16192

The filter queries are always executed in the filter context. It will always return a score of zero and only contribute to the filtering of documents.

Refer to this documentation, to know more about filter context

Due to this, you are not getting a _score of 2.0, even after applying boost, in the second query

Upvotes: 1

Related Questions