我正在使用ElasticSearch v5。我正在尝试在Elasticsearch analytics percent中执行类似的操作,其中我有一个术语聚合,我想计算一个百分比,它是每个桶中所有桶总数的值。这是我的要求:
{
"query": {
"match_all": {}
},
"aggs": {
"periods": {
"terms": {
"field": "periods",
"size": 3
},
"aggs": {
"balance": {
"sum": {
"field": "balance"
}
}
}
},
"total_balance": {
"sum_bucket": {
"buckets_path": "periods>balance"
}
}
}
}
我得到的结果如下:
{
"aggregations": {
"periods": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 1018940846,
"buckets": [
{
"key": 1177977600000,
"doc_count": 11615418,
"balance": {
"value": 2492032741768.1616
}
},
{
"key": 1185926400000,
"doc_count": 11592425,
"balance": {
"value": 2575365325406.6533
}
},
{
"key": 1175385600000,
"doc_count": 11477402,
"balance": {
"value": 2456256695380.8306
}
}
]
},
"total_balance": {
"value": 7523654762555.645
}
}
}
如何从ElasticSearch计算存储桶中每个项目的“balance”/“total_balance”?我在桶(句点)级别尝试了桶脚本聚合,但是我无法将我的buckets_path设置为total_balance。这篇文章https://discuss.elastic.co/t/combining-two-aggregations-to-get-term-percentage/22201谈到使用重要术语聚合,但我需要计算使用特定字段,而不是doc_count。我知道我可以在客户端进行简单的计算,但是如果可能的话,我想在ElasticSearch中一起完成。
不,你做不到。当我写这篇文章时,我们的版本是6.1。
根据https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax的说法,只有两种主要类型的聚合管道:父母和兄弟姐妹。
因此,为了从时段桶中引用total_balance
聚合,我们应该能够从buckets_path
属性引用“叔叔”聚合,这是不可能的。