1

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?

Michał Herman
  • 3,437
  • 6
  • 29
  • 43

2 Answers2

1

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" }
    }
}
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You can do this in two steps:

GET test_idx1/_doc/1

Then pass up struct1 with inner_field1 removed as a partial document update:

POST test_idx1/_update/1
{
    "doc" : {
        "struct1" : {
            "inner_field2" : "inner val2",
        }
    }
}
Carasel
  • 2,740
  • 5
  • 32
  • 51
  • Unfortunately this solution can be very slow. What if `struct1` have few dozens of fields and I need to delete only one of them? – Michał Herman Oct 29 '19 at 17:08
  • Are you sure struct1 needs to be an object then? It might better to model the fields as `struct1-innerfield1` etc, without nesting them. This would simplify your queries and updates. – Carasel Oct 30 '19 at 10:07