Delete a field matching a regex

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