remove objects from array elastic search remove objects from array elastic search elasticsearch elasticsearch

remove objects from array elastic search


You get that because you are trying to modify a list while iterating through it, meaning you want to change a list of object and, at the same time, listing those objects.

You instead need to do this:

POST /twitter/twit/1/_update{  "script": "item_to_remove = nil; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { item_to_remove=item; } } if (item_to_remove != nil) ctx._source.list.remove(item_to_remove);",  "params": {"tweet_id": "123"}}

If you have more than one item that matches the criteria, use a list instead:

POST /twitter/twit/1/_update{  "script": "items_to_remove = []; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { items_to_remove.add(item); } } foreach (item : items_to_remove) {ctx._source.list.remove(item);}",  "params": {"tweet_id": "123"}}


For people that need this working in elasticsearch 2.0 and up, the nil and foreach don't get recognized by groovy.

So here's a updated version, including a option to replace a item with the same id by a new object.

and also passing it the upsert will make sure the item gets added even if the document doesn't exist yet

{  "script": "item_to_remove = null; ctx._source.delivery.each { elem -> if (elem.id == item_to_add.id) { item_to_remove=elem; } }; if (item_to_remove != null) ctx._source.delivery.remove(item_to_remove); if (item_to_add.size() > 1) ctx._source.delivery += item_to_add;",  "params": {"item_to_add": {"id": "5", "title": "New item"}},  "upsert": [{"id": "5", "title": "New item"}]}