ElasticSearch中的加权多项聚合

baubqpgj  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(110)

我试图在Elasticsearch中进行多项聚合,但不是简单地计算包含每组术语的文档,而是希望它对第三个字段的值进行求和。
因此给定数据:

f1  f2  s
    === === ===
    a   a   1
    a   b   2
    b   a   2
    a   b   1
    a   b   2
    a   a   1
    b   b   3
    a   b   3

我想对f1和f2进行多项聚合,使用总和而不是文档计数,与以下内容相同:

{
    aggs: {
        f_1_2_score: {
            multi_term: {
                terms: [{ field: "f1" }, {field: "f2"}],
                scoring: { field: "s" } // the illegal part I'm trying to replace
            }
        }
    }
}

产生以下结果:

...
    buckets: [
        { key: ["a", "a"], key_as_string: "a|a", doc_score: 2 },
        { key: ["a", "b"], key_as_string: "a|b", doc_score: 7 },
        { key: ["b", "a"], key_as_string: "b|a", doc_score: 2 },
        { key: ["b", "b"], key_as_string: "b|b", doc_score: 3 },
    ]
    ...

最简单/最好的方法是什么?

x6492ojm

x6492ojm1#

尝试以下操作,相当于select sum(*) as s group by a, b order by s desc.

{
"size": 0,
"aggs": {
    "key": {
        "multi_terms": {
            "terms": [
                {
                    "field": "f1"
                },
                {
                    "field": "f2"
                }
            ]
        },
        "aggs": {
            "doc_score": {
                "sum": {
                    "field": "s"
                }
            },
            "aggr_sort": {
                "bucket_sort": {
                    "sort": [
                        {
                            "doc_score": {
                                "order": "desc"
                            }
                        }
                    ]
                }
            }
        }
    }
}}

相关问题