Search code examples
elasticsearchelasticsearch-painless

Elasticsearch - Remove field from a structure


I am using Elasticsearch 6.5.
Let say, that my Elasticsearch document looks like this:

"_source" : {
    "field1" : "val1",
    "field2" : "val2",
    "struct1" : {
      "inner_field1" : "inner val1",
      "inner_field2" : "inner val2",
    }
  }

I would like to delete one of inner fields in this structure.
I tried following code:

POST test_idx1/_doc/1/_update
{
 "script": "ctx._source.remove('struct1.inner_field1');"
}

and the result says updated, but nothing changes.
How to perform such action?


Solution

  • You can remove the field from all the existing document by this way,

    POST test_idx1/_update_by_query?conflicts=proceed
    {
        "script" : "ctx._source.struct1.remove('inner_field1')",
        "query" : {
            "exists": { "field": "struct1.inner_field1" }
        }
    }