I have a complicated Elasticsearch query like the following example. This query has two sub queries: a weighted bool query and a decay function. I am trying to understand how Elasticsearch aggregrates the scores from each sub queries. If I run the first sub query alone (the weighted bool query), my top score is 20. If I run the second sub query alone (the decay function), my score is 1. However, if I run both sub queries together, my top score is 15. Can someone explain this?
My second related question is how to weight the scores from the two sub queries?
query = { "function_score": {
"query": {
"bool": {
"should": [
{'match': {'title': {'query': 'Quantum computing', 'boost': 1}}},
{'match': {'author': {'query': 'Richard Feynman', 'boost': 2}}}
]
},
},
"functions": [
{ "exp": # a built-in exponential decay function
{
"publication_date": {
"origin": "2000-01-01",
"offset": "7d",
"scale": "180d",
"decay": 0.5
},
},
}]
}}
I found the answer myself by reading the elasticsearch document on the usage of function_score. function_score
has a parameter boost_mode
that specifies how query score and function score are combined. By default, boost_mode
is set to multiply
.
Besides the default multiply
method, we could also set boost_mode
to avg
, and add a parameter weight
to the above decay function exp
, then the combined score will be: ( the_bool_query_score + the_decay_function_score * weight ) / ( 1 + weight )
.