Michalis Michalis - 1 year ago 132
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

Answer Source

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