use*_*419 5 sorting elasticsearch
请在下面查看我的搜索查询,以下是具体问题。
search = {
'query' : {
'function_score': {
'score_mode': 'multiply'
'functions': functions,
'query': {
'match_all':{}
},
'filter': {
'bool': {
'must': filters_include,
'must_not': filters_exclude
}
}
}
}
'sort': [{'_score': {'order': 'desc'}},
{'time': {'order': 'desc'}}]
}
Run Code Online (Sandbox Code Playgroud)
其中functions的样子:
[{'weight': 5.0, 'gauss': {'time': {'scale': '7d'}}},
{'weight': 3.0, 'script_score': {'script': "1+doc['scores.year'].value"}},
{'weight': 2.0, 'script_score': {'script': "1+doc['scores.month'].value"}}]
Run Code Online (Sandbox Code Playgroud)
运行此查询时会发生什么情况?文档是否由function_score评分,然后根据事实与sort数组进行排序?_score现在是什么(请注意查询是match_all),它在排序中有什么作用?如果我将其颠倒并放在time前面_score,我应该期待什么结果?
如果没有 , Amatch_all会给出相同的分数function_score,这意味着每个文档都会得到1。
它将function_score计算所有三个分数(所有三个匹配,因为每个函数没有过滤器),并将它们相乘(因为你有score_mode: multiply)。所以,大概你会得到function1_score * function2_score * function3_score最终分数。所得分数将用于排序。如果某些 _score 相等,则time用于排序。
最适合您的是,如果您从应用程序中取出查询,但它是 Marvel 的 Sense 仪表板中的 JSON 格式,并使用?explain. 它将为您提供每次分数计算的详细解释。
让我给你举个例子:假设我们有一个包含"year":2015,"month":7,"time":"2015-07-06".
运行查询_search?explain给出了非常详细的解释:
"hits": [
{
"_shard": 4,
"_node": "jt4AX7imTECLWH4Bofbk3g",
"_index": "test",
"_type": "test",
"_id": "3",
"_score": 26691.023,
"_source": {
"text": "whatever",
"year": 2015,
"month": 7,
"time": "2015-07-06"
},
"sort": [
26691.023,
1436140800000
],
"_explanation": {
"value": 26691.023,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "ConstantScore(BooleanFilter(+cache(year:[1990 TO *]) -cache(month:[13 TO *]))), product of:",
"details": [
{
"value": 1,
"description": "boost"
},
{
"value": 1,
"description": "queryNorm"
}
]
},
{
"value": 26691.023,
"description": "Math.min of",
"details": [
{
"value": 26691.023,
"description": "function score, score mode [multiply]",
"details": [
{
"value": 0.2758249,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "match filter: *:*"
},
{
"value": 0.2758249,
"description": "product of:",
"details": [
{
"value": 0.055164978,
"description": "Function for field time:",
"details": [
{
"value": 0.055164978,
"description": "exp(-0.5*pow(MIN[Math.max(Math.abs(1.4361408E12(=doc value) - 1.437377331833E12(=origin))) - 0.0(=offset), 0)],2.0)/2.63856688924644672E17)"
}
]
},
{
"value": 5,
"description": "weight"
}
]
}
]
},
{
"value": 6048,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "match filter: *:*"
},
{
"value": 6048,
"description": "product of:",
"details": [
{
"value": 2016,
"description": "script score function, computed with script:\"1+doc['year'].value",
"details": [
{
"value": 1,
"description": "_score: ",
"details": [
{
"value": 1,
"description": "ConstantScore(BooleanFilter(+cache(year:[1990 TO *]) -cache(month:[13 TO *]))), product of:",
"details": [
{
"value": 1,
"description": "boost"
},
{
"value": 1,
"description": "queryNorm"
}
]
}
]
}
]
},
{
"value": 3,
"description": "weight"
}
]
}
]
},
{
"value": 16,
"description": "function score, product of:",
"details": [
{
"value": 1,
"description": "match filter: *:*"
},
{
"value": 16,
"description": "product of:",
"details": [
{
"value": 8,
"description": "script score function, computed with script:\"1+doc['month'].value",
"details": [
{
"value": 1,
"description": "_score: ",
"details": [
{
"value": 1,
"description": "ConstantScore(BooleanFilter(+cache(year:[1990 TO *]) -cache(month:[13 TO *]))), product of:",
"details": [
{
"value": 1,
"description": "boost"
},
{
"value": 1,
"description": "queryNorm"
}
]
}
]
}
]
},
{
"value": 2,
"description": "weight"
}
]
}
]
}
]
},
{
"value": 3.4028235e+38,
"description": "maxBoost"
}
]
},
{
"value": 1,
"description": "queryBoost"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
因此,gauss计算出的分数为 0.055164978。我不知道这与您的问题有多相关,但我们假设计算是正确的:-)。您的gauss函数weight为 5,因此分数变为 5 * 0.055164978 = 0.27582489。
对于该script year函数,我们有 (1 + 2015) * 3 = 6048。
对于该script month函数,我们有 (1 + 7) * 2 = 16。
本文档的总分multiply是 0.27582489 * 6048 * 16 = 26691.023
每个文档还有一个部分显示用于排序的值。在本文档的情况下:
"sort": [
26691.023,
1436140800000
]
Run Code Online (Sandbox Code Playgroud)
第一个数字是_score如图所示计算的,第二个数字是 date 的毫秒表示形式2015-07-06。