ElasticSearch 计算每个桶占总数的百分比 [英] ElasticSearch calculate percentage for each bucket from total

查看:30
本文介绍了ElasticSearch 计算每个桶占总数的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ElasticSearch v5.我正在尝试执行 Elasticsearch 分析百分比 中描述的类似操作,其中我有一个术语聚合并且我想要计算一个百分比,该百分比是每个存储桶中所有存储桶总数的值.这是我的要求:

I'm using ElasticSearch v5. I'm trying to do something similar described in Elasticsearch analytics percent where I have a terms aggregation and I want to calculate a percentage which is a value from each bucket over the total of all buckets. This is my request:

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "periods": {
      "terms": { 
        "field": "periods",
        "size": 3
      },
      "aggs": {
        "balance": {
          "sum": {
            "field": "balance"
          }
        }
      }
    },
    "total_balance": {
        "sum_bucket": {
            "buckets_path": "periods>balance" 
        }
    }
  }

}

我得到的结果是这样的:

The result I get back this like this:

{
  "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 存储桶中每个项目的余额"/总余额"?我在存储桶(句点)级别尝试了存储桶脚本聚合,但无法将存储桶路径设置为 total_balance.这篇文章 https://discuss.elastic.co/t/combining-two-aggregations-to-get-term-percentage/22201 谈到使用重要术语聚合,但我需要计算使用特定字段,而不是 doc_count.我知道我可以在客户端做一个简单的计算,但如果可能的话,我想在 ElasticSearch 中一起做这一切.

How do I calculate "balance"/"total_balance" for each item in the bucket from ElasticSearch? I tried bucket script aggregation at the bucket (periods) level, but I cannot set my buckets_path to total_balance. This post https://discuss.elastic.co/t/combining-two-aggregations-to-get-term-percentage/22201 talks about using Significant Terms Aggregation, but I need calculation of using specific fields, not doc_count. I know I can do this as a simple calculation on the client side, but I would like to do this all together in ElasticSearch if possible.

推荐答案

不,你不能那样做.到我写这篇文章的时候,我们已经是 6.1 版了.

No, you can't do that. By the time I'm writing this post, we're in version 6.1.

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

According to https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#buckets-path-syntax, there's only two major types of aggregations pipelines: parent and siblings.

因此,为了从周期存储桶中引用 total_balance 聚合,我们应该能够从 buckets_path 属性中引用叔叔"聚合,即不可能.

So, in order to reference the total_balance aggregation from within the periods buckets, we should be able to reference an "uncle" aggregation from the buckets_path attribute, which is not possible.

这篇关于ElasticSearch 计算每个桶占总数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆