joebuild
joebuild

Reputation: 544

Elasticsearch - Nested Query Boost in function_score?

My question is about the boost function in elasticsearch (I've read their docs, and it's still quite unclear). Will the following "boost_mode" : "sum" apply to the boosts within the matches? Or since it's outside the enclosure perhaps it's just the sum of the final result, which is just the same as the default. I've got many fields and a vector of values - I want the scoring to be additive and not multiplicative. If the following does not work - any suggestions or pointers would be appreciated. Thanks!

"""
 |  "query": {
 |    "function_score": {
 |      "boost_mode": "sum",
 |      "query": {
 |        "bool": {
 |          "should": [
 |            { "match": { "someField":      { "query": "someValue",      "boost": 2 } } },
 |            { "match": { "someOtherField": { "query": "someOtherValue", "boost": 3 } } }
 |        }
 |      }
 |    }
 |  }
"""

Upvotes: 0

Views: 1234

Answers (1)

Val
Val

Reputation: 217544

The way the sum boost mode works is that it computes the score according to the following formula:

queryBoost * (queryScore + Math.min(funcScore, maxBoost))

where:

  • queryBoost is the value of the boost parameter inside your function score, since there is none, it defaults to 1.0f
  • queryScore is the normal score of the query, in your case it's variable and depends on the searched terms and the additional boost you're setting in your match queries
  • funcScore is the result of the multiplication of the score of each of your filter functions, defaults to 1.0f
  • maxBoost is the value of the max_boost parameter inside your function score, since there is none, it defaults to Float.MAX_VALUE

Also worth noting is that since you have no filter functions, there is no funcScore to compute and the overall score is simply the queryScore. So based what precedes, the formula can be simplified to

queryScore

which means in the end that your overall score is directly related to your query score

A good thing is also to pass ?explain=true in your query so you can get more insights into how the score was computed. In your case, since you have no filter functions, the boost_mode is simply not used at all and the query score is returned instead.

If you were to add a functions parameter with one or more score functions, then the result would be different as a funcScore could be computed.

Upvotes: 2

Related Questions