Delete a field matching a regex


(Gregory Boero-Teyssier) #1

Hi

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

Thanks and Regards


(Igor Motov) #2

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.


(Gregory Boero-Teyssier) #3

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


(Igor Motov) #4

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"
  }
}

(Gregory Boero-Teyssier) #5

Ho thank you ,

I miss that from the painless doc :star_struck:


(system) #6

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