percentiles-bucket-aggregation.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. [[search-aggregations-pipeline-percentiles-bucket-aggregation]]
  2. === Percentiles Bucket Aggregation
  3. A sibling pipeline aggregation which calculates percentiles 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. ==== Syntax
  6. A `percentiles_bucket` aggregation looks like this in isolation:
  7. [source,js]
  8. --------------------------------------------------
  9. {
  10. "percentiles_bucket": {
  11. "buckets_path": "the_sum"
  12. }
  13. }
  14. --------------------------------------------------
  15. // NOTCONSOLE
  16. .`percentiles_bucket` Parameters
  17. |===
  18. |Parameter Name |Description |Required |Default Value
  19. |`buckets_path` |The path to the buckets we wish to find the percentiles for (see <<buckets-path-syntax>> for more
  20. details) |Required |
  21. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  22. details)|Optional | `skip`
  23. |`format` |format to apply to the output value of this aggregation |Optional | `null`
  24. |`percents` |The list of percentiles to calculate |Optional | `[ 1, 5, 25, 50, 75, 95, 99 ]`
  25. |===
  26. The following snippet calculates the percentiles for the total monthly `sales` buckets:
  27. [source,js]
  28. --------------------------------------------------
  29. POST /sales/_search
  30. {
  31. "size": 0,
  32. "aggs" : {
  33. "sales_per_month" : {
  34. "date_histogram" : {
  35. "field" : "date",
  36. "interval" : "month"
  37. },
  38. "aggs": {
  39. "sales": {
  40. "sum": {
  41. "field": "price"
  42. }
  43. }
  44. }
  45. },
  46. "percentiles_monthly_sales": {
  47. "percentiles_bucket": {
  48. "buckets_path": "sales_per_month>sales", <1>
  49. "percents": [ 25.0, 50.0, 75.0 ] <2>
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // CONSOLE
  56. // TEST[setup:sales]
  57. <1> `buckets_path` instructs this percentiles_bucket aggregation that we want to calculate percentiles for
  58. the `sales` aggregation in the `sales_per_month` date histogram.
  59. <2> `percents` specifies which percentiles we wish to calculate, in this case, the 25th, 50th and 75th percentiles.
  60. And the following may be the response:
  61. [source,js]
  62. --------------------------------------------------
  63. {
  64. "took": 11,
  65. "timed_out": false,
  66. "_shards": ...,
  67. "hits": ...,
  68. "aggregations": {
  69. "sales_per_month": {
  70. "buckets": [
  71. {
  72. "key_as_string": "2015/01/01 00:00:00",
  73. "key": 1420070400000,
  74. "doc_count": 3,
  75. "sales": {
  76. "value": 550.0
  77. }
  78. },
  79. {
  80. "key_as_string": "2015/02/01 00:00:00",
  81. "key": 1422748800000,
  82. "doc_count": 2,
  83. "sales": {
  84. "value": 60.0
  85. }
  86. },
  87. {
  88. "key_as_string": "2015/03/01 00:00:00",
  89. "key": 1425168000000,
  90. "doc_count": 2,
  91. "sales": {
  92. "value": 375.0
  93. }
  94. }
  95. ]
  96. },
  97. "percentiles_monthly_sales": {
  98. "values" : {
  99. "25.0": 375.0,
  100. "50.0": 375.0,
  101. "75.0": 550.0
  102. }
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  108. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  109. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  110. ==== Percentiles_bucket implementation
  111. The Percentile Bucket returns the nearest input data point that is not greater than the requested percentile; it does not
  112. interpolate between data points.
  113. The percentiles are calculated exactly and is not an approximation (unlike the Percentiles Metric). This means
  114. the implementation maintains an in-memory, sorted list of your data to compute the percentiles, before discarding the
  115. data. You may run into memory pressure issues if you attempt to calculate percentiles over many millions of
  116. data-points in a single `percentiles_bucket`.