elasticsearch 函数分数为所有文档返回相同的分数
我使用带有采样器聚合的函数评分来匹配上次访问的文档 ES查询elasticsearch 函数分数为所有文档返回相同的分数,elasticsearch,lucene,elasticsearch,Lucene,我使用带有采样器聚合的函数评分来匹配上次访问的文档 ES查询 { "query": { "function_score": { "boost_mode": "replace", // we need to replace document score with the result of the functions, "query": { }, "functions": [ { "field_
{
"query": {
"function_score": {
"boost_mode": "replace", // we need to replace document score with the result of the functions,
"query": {
},
"functions": [
{
"field_value_factor": { // return `lastvisited` value as score
"field": "visited_time"
}
,"weight":1
}
]
}
},
"size": 10000
}
响应
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"hits" : {
"total" : 5,
"max_score" : 1.45973969E12,
"hits" : [ {
"_index" : "idx0",
"_type" : "8001",
"_id" : "null-100-1459739724631",
"_score" : 1.45973969E12,
"_routing" : "100",
"_source" : {
"visited_time" : 1459739724636
}
}, {
"_index" : "idx0",
"_type" : "8001",
"_id" : "null-101-1459708570522",
"_score" : 1.45970862E12,
"_routing" : "101",
"_source" : {
"visited_time" : 1459708570525
}
}, {
"_index" : "idx0",
"_type" : "8001",
"_id" : "null-101-1459708599619",
"_score" : 1.45970862E12,
"_routing" : "101",
"_source" : {
"visited_time" : 1459708599620
}
}, {
"_index" : "idx0",
"_type" : "8001",
"_id" : "null-100-1459708476386",
"_score" : 1.45970849E12,
"_routing" : "100",
"_source" : {
"visited_time" : 1459708476387
}
}, {
"_index" : "idx0",
"_type" : "8001",
"_id" : "null-100-1459708421417",
"_score" : 1.45970836E12,
"_routing" : "100",
"_source" : {
"visited_time" : 1459708421492
}
} ]
}
}
我不知道为什么它返回相同的文档分数?您的查询很好,但我猜您处理的数字的精度非常高。文档的分数是一个
双精度值,而不是长值
。因此,当将long
值转换为double
时,精度会有所下降,因此您会看到一些结果出现问题。请注意,只有第二个和第三个结果是无序的。我想没有简单的方法来解决这个问题,除非你处理的是低精度的值
但是对于您试图解决的特定问题,有一个简单的解决方案。您可以使用它,而不必处理上述问题。使用以下查询:
{
"query": {
// query goes here
},
"sort": [
{
"visited_time": {
"order": "desc"
}
}
],
"size": 10000
}
我使用带有采样器聚合的函数score query来匹配用户的最后一个文档。我也尝试过分类,但也没有帮助。我解释了为什么在这个链接中尝试这个查询