Elasticsearch Groovy Script eachWithIndex

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

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     } } 

回答1:

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]     } } 


回答2:

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!