Stats Bucket Aggregation

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-aggregations-pipeline-stats-bucket-aggregation.html

译文链接 : Stats Bucket Aggregation

贡献者 : @苏涛,ApacheCNApache中文网

警告

此功能是实验性的,可能会在将来的版本中完全更改或删除。Elastic将采取最大的努力来解决此问题,但实验功能不受SLA官方功能的支持。

统计桶聚合,在一组聚合的所有桶中对一个指定的度量计算各种统计值。指定的度量必须是数字型而且这个桶聚合必须是多桶聚合。

语法

stats_bucket 聚合结构如下:

  1. {
  2. "stats_bucket": {
  3. "buckets_path": "the_sum"
  4. }
  5. }

stats_bucket 参数如下:

参数名称 描述 是否必填 默认值
buckets_path 想要计算统计信息的桶路径,点击 the section called “buckets_path Syntaxedit”查看更多细节 必填
gap_policy 当数据缺口出现时采用的策略,点击the section called “Dealing with gaps in the dataedit”查看更多细节 可选 skip
format 用于规范聚合输出值的格式 可选 null

以下代码段计算每月销售总额的统计信息:

  1. POST /sales/_search
  2. {
  3. "size": 0,
  4. "aggs" : {
  5. "sales_per_month" : {
  6. "date_histogram" : {
  7. "field" : "date",
  8. "interval" : "month"
  9. },
  10. "aggs": {
  11. "sales": {
  12. "sum": {
  13. "field": "price"
  14. }
  15. }
  16. }
  17. },
  18. "stats_monthly_sales": {
  19. "stats_bucket": {
  20. "buckets_path": "sales_per_month>sales" #1
  21. }
  22. }
  23. }
  24. }

| 1 | bucket_path指示这个stats_bucket聚合是要得到sales_per_month日期直方图中的sales聚合的统计信息。 |

可能得到如下的响应:

  1. {
  2. "took": 11,
  3. "timed_out": false,
  4. "_shards": ...,
  5. "hits": ...,
  6. "aggregations": {
  7. "sales_per_month": {
  8. "buckets": [
  9. {
  10. "key_as_string": "2015/01/01 00:00:00",
  11. "key": 1420070400000,
  12. "doc_count": 3,
  13. "sales": {
  14. "value": 550.0
  15. }
  16. },
  17. {
  18. "key_as_string": "2015/02/01 00:00:00",
  19. "key": 1422748800000,
  20. "doc_count": 2,
  21. "sales": {
  22. "value": 60.0
  23. }
  24. },
  25. {
  26. "key_as_string": "2015/03/01 00:00:00",
  27. "key": 1425168000000,
  28. "doc_count": 2,
  29. "sales": {
  30. "value": 375.0
  31. }
  32. }
  33. ]
  34. },
  35. "stats_monthly_sales": {
  36. "count": 3,
  37. "min": 60.0,
  38. "max": 550.0,
  39. "avg": 328.3333333333333,
  40. "sum": 985.0
  41. }
  42. }
  43. }