适用于弹性搜索文件中字段总和的脚本 [英] Proper groovy script for sum of fields in Elasticsearch documents

查看:124
本文介绍了适用于弹性搜索文件中字段总和的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是对此问题

如果我的文档如下所示:

If my documents look like so:

{"documentid":1,
"documentStats":[ {"foo_1_1":1}, {"foo_2_1":5}, {"boo_1_1":3} ]
}

什么是正确的groovy脚本在script_field中用于返回与特定模式匹配的每个文档的所有documentStats的总和(例如,包含 _1 _

What would be the correct groovy script to be used in a script_field for returning the sum of all documentStats per document that match a particular pattern, (e.g., contain _1_)

推荐答案

类似于所提到的问题,有一个单行方式与您的新结构相同:

Similar to the referred question, there's a one-liner that does the same thing with your new structure:

{
    "query" : {
        ...
    },
    "script_fields" : {
        "sum" : {
            "script" : "_source.documentStats.findAll{ it.keySet()[0] =~'_1_' }.collect{it.values()}.flatten().sum()"
        }
    }
}

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

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