问题描述
我有一个复杂的 Elasticsearch 查询,如下例所示。这个查询有两个子查询:一个加权布尔查询和一个衰减函数。我试图了解 Elasticsearch 如何汇总每个子查询的分数。如果我单独运行第一个子查询(加权 bool 查询),我的最高分是 20。如果我单独运行第二个子查询(衰减函数),我的分数是 1。但是,如果我同时运行两个子查询,我的最高分是 15。有人可以解释一下吗?
我的第二个相关问题是如何对两个子查询的分数进行加权?
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
},}]
}}
解决方法
我自己通过阅读 elasticsearch document on the usage of function_score. function_score
有一个参数 boost_mode
来找到答案,该参数指定了查询分数和函数分数的组合方式。默认情况下,boost_mode
设置为 multiply
。
除了默认的multiply
方法,我们还可以将boost_mode
设置为avg
,并在上面的衰减函数weight
中添加一个参数exp
,然后总分将为:( the_bool_query_score + the_decay_function_score * weight ) / ( 1 + weight )
。