extended-stats-bucket-aggregation.asciidoc 4.3 KB

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