elasticsearch,Groovy,elasticsearch" /> elasticsearch,Groovy,elasticsearch" />

Groovy Elasticsearch:通过向其数组字段插入元素来更新现有文档

Groovy Elasticsearch:通过向其数组字段插入元素来更新现有文档,groovy,elasticsearch,Groovy,elasticsearch,考虑以下文件 { "title": "My first blog entry", "text": "Starting to get the hang of this...", "tags": [ "testing" ], "views": 0 } 我需要做一个类似于“插入”的操作。如果我遇到这样的数据 { "id": 1, "tags": [ "new tag" ] } 我想用相同的id更新现有文档。因此结果应该是: { "id": 1,

考虑以下文件

{
  "title":  "My first blog entry",
  "text":   "Starting to get the hang of this...",
  "tags": [ "testing" ], 
  "views":  0 
}
我需要做一个类似于“插入”的操作。如果我遇到这样的数据

{
    "id": 1,
    "tags": [ "new tag" ]
}
我想用相同的id更新现有文档。因此结果应该是:

{
    "id": 1,
    "title":  "My first blog entry",
    "text":   "Starting to get the hang of this...",
    "tags": [ "testing", "new tag" ], 
    "views":  0 
}
如果不存在具有相同id的文档,我想创建一个新文档

现在在mongoDB这样的数据库中,我可以使用带有$addToSet或$push操作的更新。我在Elasticsearch中找不到类似的操作


我读到它可以通过在groovy中编写脚本来完成。但是,这需要在包含2亿条记录的文件上完成。我不确定是否可以将groovy与批量API结合使用。有可能吗?

您不需要为此使用批量API。你可以使用。Upsert请求也可以嵌入到批量请求中

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
  "script": "if (ctx._source.tags.contains(\"tags\")) {ctx._source.tags += tag;} else {ctx._source.tags = [tag]}",
  "params": {
    "tag": "newTag"
  },
  "upsert": {
    "title": "My first blog entry",
    "text": "Starting to get the hang of this...",
    "tags": [
      "newTag"
    ],
    "views": 0
  }
}'

查看一下HttpBuilder项目()您能显示包含记录的文件摘录吗?谢谢Vinet。这在批量API中运行良好。然而,我的要求有一点变化。upsert文档的标记数组中应包含与params中的标记相同的标记。此外,应修改if条件以包含if(!ctx.\u source.tags.contains(tag))的条件。请修改您的答案,以便我可以接受并关闭它。如果我想更新文档数组,我已经在相同的场景中进行了更改。我怎样才能通过params。请帮帮我……谢谢you@PavanKumarVarma-你能把它作为一个单独的问题和ping发布吗me@VineethMohan有没有关于如何将upsert与批量api一起使用的示例?