Elasticsearch部分更新脚本:清除数组并替换为新值 [英] Elasticsearch partial update script: Clear array and replace with new values

查看:505
本文介绍了Elasticsearch部分更新脚本:清除数组并替换为新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件:

{
  MyProp: ["lorem", "ipsum", "dolor"]
  ... lots of stuff here ...
}

我的文档可能很大(但是这些 MyProp 字段不是),并且从头开始生成它们很昂贵。

My documents can be quite big (but these MyProp fields are not), and expensive to generate from scratch.

有时我需要更新这些批次-因此进行部分更新(以节省索引客户端的处理能力和带宽,从而节省时间)并用新值替换MyProp值将是有益的。

Sometimes I need to update batches of these - it would therefore be beneficial to do a partial update (to save "indexing client" processing power and bandwidth, and thus time) and replace the MyProp values with new values.

原始文档示例:

{
  MyProp: ["lorem", "ipsum", "dolor"]
  ... lots of stuff here ...
}

已更新文档的示例(或外观):

Example of updated document (or rather how it should look):

{
  MyProp: ["dolor", "sit"]
  ... lots of stuff here ...
}

据我所见,这包括脚本。

From what I have seen, this includes scripting.

Ca n有人能用剩下的难题启发我吗?

Can anyone enlighten me with the remaining bits of the puzzle?

我想如有可能,还提供了一些有关如何在批处理语句中进行说明的说明。

I'd like to also have some instructions of how to make these in a batch statement, if possible.

推荐答案

您可以使用通过查询API更新批量更新。从ES 2.3开始,此方法有效,否则您需要安装插件

You can use the update by query API in order to do batch updates. This works since ES 2.3 onwards, otherwise you need to install a plugin.

POST index/_update_by_query
{
  "script": {
    "inline": "ctx._source.myProp += newProp",
    "params": {
      "newProp": "sit"
    }
  },
  "query": {
    "match_all": {}
  }
}

您可以当然,可以使用任何查询查询来选择需要更新 MyProp 的文档。例如,您可以查询选择具有某些特定 MyProp 值的文档。

You can of course use whatever query you want in order to select the documents on which MyProp needs to be updated. For instance, you could have a query to select documents having some specific MyProp values to be replaced.

以上只会将新值添加到现有数组。如果您需要完全替换 MyProp 数组,则还可以将脚本更改为:

The above will only add a new value to the existing array. If you need to completely replace the MyProp array, then you can also change the script to this:

POST index/_update_by_query
{
  "script": {
    "inline": "ctx._source.myProp = newProps",
    "params": {
      "newProps": ["dolor", "sit"]
    }
  },
  "query": {
    "match_all": {}
  }
}

请注意,您还需要启用动态脚本以便

更新

如果您只想更新一个您可以使用部分文档更新API ,就像这样:

If you simply want to update a single document you can use the partial document update API, like this:

POST test/type1/1/_update
{
    "doc" : {
        "MyProp" : ["dolor", "sit"]
    }
}

这将有效替换 MyProp 数组

如果您要使用批量路线,则不需要编写脚本即可实现所需的功能:

If you want to go the bulk route, you don't need scripting to achieve what you want:

POST index/type/_bulk
{ "update" : {"_id" : "1"} }
{ "doc" : {"MyProp" : ["dolor", "sit"] } }
{ "update" : {"_id" : "2"} }
{ "doc" : {"MyProp" : ["dolor", "sit"] } }

这篇关于Elasticsearch部分更新脚本:清除数组并替换为新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆