2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.