Boosting has no effect in a Boolean-filtered query in Elasticsearch

2.1k views Asked by At

I'm trying to add a boost to documents that match to a term filter. The basis is a Boolean/MatchAll query. But the boosting in my Elasticsearch query has no effect. All result scores are set to 1:

curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "title.keyword": {
                    "value": "Main Page",
                    "boost": 9
                  }
                }
              },
              {
                "term": {
                  "title.keyword": {
                    "value": "Top Page",
                    "boost": 999
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
'

However, when using a filtered query, the boosting works. But due to restrictions in my system I cannot use a filtered query. So is there any method to make the boosting in the original query work?

1

There are 1 answers

0
jonasfh On BEST ANSWER

In the filter part of the query, boosting will have no effect, as the filters only job is to, ehhm, filter queries that match certain values. Try instead:

curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "should": [
        {
          "term": {
            "title.keyword": {
              "value": "Main Page",
              "boost": 9
            }
          }
        },
        {
          "term": {
            "title.keyword": {
              "value": "Top Page",
              "boost": 999
            }
          }
        }
      ]
    }
  }
}
'

...moving the two term-queries directly into the should-clause in your top level bool query.