Elasticsearch: remove/update field inside nested object

后端 未结 4 1864
借酒劲吻你
借酒劲吻你 2021-01-12 18:36
{
  \"_index\" : \"test\",
  \"_type\" : \"test\",
  \"_id\" : \"1212\",
  \"_version\" : 5,
  \"found\" : true,
  \"_source\" : {
    \"count\" : 42,
    \"list_dat         


        
相关标签:
4条回答
  • 2021-01-12 18:50

    I was getting error [UpdateRequest] unknown field [params] as I was using the latest version of ElasticSearch (7.9.0.), seems like the syntax is changed a bit.

    Following should work for newer versions of ElasticSearch:

    $ curl -XPOST 'localhost:9200/<index-name>/_update/1212'
    {
      "script": {
        "source": "ctx._source.list_data.removeIf(list_item -> list_item.list_id == params.remove_id);",
        "params": {
          "remove_id": 122
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-12 19:03

    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.

    0 讨论(0)
  • 2021-01-12 19:08

    What worked for me was the instructions in the following link. Perhaps it is the version of ES.

    0 讨论(0)
  • 2021-01-12 19:12

    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
        }
    }'
    
    0 讨论(0)
提交回复
热议问题