Elasticsearch 5.4更新\添加嵌套文档

Elastic search 5.4 Update \ add nested document

{
        "id":100,
        "name":"xxx",
        "others":{
            "hobbies":["cricket","footbal"]
        }}
  1. 如何在爱好中增加新的价值("hobbies":["cricket","footbal","reading books"])
  2. 如何在其他字段中添加新字段(例如:位置)
  3. 如何从爱好中删除“蟋蟀”。

您可以使用 scripted update(documentation link) 来满足您的要求。

  1. 查询 在 hobbiles

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


  2. 在others中添加新字段

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


  3. 从爱好中删除 蟋蟀

    {
        "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"
        }
    }