简而言之:如果我对每个存储桶的 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
}
}
Run Code Online (Sandbox Code Playgroud)
假设我有两个商店,每个商店有两个不同时间戳的数量。这是聚合的结果:
"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
]
}
]
}
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我现在想在 ElasticSearch 中有一个聚合,它对这些存储桶进行求和。在示例数据中,总和超过 6 和 11。我尝试了以下聚合:
"latest_quantity": {
"sum_bucket": {
"buckets_path": "latest_quantity_per_store>latest_quantity>hits>hits>_source>quantity"
}
}
Run Code Online (Sandbox Code Playgroud)
但这会导致此错误:
{
"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
}
Run Code Online (Sandbox Code Playgroud)
以某种方式从 ElasticSearch 返回数字 17 的正确聚合是什么?
我为我拥有的另一个聚合做了类似的事情,一个平均值而不是一个 top_hits 聚合。
"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
}
}
Run Code Online (Sandbox Code Playgroud)
这按预期工作,结果如下:
"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
}
Run Code Online (Sandbox Code Playgroud)
有一种方法可以混合使用scripted_metric聚合和sum_bucket管道聚合来解决这个问题。脚本化的度量聚合有点复杂,但主要思想是允许您提供自己的分桶算法并从中输出单个度量值。
在您的情况下,您想要做的是找出每个商店的最新数量,然后将这些商店数量相加。解决方案如下所示,我将在下面解释一些细节:
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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,为了使其工作,您需要script.painless.regex.enabled: true在elasticsearch.yml配置文件中进行设置。
在init_script创建TreeMap每个碎片。将map_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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1512 次 |
| 最近记录: |