【问题标题】:Elasticsearch Groovy Script eachWithIndexElasticsearch Groovy 脚本 eachWithIndex
【发布时间】:2016-08-27 04:40:56
【问题描述】:

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

{
    "script" : "ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) ctx._source.academies.remove(index) }",
    "params": {
        "academy_id": 344
    }
}

【问题讨论】:

    标签: elasticsearch groovy


    【解决方案1】:

    你试过了吗?

    ctx._source.academies.removeAll { it['academy_id'] == academy_id }
    

    如果您需要检查academy_id 是否匹配项目列表,则可以使用以下内容:(闭包应满足条件)

    ctx._source.academies.removeAll { it['academy_id'] in [academy_id, some_other_id] }
    

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

    {
        "script" : "ctx._source.academies.removeAll { it['academy_id'] in academy_ids }",
        "params": {
            "academy_ids": [344, 345, 346]
        }
    }
    

    【讨论】:

    • 你是完美的!!!!!!谢谢.... 我们如何制作 academy_id 数组?我们可以将学院作为数组提供给参数吗?所以我们可以一次删除多个有多个id的学院
    • 当然可以。您只需要一个从闭包返回的布尔值。查看更新的答案。
    【解决方案2】:

    这是最好的解决方案吗?

    def findIndex; ctx._source.academies.eachWithIndex { it, index -> if(it['academy_id'] == academy_id) findIndex = index }; ctx._source.academies.remove(findIndex)
    

    但这仅适用于最后找到的对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      相关资源
      最近更新 更多