Michalis Michalis - 1 month ago 13x
Groovy Question

Elasticsearch Groovy Script eachWithIndex

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


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