Elasticsearch 分析百分比

标签 elasticsearch analytics

我正在使用 Elasticsearch 1.7.3 为分析报告收集数据。

我有一个保存文档的索引,其中每个文档都有一个名为“duration”的数字字段(请求花费了多少毫秒)和一个名为“component”的字符串字段。可以有多个文档具有相同的组件名称。

例如。

{"component": "A", "duration": 10}
{"component": "B", "duration": 27}
{"component": "A", "duration": 5}
{"component": "C", "duration": 2}

我想生成一份报告,说明每个组件:

此组件的所有“持续时间”字段的总和。

A: 15
B: 27
C: 2

此总和占所有 文档持续时间总和的百分比。在我的例子中

A: (10+5) / (10+27+5+2) * 100
B: 27 / (10+27+5+2) * 100
C: 2 / (10+27+5+2) * 100

每个组件的文档占总组件的百分比。

A: 2 / 4 * 100
B: 1 / 4 * 100
C: 1 / 4 * 100

如何使用 Elasticsearch 1.7.3 做到这一点?

最佳答案

在 ES 1.7.3 中,无法根据两个不同聚合的结果计算数据,这在 ES 2.0 中可以通过 pipeline aggregations 完成。 , 尽管。

但是,您所要求的在客户端使用 1.7.3 并不太复杂。如果您使用下面的查询,您将获得获得预期数字所需的一切:

POST components/_search
{
   "size": 0,
   "aggs": {
      "total_duration": {
         "sum": {
            "field": "duration"
         }
      },
      "components": {
         "terms": {
            "field": "component"
         },
         "aggs": {
            "duration_sum": {
               "sum": {
                  "field": "duration"
               }
            }
         }
      }
   }
}

结果看起来像这样:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "total_duration": {
         "value": 44
      },
      "components": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "a",
               "doc_count": 2,
               "duration_sum": {
                  "value": 15
               }
            },
            {
               "key": "b",
               "doc_count": 1,
               "duration_sum": {
                  "value": 27
               }
            },
            {
               "key": "c",
               "doc_count": 1,
               "duration_sum": {
                  "value": 2
               }
            }
         ]
      }
   }
}

现在您需要做的就是以下几点。我使用的是 JavaScript,但您可以使用任何其他可以读取 JSON 的语言来执行此操作。

var response = ...the JSON response above...
var total_duration = response.aggregations.total_duration.value;
var total_docs = response.hits.total;

response.aggregations.components.buckets.forEach(function(comp_stats) {
    // total duration for the component
    var total_duration_comp = comp_stats.duration_sum.value;

    // percentage duration of the component
    var perc_duration_comp = total_duration_comp / total_duration * 100;

    // percentage documents for the component
    var perc_doc_comp = comp_stats.doc_count / total_docs * 100;
});

关于Elasticsearch 分析百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532425/

相关文章:

elasticsearch - Nest.ConnectionSettings.SetJsonSerializerSettingsModifier还能工作吗?

elasticsearch - 在复杂文档中进行 Elasticsearch

google-analytics - 谷歌分析跟踪 pdf 下载

android - 从 Google Analytics 到 Firebase Analytics 的行为流

mysql - 定义用于使用分析的 Web 服务(桌面应用程序)

mysql - dbWriteTable(..., append = T) 在 R 中被覆盖

mysql - 我们可以直接在 MySql 数据库上执行 Mdx 查询吗?如果可以,怎么办?

elasticsearch - ElasticSearch将1.x升级到6.x

elasticsearch - 如何在 Grafana 中重置 "alerting"状态

java - Elasticsearch 不处理 liquibase 加载的数据