gpt4 book ai didi

elasticsearch - 如何通过查询更新以从 elasticsearch 6.4 中的排序查询创建递增排名

转载 作者:行者123 更新时间:2023-11-29 02:57:27 25 4
gpt4 key购买 nike

我正在尝试 _update_by_query 以便在排序搜索中对项目进行排名。是否可以在脚本中获取文档的当前“索引”?

例如。

创建索引:

PUT test
{
"mappings" : {
"person" : {
"properties" : {
"score" : { "type" : "integer" },
"rank" : { "type" : "integer" }
}
}
}
}

添加文档:

POST /test/person/1
{
"score": 100,
"rank": 0
}
POST /test/person/2
{
"score": 200,
"rank": 0
}

运行_update_by_query:

POST /test/_update_by_query
{
"script": {
"source": "ctx._source.rank=3", // how to get document "index" in sort here?
"lang": "painless"
},
"sort": { "score": "desc" }
}

升序排序的结果应该是

score: 200, rank: 1
score: 100, rank: 2

最佳答案

看起来您正在尝试更新单个文档。为此,_update API会是更合适的选择。例如:

POST test/_update/1
{
"script" : {
"source": "ctx._source.rank=params.rank",
"lang": "painless",
"params" : {
"rank" : 4
}
}
}

在上述情况下,您可以看到文档 ID 已提供给仅更新该文档的 API。

相比之下,_update_by_query API 对索引中的每个文档执行更新。因此,如果您需要基于脚本更新多个文档,这就是您的选择。

希望对你有帮助

关于elasticsearch - 如何通过查询更新以从 elasticsearch 6.4 中的排序查询创建递增排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52406061/

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