在顶部聚合上求和(_H)



简而言之,问题是:如果我对每个bucket的top_hits进行了聚合,那么如何对结果结构中的特定值求和?

详细信息:

我有许多记录,每个商店都包含一定数量的记录。我想得到每家商店所有最新唱片的总数。

为了获得每个商店的最新记录,我创建了以下聚合:

"latest_quantity_per_store": {
"aggs": {
"latest_quantity": {
"top_hits": {
"sort": [
{
"datetime": "desc"
},
{
"quantity": "asc"
}
],
"_source": {
"includes": [
"quantity"
]
},
"size": 1
}
}
},
"terms": {
"field": "store",
"size": 10000
}
}

假设我有两个商店,对于两个不同的时间戳,每个商店有两个数量。这是聚合的结果:

"latest_quantity_per_store": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "01",
"doc_count": 2,
"latest_quantity": {
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "inventory-local",
"_type": "doc",
"_id": "O6wFD2UBG8e7nvSU8dYg",
"_score": null,
"_source": {
"quantity": 6
},
"sort": [
1532476800000,
6
]
}
]
}
}
},
{
"key": "02",
"doc_count": 2,
"latest_quantity": {
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "inventory-local",
"_type": "doc",
"_id": "pLUFD2UBHBuSGcoH0ZT4",
"_score": null,
"_source": {
"quantity": 11
},
"sort": [
1532476800000,
11
]
}
]
}
}
}
]
}

我现在想在ElasticSearch中有一个聚合,它接受这些桶的总和。在示例数据中,6和11的总和。我尝试了以下聚合:

"latest_quantity": {
"sum_bucket": {
"buckets_path": "latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity"
}
}

但这导致了这个错误:

{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "No aggregation [hits] found for path [latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity]"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "inventory-local",
"node": "3z5CqmmAQ-yT2sUCb69DzA",
"reason": {
"type": "illegal_argument_exception",
"reason": "No aggregation [hits] found for path [latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity]"
}
}
]
},
"status": 400
}

从ElasticSearch中以某种方式返回数字17的正确聚合是什么

我为我的另一个聚合做了类似的事情,一个平均值而不是顶部聚合。

"average_quantity": {
"sum_bucket": {
"buckets_path": "average_quantity_per_store>average_quantity"
}
},
"average_quantity_per_store": {
"aggs": {
"average_quantity": {
"avg": {
"field": "quantity"
}
}
},
"terms": {
"field": "store",
"size": 10000
}
}

这正如预期的那样,这就是结果:

"average_quantity_per_store": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "01",
"doc_count": 2,
"average_quantity": {
"value": 6
}
},
{
"key": "02",
"doc_count": 2,
"average_quantity": {
"value": 11.5
}
}
]
},
"average_quantity": {
"value": 17.5
}

有一种方法可以通过混合使用scripted_metric聚合和sum_bucket管道聚合来解决此问题。脚本化的度量聚合有点复杂,但其主要思想是允许您提供自己的bucketing算法,并从中得出单个度量数字

在你的情况下,你想做的是计算出每个商店的最新数量,然后将这些商店的数量相加。解决方案看起来是这样的,我将在下面解释一些细节:

POST inventory-local/_search
{
"size": 0,
"aggs": {
"bystore": {
"terms": {
"field": "store.keyword",
"size": 10000
},
"aggs": {
"latest_quantity": {
"scripted_metric": {
"init_script": "params._agg.quantities = new TreeMap()",
"map_script": "params._agg.quantities.put(doc.datetime.date, [doc.datetime.date.millis, doc.quantity.value])",
"combine_script": "return params._agg.quantities.lastEntry().getValue()",
"reduce_script": "def maxkey = 0; def qty = 0; for (a in params._aggs) {def currentKey = a[0]; if (currentKey > maxkey) {maxkey = currentKey; qty = a[1]} } return qty;"
}
}
}
},
"sum_latest_quantities": {
"sum_bucket": {
"buckets_path": "bystore>latest_quantity.value"
}
}
}
}

请注意,为了使其工作,您需要在elasticsearch.yml配置文件中设置script.painless.regex.enabled: true

init_script为每个碎片创建一个TreeMapmap_script用日期/数量的映射填充每个碎片上的TreeMap。我们放入映射中的值包含单个字符串中的时间戳和数量。我们稍后将在reduce_script中需要该时间戳。combine_script只是取TreeMap的最后一个值,因为这是给定碎片的最新数量。大部分工作位于reduce_script中。我们迭代每个碎片的所有最新数量,并返回最新数量。

目前,我们有每家商店的最新数量。剩下要做的就是使用sum_bucket管道聚合来对每个存储数量求和。结果是17。

回复如下:

"aggregations": {
"bystore": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "01",
"doc_count": 2,
"latest_quantity": {
"value": 6
}
},
{
"key": "02",
"doc_count": 2,
"latest_quantity": {
"value": 11
}
}
]
},
"sum_latest_quantities": {
"value": 17
}
}

最新更新