gpt4 book ai didi

elasticsearch - Elasticsearch使用多个文件的相同脚本批量更新

转载 作者:行者123 更新时间:2023-12-03 02:15:01 25 4
gpt4 key购买 nike

我正在尝试为多个文档运行批量更新语句。但是,我想对它们全部执行相同的script
让我们看下面的例子:

POST _bulk
{ "update": {"_id": "1", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "source": "ctx._source.count2 = params.count2", "lang": "painless", "params": { "count2": 4 }}}
{ "update": {"_id": "2", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "source": "ctx._source.count2 = params.count2", "lang": "painless", "params": { "count2": 4 }}}
{ "update": {"_id": "3", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "source": "ctx._source.count2 = params.count2", "lang": "painless", "params": { "count2": 4 }}}
在我的特定情况下,脚本更加复杂。
有什么办法可以重用同一脚本语句?
我故意不想使用 update_by_query命令,因为它缺少冲突重试机制。
谢谢

最佳答案

您绝对可以将脚本存储在ES中,并在批量更新中引用它:
首先,存储您的脚本:

POST _scripts/my-script
{
"script": {
"lang": "painless",
"source": "ctx._source.count2 = params.count2"
}
}
然后,使用它:
POST _bulk
{ "update": {"_id": "1", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "id": "my-script", "params": { "count2": 4 }}}
{ "update": {"_id": "2", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "id": "my-script", "params": { "count2": 4 }}}
{ "update": {"_id": "3", "_index": "test", "retry_on_conflict" : 3} }
{ "script": { "id": "my-script", "params": { "count2": 4 }}}

关于elasticsearch - Elasticsearch使用多个文件的相同脚本批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63778168/

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