extended-stats-bucket-aggregation.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. [[search-aggregations-pipeline-extended-stats-bucket-aggregation]]
  2. === Extended Stats Bucket Aggregation
  3. A sibling pipeline aggregation which calculates a variety of stats across all bucket of a specified metric in a sibling aggregation.
  4. The specified metric must be numeric and the sibling aggregation must be a multi-bucket aggregation.
  5. This aggregation provides a few more statistics (sum of squares, standard deviation, etc) compared to the `stats_bucket` aggregation.
  6. ==== Syntax
  7. A `extended_stats_bucket` aggregation looks like this in isolation:
  8. [source,js]
  9. --------------------------------------------------
  10. {
  11. "extended_stats_bucket": {
  12. "buckets_path": "the_sum"
  13. }
  14. }
  15. --------------------------------------------------
  16. // NOTCONSOLE
  17. .`extended_stats_bucket` Parameters
  18. |===
  19. |Parameter Name |Description |Required |Default Value
  20. |`buckets_path` |The path to the buckets we wish to calculate stats for (see <<buckets-path-syntax>> for more
  21. details) |Required |
  22. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  23. details)|Optional | `skip`
  24. |`format` |format to apply to the output value of this aggregation |Optional | `null`
  25. |`sigma` |The number of standard deviations above/below the mean to display |Optional | 2
  26. |===
  27. The following snippet calculates the extended stats for monthly `sales` bucket:
  28. [source,js]
  29. --------------------------------------------------
  30. POST /sales/_search
  31. {
  32. "size": 0,
  33. "aggs" : {
  34. "sales_per_month" : {
  35. "date_histogram" : {
  36. "field" : "date",
  37. "interval" : "month"
  38. },
  39. "aggs": {
  40. "sales": {
  41. "sum": {
  42. "field": "price"
  43. }
  44. }
  45. }
  46. },
  47. "stats_monthly_sales": {
  48. "extended_stats_bucket": {
  49. "buckets_path": "sales_per_month>sales" <1>
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // CONSOLE
  56. // TEST[setup:sales]
  57. <1> `bucket_paths` instructs this `extended_stats_bucket` aggregation that we want the calculate stats for the `sales` aggregation in the
  58. `sales_per_month` date histogram.
  59. And the following may be the response:
  60. [source,js]
  61. --------------------------------------------------
  62. {
  63. "took": 11,
  64. "timed_out": false,
  65. "_shards": ...,
  66. "hits": ...,
  67. "aggregations": {
  68. "sales_per_month": {
  69. "buckets": [
  70. {
  71. "key_as_string": "2015/01/01 00:00:00",
  72. "key": 1420070400000,
  73. "doc_count": 3,
  74. "sales": {
  75. "value": 550.0
  76. }
  77. },
  78. {
  79. "key_as_string": "2015/02/01 00:00:00",
  80. "key": 1422748800000,
  81. "doc_count": 2,
  82. "sales": {
  83. "value": 60.0
  84. }
  85. },
  86. {
  87. "key_as_string": "2015/03/01 00:00:00",
  88. "key": 1425168000000,
  89. "doc_count": 2,
  90. "sales": {
  91. "value": 375.0
  92. }
  93. }
  94. ]
  95. },
  96. "stats_monthly_sales": {
  97. "count": 3,
  98. "min": 60.0,
  99. "max": 550.0,
  100. "avg": 328.3333333333333,
  101. "sum": 985.0,
  102. "sum_of_squares": 446725.0,
  103. "variance": 41105.55555555556,
  104. "std_deviation": 202.74505063146563,
  105. "std_deviation_bounds": {
  106. "upper": 733.8234345962646,
  107. "lower": -77.15676792959795
  108. }
  109. }
  110. }
  111. }
  112. --------------------------------------------------
  113. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  114. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  115. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]