Elasticsearch更新嵌套对象 [英] Elasticsearch Updating nested objects

查看:1410
本文介绍了Elasticsearch更新嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上发现了同样的问题,但是我不明白解决方案. 问题是,如何在不更新所有数组的情况下更新Nestend文档.非常感谢.

I found out the same problem in internet, but i dont understand a solution. The problem is, how can i update a nestend document without to update all array. Thank you so much.

这是一个带鼻孔的文档

"links": [
  [
    {
      "note_link_id": "1",
      "user_id": "11",
      "creation": "2016-11-15T11:21:10",
      "modification": "2016-11-15T13:38:04",
      "to_asset": "100",
      "from_asset": "99",
      "comment": "Comment 1."
    },
    {
      "note_link_id": "2",
      "user_id": "11",
      "creation": "2016-11-15T13:37:04",
      "modification": "2016-11-15T13:37:27",
      "to_asset": "101",
      "from_asset": "99",
      "comment": "Comment 2."
    },
    {
      "note_link_id": "3",
      "user_id": "11",
      "creation": "2016-11-15T14:01:27",
      "modification": "2016-11-15T14:02:52",
      "to_asset": "102",
      "from_asset": "99",
      "comment": "Comment 3."
    }
  ]
],

问题:如何更新"note_link_id":"2"并仅更新来自 评论2"改为"blja blja blja"

Question: how can i update "note_link_id": "2" and update only comment from "Comment 2" to "blja blja blja"

我再说一遍,如何在不更新所有字段的情况下更新此数组,例如 "note_link_id":"1","note_link_id":"2"和"note_link_id":"3"

I repeat, how can i update this array without to updating all fields, like "note_link_id": "1", "note_link_id": "2" and "note_link_id": "3"

谢谢

推荐答案

您可以尝试使用部分更新和某些脚本语言.脚本看起来或多或少(在这里是时髦的),就像:

You can try to use Partial Updates and some scripting language. The script would look more or less (here in groovy) like:

if (ctx._source.links != null) {
    for (item in ctx._source.links) { 
        if (item.note_link_id == params.link_id_param) {
            item.comment = params.comment_param;
        }
    }
}

然后将脚本与Update API配合使用:

Then use the script with Update API:

POST /index_name/type_name/document_id/_update
{
   "script" : "...",
   "params" : {
      "link_id_param" : "2",
      "comment_param" : "blja blja blja"
   }
}

请记住,Elasticsearch中的文档是不可变的,因此从技术上讲整个文档都将重新索引.

Just remember that documents in Elasticsearch are immutable so technically the whole document will be reindexed.

这篇关于Elasticsearch更新嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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