我正在使用 ElasticSearch 更新 操作 Upserts在 bulk 中执行操作时,创建或更新当前索引中的现有文档像这样的 API
{
index: myIndex,
type: '_doc',
body: [
{ index: {_id: docItemID1 } },
docItem1,
{ index: {_id: docItemID2 } },
docItem2
]
}
这工作正常。现在我想使用 scripted_upsert
标志和 script
更新操作将新值更新/附加到文档项中的 tag
字段,以便像这样:
{
"scripted_upsert":true,
"script" : {
"source": "if ( !ctx._source.tags.contains(params.tag) ) ctx._source.tag.concat( params.tag)",
"lang": "painless",
"params" : {
"tag" : "blue"
}
},
"upsert" : {
"tag" : ["red","green"]
}
}
现在,我想使用 scripted_upsert
来充分利用这两个世界,所以我想象这样的事情 - 如果它是正确的(这是我的问题)
{
"script" : {
"source": "if ( !ctx._source.tags.contains(params.tag) ) ctx._source.tag.concat( params.tag)",
"lang": "painless",
"params" : {
"tag" : myNewTag
}
},
"upsert" : docItem
}
其中 docItem
将包含要更新的 tag
项。此标签项是逗号分隔的标签列表,例如 red,green
。
这种方法正确吗?如果是这样,那么哪个是 bulk
api 的正确主体,即当使用 body
作为具有更新 script< 的操作的
带有一个或多个项目的 []
时scripted_upsert
标志?
最佳答案
没错,docs say :
If you would like your script to run regardless of whether the document exists or not - [...] - then set
scripted_upsert
to true
因此,同时拥有“upsert”和“script”部分是可行的方法,但您必须保留 "scripted_upsert":true
(最后一个片段中缺少该内容)。
使用elasticsearch-js库,它是这样的:
await client.bulk({
index: 'myIndex',
type: 'myType',
body [
{ update: { _id: docId } },
{ script: { source, params }, scripted_upsert: true, upsert: docItem },
],
});
关于javascript - 使用 ElasticSearch Bulk JavaScript API 中的脚本更新索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53394321/