Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ElasticSearch批量JavaScript API中的脚本更新索引_Javascript_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Javascript,elasticsearch" /> elasticsearch,Javascript,elasticsearch" />

使用ElasticSearch批量JavaScript API中的脚本更新索引

使用ElasticSearch批量JavaScript API中的脚本更新索引,javascript,elasticsearch,Javascript,elasticsearch,我使用ElasticSearchUpdate操作创建或更新当前索引中的现有文档,在api中执行类似操作时 { index: myIndex, type: '_doc', body: [ { index: {_id: docItemID1 } }, docItem1,

我使用ElasticSearchUpdate操作创建或更新当前索引中的现有文档,在api中执行类似操作时

{
                    index: myIndex,
                    type: '_doc',
                    body: [
                    { index:  {_id: docItemID1 } },
                    docItem1,
                    { index:  {_id: docItemID2 } },
                    docItem2
]
                }
这样行。现在,我想使用
scripted\u 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
来充分利用这两个世界,所以我想象这样的事情——如果它是正确的(这是我的问题)

其中
docItem
将包含要更新的
标记。此标记项是一个逗号分隔的标记列表,如
红色、绿色

这种方法正确吗?如果是这样的话,
批量
api的正确主体是哪个,即当使用
主体
作为操作的
[]
时,一个或多个项目的更新
脚本
带有plus
scripted\u upsert
标志?

正确,则:

无论文档是否存在,都希望脚本运行 - [...] - 然后将
scripted\u upsert
设置为true

因此,同时具有“upsert”和“script”部分是一种方法,但您必须保留
“scripted\u upsert”:true(上一个代码片段中缺少)

使用elasticsearch js库,如下所示:

await client.bulk({
  index: 'myIndex',
  type: 'myType',
  body [
    { update: { _id: docId } },
    { script: { source, params }, scripted_upsert: true, upsert: docItem },
  ],
});
await client.bulk({
  index: 'myIndex',
  type: 'myType',
  body [
    { update: { _id: docId } },
    { script: { source, params }, scripted_upsert: true, upsert: docItem },
  ],
});