【问题标题】:Elasticsearch analytics percentElasticsearch 分析百分比
【发布时间】:2016-11-01 19:40:35
【问题描述】:

我正在使用 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 做到这一点?

【问题讨论】:

标签: elasticsearch analytics


【解决方案1】:

在 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;
});

【讨论】:

  • 我想回答这个问题.. :-( . 完美总结@Val
  • 谢谢瓦尔。我的初始查询或多或少是相同的,但是我试图扩展它以获取我在同一个 GET 查询中需要的其余统计信息。我没有意识到我必须使用额外的代码来获取其余信息。
  • 我已经迁移到 ES 2.0,所以现在我可以使用管道聚合了。哪种管道聚合将帮助我实现我的要求?
  • @TsiyonaDershowitz:看起来你不能:stackoverflow.com/questions/43508321/…
【解决方案2】:

在ElasticSearch[2.x]中,可以使用bucket script aggregation,完全满足你的需求!

例如:

{
    "bucket_script": {
        "buckets_path": {
            "my_var1": "the_sum", 
            "my_var2": "the_value_count"
        },
        "script": "my_var1 / my_var2"
    }
}

详情:

POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "t-shirts": {
                  "filter": {
                    "term": {
                      "type": "t-shirt"
                    }
                  },
                  "aggs": {
                    "sales": {
                      "sum": {
                        "field": "price"
                      }
                    }
                  }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                          "tShirtSales": "t-shirts>sales",
                          "totalSales": "total_sales"
                        },
                        "script": "params.tShirtSales / params.totalSales * 100"
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多