ammcom
ammcom

Reputation: 1034

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

Upvotes: 0

Views: 403

Answers (1)

Bhavya
Bhavya

Reputation: 16172

You can use a boost parameter along with the should clause

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

Upvotes: 1

Related Questions