percentile-rank-aggregation.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. [[search-aggregations-metrics-percentile-rank-aggregation]]
  2. === Percentile Ranks Aggregation
  3. A `multi-value` metrics aggregation that calculates one or more percentile ranks
  4. over numeric values extracted from the aggregated documents. These values
  5. can be extracted either from specific numeric fields in the documents, or
  6. be generated by a provided script.
  7. [NOTE]
  8. ==================================================
  9. Please see <<search-aggregations-metrics-percentile-aggregation-approximation>>
  10. and <<search-aggregations-metrics-percentile-aggregation-compression>> for advice
  11. regarding approximation and memory use of the percentile ranks aggregation
  12. ==================================================
  13. Percentile rank show the percentage of observed values which are below certain
  14. value. For example, if a value is greater than or equal to 95% of the observed values
  15. it is said to be at the 95th percentile rank.
  16. Assume your data consists of website load times. You may have a service agreement that
  17. 95% of page loads completely within 500ms and 99% of page loads complete within 600ms.
  18. Let's look at a range of percentiles representing load time:
  19. [source,js]
  20. --------------------------------------------------
  21. GET latency/_search
  22. {
  23. "size": 0,
  24. "aggs" : {
  25. "load_time_ranks" : {
  26. "percentile_ranks" : {
  27. "field" : "load_time", <1>
  28. "values" : [500, 600]
  29. }
  30. }
  31. }
  32. }
  33. --------------------------------------------------
  34. // CONSOLE
  35. // TEST[setup:latency]
  36. <1> The field `load_time` must be a numeric field
  37. The response will look like this:
  38. [source,js]
  39. --------------------------------------------------
  40. {
  41. ...
  42. "aggregations": {
  43. "load_time_ranks": {
  44. "values" : {
  45. "500.0": 55.00000000000001,
  46. "600.0": 64.0
  47. }
  48. }
  49. }
  50. }
  51. --------------------------------------------------
  52. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  53. From this information you can determine you are hitting the 99% load time target but not quite
  54. hitting the 95% load time target
  55. ==== Keyed Response
  56. By default the `keyed` flag is set to `true` associates a unique string key with each bucket and returns the ranges as a hash rather than an array. Setting the `keyed` flag to `false` will disable this behavior:
  57. [source,js]
  58. --------------------------------------------------
  59. GET latency/_search
  60. {
  61. "size": 0,
  62. "aggs": {
  63. "load_time_ranks": {
  64. "percentile_ranks": {
  65. "field": "load_time",
  66. "values": [500, 600],
  67. "keyed": false
  68. }
  69. }
  70. }
  71. }
  72. --------------------------------------------------
  73. // CONSOLE
  74. // TEST[setup:latency]
  75. Response:
  76. [source,js]
  77. --------------------------------------------------
  78. {
  79. ...
  80. "aggregations": {
  81. "load_time_ranks": {
  82. "values": [
  83. {
  84. "key": 500.0,
  85. "value": 55.00000000000001
  86. },
  87. {
  88. "key": 600.0,
  89. "value": 64.0
  90. }
  91. ]
  92. }
  93. }
  94. }
  95. --------------------------------------------------
  96. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  97. ==== Script
  98. The percentile rank metric supports scripting. For example, if our load times
  99. are in milliseconds but we want to specify values in seconds, we could use
  100. a script to convert them on-the-fly:
  101. [source,js]
  102. --------------------------------------------------
  103. GET latency/_search
  104. {
  105. "size": 0,
  106. "aggs" : {
  107. "load_time_ranks" : {
  108. "percentile_ranks" : {
  109. "values" : [500, 600],
  110. "script" : {
  111. "lang": "painless",
  112. "source": "doc['load_time'].value / params.timeUnit", <1>
  113. "params" : {
  114. "timeUnit" : 1000 <2>
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. --------------------------------------------------
  122. // CONSOLE
  123. // TEST[setup:latency]
  124. <1> The `field` parameter is replaced with a `script` parameter, which uses the
  125. script to generate values which percentile ranks are calculated on
  126. <2> Scripting supports parameterized input just like any other script
  127. This will interpret the `script` parameter as an `inline` script with the `painless` script language and no script parameters. To use a stored script use the following syntax:
  128. [source,js]
  129. --------------------------------------------------
  130. GET latency/_search
  131. {
  132. "size": 0,
  133. "aggs" : {
  134. "load_time_ranks" : {
  135. "percentile_ranks" : {
  136. "values" : [500, 600],
  137. "script" : {
  138. "id": "my_script",
  139. "params": {
  140. "field": "load_time"
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. --------------------------------------------------
  148. // CONSOLE
  149. // TEST[setup:latency,stored_example_script]
  150. ==== HDR Histogram
  151. NOTE: This setting exposes the internal implementation of HDR Histogram and the syntax may change in the future.
  152. https://github.com/HdrHistogram/HdrHistogram[HDR Histogram] (High Dynamic Range Histogram) is an alternative implementation
  153. that can be useful when calculating percentile ranks for latency measurements as it can be faster than the t-digest implementation
  154. with the trade-off of a larger memory footprint. This implementation maintains a fixed worse-case percentage error (specified as a
  155. number of significant digits). This means that if data is recorded with values from 1 microsecond up to 1 hour (3,600,000,000
  156. microseconds) in a histogram set to 3 significant digits, it will maintain a value resolution of 1 microsecond for values up to
  157. 1 millisecond and 3.6 seconds (or better) for the maximum tracked value (1 hour).
  158. The HDR Histogram can be used by specifying the `method` parameter in the request:
  159. [source,js]
  160. --------------------------------------------------
  161. GET latency/_search
  162. {
  163. "size": 0,
  164. "aggs" : {
  165. "load_time_ranks" : {
  166. "percentile_ranks" : {
  167. "field" : "load_time",
  168. "values" : [500, 600],
  169. "hdr": { <1>
  170. "number_of_significant_value_digits" : 3 <2>
  171. }
  172. }
  173. }
  174. }
  175. }
  176. --------------------------------------------------
  177. // CONSOLE
  178. // TEST[setup:latency]
  179. <1> `hdr` object indicates that HDR Histogram should be used to calculate the percentiles and specific settings for this algorithm can be specified inside the object
  180. <2> `number_of_significant_value_digits` specifies the resolution of values for the histogram in number of significant digits
  181. The HDRHistogram only supports positive values and will error if it is passed a negative value. It is also not a good idea to use
  182. the HDRHistogram if the range of values is unknown as this could lead to high memory usage.
  183. ==== Missing value
  184. The `missing` parameter defines how documents that are missing a value should be treated.
  185. By default they will be ignored but it is also possible to treat them as if they
  186. had a value.
  187. [source,js]
  188. --------------------------------------------------
  189. GET latency/_search
  190. {
  191. "size": 0,
  192. "aggs" : {
  193. "load_time_ranks" : {
  194. "percentile_ranks" : {
  195. "field" : "load_time",
  196. "values" : [500, 600],
  197. "missing": 10 <1>
  198. }
  199. }
  200. }
  201. }
  202. --------------------------------------------------
  203. // CONSOLE
  204. // TEST[setup:latency]
  205. <1> Documents without a value in the `load_time` field will fall into the same bucket as documents that have the value `10`.