ElasticSearch从总计算每个桶的百分比

时间:2017-04-20 00:39:30

标签: elasticsearch

我正在使用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
    }
  }
}

我如何计算"平衡" /" total_balance"对于ElasticSearch中的每个项目?我在桶(句点)级别尝试了桶脚本聚合,但是我无法将我的buckets_path设置为total_balance。这篇文章https://discuss.elastic.co/t/combining-two-aggregations-to-get-term-percentage/22201讨论了使用重要术语聚合,但我需要计算使用特定字段,而不是doc_count。我知道我可以在客户端进行简单的计算,但是如果可能的话,我想在ElasticSearch中一起完成这个。

1 个答案:

答案 0 :(得分:1)

不,你不能这样做。在我写这篇文章的时候,我们已经在版本6.1中了。

根据 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax, 只有两种主要类型的聚合管道:父级和兄弟级。

因此,为了从时段桶中引用total_balance聚合,我们应该能够引用一个"叔叔"来自buckets_path属性的聚合,这是不可能的。

相关问题