gpt4 book ai didi

sorting - 如何在使用脚本对 Elasticsearch 中的结果进行排序时获取 _score 而不是 null

转载 作者:行者123 更新时间:2023-12-02 22:13:44 24 4
gpt4 key购买 nike

在使用脚本对查询结果进行排序时,为什么 Elasticsearch 给出的是 null 而不是实际分数。

我正在使用这个简单的脚本进行测试。

PUT _scripts/simple_sorting
{
"script" :{
"lang": "painless",
"source": """
return Math.random();
"""
}
}

查询是

GET some_index/_search
{
"explain": true,
"stored_fields": [
"_source"
],
"sort": {
"_script":{
"type" : "number",
"script" : {
"id": "simple_sorting"
},
"order" : "desc"

}
},
"query" : {
"bool": {
"should": [
{
"match": {
"tm_applied_for": {
"query": "bisire"
}
}
}
]
}
}
}

查询给我的结果看起来像这样。

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 20,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_shard" : "[some_index][0]",
"_node" : "UIMgEAZNRzmIpRGyQt232g",
"_index" : "some_index",
"_type" : "_doc",
"_id" : "1171229",
"_score" : null,
"_source" : {
"status" : "Registered",
"proprietor_name
.
.
.
.
"@timestamp" : "2020-03-27T20:05:25.753Z",
"tm_applied_for_anan" : "BISLERI"
},
"sort" : [
0.28768208622932434
],

您可以看到 ma​​x_score_score 值为空。但它在 sort 数组中给出了一个值,elasticsearch 根据该值对文档进行排序。

我希望在我使用脚本排序之前 Elasticsearch 给 Query 的原始分数被返回而不是 null。

此外,当我按如下方式更改脚本 simple_sorting 时。我在 sort 数组(比如 0.234...)中得到一些值,它不等于它之前返回的值(比如 12.1234... ) 当我没有使用脚本进行排序时。

PUT _scripts/simple_sorting
{
"script" :{
"lang": "painless",
"source": """
return _score;
"""
}
}

为什么两次的 _score 值不同?

当 Elasticsearch Documentation清楚地说我可以在使用脚本排序时访问 _score

当我使用 Script 进行排序时,我期望发生的事情是这样的。

1) ma​​x_score_score 保持 Elasticsearch 给出的值,而不是变为空值。

2) 根据 Math.random() 值进行排序。

最佳答案

这是 elasticsearch 的默认行为,因为您使用自己的逻辑对结果进行排序,因此它会忽略分数。为了仍然获得分数,请将 track_scores 参数设置为 true。这将为您提供 elasticsearch 计算的相关性分数。

GET some_index/_search
{
"explain": true,
"stored_fields": [
"_source"
],
"sort": {
"_script": {
"type": "number",
"script": {
"id": "simple_sorting"
},
"order": "desc"
}
},
"query": {
"bool": {
"should": [
{
"match": {
"tm_applied_for": {
"query": "bisire"
}
}
}
]
}
},
"track_scores": true
}

关于sorting - 如何在使用脚本对 Elasticsearch 中的结果进行排序时获取 _score 而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61337961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com