Elasticsearch: remove/update field inside nested object

你。 提交于 2020-01-20 06:49:18

问题


{
  "_index" : "test",
  "_type" : "test",
  "_id" : "1212",
  "_version" : 5,
  "found" : true,
  "_source" : {
    "count" : 42,
    "list_data" : [ {
      "list_id" : 11,
      "timestamp" : 1469125397
    }, {
      "list_id" : 122,
      "timestamp" : 1469050965
    } ]
  }
}

This is my document schema.list_data is nested object. I have requirement to update/delete particular filed inside list_data. I am able to update count field using groovy script.

$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d '
{
    "script" : "ctx._source.count = 41"
}'

But don't know how to update nested object.

For example i want to add this into list_data.

{
   "list_id" : 121,
   "timestamp" : 1469050965
}

and my document should change to:

{
  "_index" : "test",
  "_type" : "test",
  "_id" : "1212",
  "_version" : 6,
  "found" : true,
  "_source" : {
    "count" : 41,
    "list_data" : [ {
      "list_id" : 11,
      "timestamp" : 1469125397
    }, {
      "list_id" : 121,
      "timestamp" : 1469050965
    }, {
      "list_id" : 122,
      "timestamp" : 1469050965
    } ]
  }
}

and if i perform delete based on list_id = 122 my record should look like

{
  "_index" : "test",
  "_type" : "test",
  "_id" : "1212",
  "_version" : 7,
  "found" : true,
  "_source" : {
    "count" : 41,
    "list_data" : [ {
      "list_id" : 11,
      "timestamp" : 1469125397
    }, {
      "list_id" : 121,
      "timestamp" : 1469050965
    }]
  }
}

回答1:


To add a new element to your nested field you can proceed like this:

$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d '
{
    "script" : "ctx._source.list_data += newElement",
    "params": {
        "newElement": {
           "list_id" : 121,
           "timestamp" : 1469050965
        }
    }
}'

To remove an existing element from your nested field list, you can proceed like this:

$ curl -XPOST 'localhost:9200/index/type/1212/_update?pretty' -d '
{
    "script" : "ctx._source.list_data.removeAll{it.list_id == remove_id}",
    "params": {
        "remove_id" : 122
    }
}'



回答2:


I don't know why, but I find that

ctx._source.list_data.removeAll{it.list_id == remove_id}

can't work. Instead I use removeIf like this:

ctx._source.list_data.removeIf{list_item -> list_item.list_id == remove_id}

where list_item could be arbitrary string.



来源:https://stackoverflow.com/questions/38741658/elasticsearch-remove-update-field-inside-nested-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!