percentiles-bucket-aggregation.asciidoc 4.3 KB

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