Search code examples
elasticsearchelasticsearch-painless

ElasticSearch _update_by_query filter elements of array


Problem: I need to remove numbers > 25000000 from array in elasticsearch using _update_by_query.

curl -XPOST 'http://127.0.0.1:9200/mydb/_update_by_query' --header 'Content-Type: application/json'  --data '
{"query": {
 "range": {
 "multiAccounts": { "gte": 25000000 } } },
 "script": {"source": "ctx._source.multiAccounts = ctx._source.multiAccounts;"}
}'

Insted of ctx._source.multiAccounts = ctx._source.multiAccounts; I need somethig like:

ctx._source.multiAccounts = ctx._source.multiAccounts.filter(el < 25000000);

Solution

  • Since Painless relies on Java (and if multiAccounts is an array in your source document), then you can use streams, like this:

    ctx._source.multiAccounts = ctx._source.multiAccounts.stream().filter(el -> el > 25000000).collect(Collectors.toList());