弹性搜索Groovy脚本eachWithIndex [英] Elasticsearch Groovy Script eachWithIndex

查看:1392
本文介绍了弹性搜索Groovy脚本eachWithIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个groovy脚本。问题是 ctx._source.academies.remove(index),因为ctx在那里不可见。索引变量是正确的...但我不能使用ctx。任何建议?

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


推荐答案

p>

Have you tried this?

ctx._source.academies.removeAll { it['academy_id'] == 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] }

从弹性搜索的角度来看,我认为脚本可以重写如下: / p>

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

这篇关于弹性搜索Groovy脚本eachWithIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆