如何在聚合超过子聚合结果的 ElasticSearch 中进行嵌套聚合

How to do nested aggregation in ElasticSearch where aggregation is over results of sub-agg

我正在使用 ElasticSearch 5.3。如果您能指导我如何在 ES 或 Kibana 中完成此操作,我们将不胜感激。我已经阅读了文档,尤其是关于作用域聚合、嵌套聚合和管道聚合的文档,但根本无法进行任何工作或生成我想要的内容。

我不想用通用术语描述我想要的东西,而是想将我的问题表述为关系数据库问题:

这是我的 table:

CREATE TABLE metrics
    (`host` varchar(50), `counter` int, `time` int)
;

INSERT INTO metrics
    (`host`, `counter`, `time`)
VALUES
    ('host1', 3, 1),
    ('host2', 2, 2),
    ('host3', 1, 3)
    ,('host1', 5, 4)
    ,('host2', 2, 5)
    ,('host3', 2, 6)
    ,('host1', 9, 7)
    ,('host2', 3, 8)
    ,('host3', 5, 9)
;

我想获取所有主机的计数器总值。请注意,每个主机都会为某个计数器发出不断增加的值,因此我不能只是去为每条记录添加计数器。相反,我需要使用以下 SQL:

select sum(max_counter)
from ( select max(counter) as max_counter
    from metrics
    where time > 0 AND time < 10
    group by host) as temptable;

产生的正确结果为:17 (= 9 + 3 + 5)

你可以通过管道聚合来实现

{
    "size": 0,
    "aggs": {
        "hosts": {
            "terms": {
                "field": "host"
            },
            "aggs": {
                "maxCounter": {
                    "max": {
                        "field": "counter"
                    }
                }
            }
        },
        "sumCounter": {
            "sum_bucket": {
                "buckets_path": "hosts>maxCounter"
            }
        }
    },
    "query": {
        "range": {
            "time": {
                "gt": 0.0,
                "lt": 10.0
            }
        }
    }
}

首先,您在 hosts 聚合中按 host 字段对条目进行分组。然后在其中应用 max 聚合。然后添加 sum_bucket 聚合,它接受前一个结果和 returns 所需的总和。您还可以使用 range 查询过滤您的条目。

这是一个结果

{
    "took": 22,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 9,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "hosts": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "host1",
                    "doc_count": 3,
                    "maxCounter": {
                        "value": 9.0
                    }
                },
                {
                    "key": "host2",
                    "doc_count": 3,
                    "maxCounter": {
                        "value": 3.0
                    }
                },
                {
                    "key": "host3",
                    "doc_count": 3,
                    "maxCounter": {
                        "value": 5.0
                    }
                }
            ]
        },
        "sumCounter": {
            "value": 17.0
        }
    }
}

sumCounter等于17.

为了以防万一,这里是原始映射

{
    "mappings": {
        "metrics": {
            "properties": {
                "host": {
                    "type": "text",
                    "fielddata": true
                },
                "counter": {
                    "type": "integer"
                },
                "time": {
                    "type": "integer"
                }
            }
        }
    }
}