Assign custom scores to clauses in boolean query in Elasticsearch

382 Views Asked by At

In Elasticsearch I am testing the following query:

GET sdata/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "f1": "sth" } }
      ],
      "should": [
        { "match": { "f2": "sth" } }
      ]
    }
  }
}

I know that the overall score of retrieved documents depends on the number of matches they achieve. but is it possible to customize the final score so that the documents that match the should clause may be weighted much more higher than documents that match the must alone? can I add a script to determine how each clause contribute to the final score?

Thank you in advance

1

There are 1 best solutions below

0
ESCoder On BEST ANSWER

You can use a boost parameter along with the should clause

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "f1": "sth"
          }
        }
      ],
      "should": [
        {
          "match": {
            "f2": {
              "query": "sth",
              "boost": 10
            }
          }
        }
      ]
    }
  }
}