ElasticSearch5.4更新\添加嵌套文档

a64a0gku  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(379)
{
        "id":100,
        "name":"xxx",
        "others":{
            "hobbies":["cricket","footbal"]
        }}

如何增加爱好的新价值(“爱好”:[“板球”,“足球”,“读书”])
如何在其他字段中添加新字段(例如:位置)
如何消除“板球爱好。

kt06eoxx

kt06eoxx1#

你可以使用 scripted update (文档链接)以满足您的要求。
查询以在hobbiles中添加值

{
    "script" : {
        "source": "ctx._source.others.hobbies.add(params.hobby)",
        "lang": "painless",
        "params" : {
        "hobby": "reading books"
     }
   }
}

在“其他”中添加新字段

{
    "script": {
        "source": "ctx._source.others.location = 'value_of_new_field'",
        "lang": "painless"
    }
}

把蟋蟀从爱好中剔除

{
    "script" : {
        "source": "int index = ctx._source.others.hobbies.indexOf(params.remove_string); if(index != -1){ctx._source.others.hobbies.remove(index);}",
        "lang": "painless",
        "params" : {
            "remove_string" : "cricket"
        }
    }
}

update-1是一种添加数组字段的方法。
添加数组字段

{
        "script": {
            "source": "ctx._source.extra.location = []",
            "lang": "painless"
        }
    }

相关问题