percentiles-bucket-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. [[search-aggregations-pipeline-percentiles-bucket-aggregation]]
  2. === Percentiles bucket aggregation
  3. ++++
  4. <titleabbrev>Percentiles bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which calculates percentiles 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. ==== Syntax
  9. A `percentiles_bucket` aggregation looks like this in isolation:
  10. [source,js]
  11. --------------------------------------------------
  12. {
  13. "percentiles_bucket": {
  14. "buckets_path": "the_sum"
  15. }
  16. }
  17. --------------------------------------------------
  18. // NOTCONSOLE
  19. [[percentiles-bucket-params]]
  20. .`percentiles_bucket` Parameters
  21. [options="header"]
  22. |===
  23. |Parameter Name |Description |Required |Default Value
  24. |`buckets_path` |The path to the buckets we wish to find the percentiles for (see <<buckets-path-syntax>> for more
  25. details) |Required |
  26. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  27. details)|Optional | `skip`
  28. |`format` |{javadoc}/java.base/java/text/DecimalFormat.html[DecimalFormat pattern] for the
  29. output value. If specified, the formatted value is returned in the aggregation's
  30. `value_as_string` property |Optional | `null`
  31. |`percents` |The list of percentiles to calculate |Optional | `[ 1, 5, 25, 50, 75, 95, 99 ]`
  32. |`keyed` |Flag which returns the range as an hash instead of an array of key-value pairs |Optional | `true`
  33. |===
  34. The following snippet calculates the percentiles for the total monthly `sales` buckets:
  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. "percentiles_monthly_sales": {
  55. "percentiles_bucket": {
  56. "buckets_path": "sales_per_month>sales", <1>
  57. "percents": [ 25.0, 50.0, 75.0 ] <2>
  58. }
  59. }
  60. }
  61. }
  62. --------------------------------------------------
  63. // TEST[setup:sales]
  64. <1> `buckets_path` instructs this percentiles_bucket aggregation that we want to calculate percentiles for
  65. the `sales` aggregation in the `sales_per_month` date histogram.
  66. <2> `percents` specifies which percentiles we wish to calculate, in this case, the 25th, 50th and 75th percentiles.
  67. And the following may be the response:
  68. [source,console-result]
  69. --------------------------------------------------
  70. {
  71. "took": 11,
  72. "timed_out": false,
  73. "_shards": ...,
  74. "hits": ...,
  75. "aggregations": {
  76. "sales_per_month": {
  77. "buckets": [
  78. {
  79. "key_as_string": "2015/01/01 00:00:00",
  80. "key": 1420070400000,
  81. "doc_count": 3,
  82. "sales": {
  83. "value": 550.0
  84. }
  85. },
  86. {
  87. "key_as_string": "2015/02/01 00:00:00",
  88. "key": 1422748800000,
  89. "doc_count": 2,
  90. "sales": {
  91. "value": 60.0
  92. }
  93. },
  94. {
  95. "key_as_string": "2015/03/01 00:00:00",
  96. "key": 1425168000000,
  97. "doc_count": 2,
  98. "sales": {
  99. "value": 375.0
  100. }
  101. }
  102. ]
  103. },
  104. "percentiles_monthly_sales": {
  105. "values" : {
  106. "25.0": 375.0,
  107. "50.0": 375.0,
  108. "75.0": 550.0
  109. }
  110. }
  111. }
  112. }
  113. --------------------------------------------------
  114. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  115. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  116. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  117. ==== Percentiles_bucket implementation
  118. The Percentile Bucket returns the nearest input data point that is not greater than the requested percentile; it does not
  119. interpolate between data points.
  120. The percentiles are calculated exactly and is not an approximation (unlike the Percentiles Metric). This means
  121. the implementation maintains an in-memory, sorted list of your data to compute the percentiles, before discarding the
  122. data. You may run into memory pressure issues if you attempt to calculate percentiles over many millions of
  123. data-points in a single `percentiles_bucket`.