Delete a field matching a regex

Hi

is it possible to delete a field in all the document of an index if he match to a regex ?

Thanks and Regards

Sounds like a work for Update By Query with a regex query.

Please note that in Elasticsearch it's not possible to delete a field. Operations are atomic on the document level, so deleting a field is basically deleting a document in reindexing the same document without the field.

Thanks for the quick response,

I've read the link you give me, but the thing that I don't see how to do is to perform the regex not on a specific value of a field but on the field name.

by example : if I want to remove all the fields that begin by extra_data_other

thanks

This will reindex all documents from the index src to the index dst while removing all fields that start with b:

PUT src/doc/1
{
  "foo": 1,
  "bar": 2,
  "baz": 3
}

POST _reindex
{
  "source": {
    "index": "src"
  },
  "dest": {
    "index": "dst"
  },
  "script": {
    "source": "ctx._source.keySet().stream().filter(s -> s.startsWith('b')).collect(Collectors.toList()).forEach(k -> ctx._source.remove(k))",
    "lang": "painless"
  }
}
4 Likes

Ho thank you ,

I miss that from the painless doc :star_struck:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.