elasticsearchscoringbooleanquery

Assign custom scores to clauses in boolean query in Elasticsearch


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


Solution

  • You can use a boost parameter along with the should clause

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