this is a groovy script. The problem is the ctx._source.academies.remove(index) because ctx is not visible there. The index variable is the right... but i cant use the ctx there. Any suggestions?
{ "script" : "ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) ctx._source.academies.remove(index) }", "params": { "academy_id": 344 } }
Have you tried this?
ctx._source.academies.removeAll { it['academy_id'] == academy_id }
In case you need to check academy_id
matching a list of items, then below can be used: (The closure should satisfy a criteria)
ctx._source.academies.removeAll { it['academy_id'] in [academy_id, some_other_id] }
From an elastic search perspective, I suppose the script can be rewritten as below:
{ "script" : "ctx._source.academies.removeAll { it['academy_id'] in academy_ids }", "params": { "academy_ids": [344, 345, 346] } }
is this the best solution?
def findIndex; ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) findIndex = index }; ctx._source.academies.remove(findIndex)
but this works only for the last found object