percentile-aggregation.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. [[search-aggregations-metrics-percentile-aggregation]]
  2. === Percentiles aggregation
  3. ++++
  4. <titleabbrev>Percentiles</titleabbrev>
  5. ++++
  6. A `multi-value` metrics aggregation that calculates one or more percentiles
  7. over numeric values extracted from the aggregated documents. These values can be
  8. generated by a provided script or extracted from specific numeric or
  9. <<histogram,histogram fields>> in the documents.
  10. Percentiles show the point at which a certain percentage of observed values
  11. occur. For example, the 95th percentile is the value which is greater than 95%
  12. of the observed values.
  13. Percentiles are often used to find outliers. In normal distributions, the
  14. 0.13th and 99.87th percentiles represents three standard deviations from the
  15. mean. Any data which falls outside three standard deviations is often considered
  16. an anomaly.
  17. When a range of percentiles are retrieved, they can be used to estimate the
  18. data distribution and determine if the data is skewed, bimodal, etc.
  19. Assume your data consists of website load times. The average and median
  20. load times are not overly useful to an administrator. The max may be interesting,
  21. but it can be easily skewed by a single slow response.
  22. Let's look at a range of percentiles representing load time:
  23. [source,console]
  24. --------------------------------------------------
  25. GET latency/_search
  26. {
  27. "size": 0,
  28. "aggs": {
  29. "load_time_outlier": {
  30. "percentiles": {
  31. "field": "load_time" <1>
  32. }
  33. }
  34. }
  35. }
  36. --------------------------------------------------
  37. // TEST[setup:latency]
  38. <1> The field `load_time` must be a numeric field
  39. By default, the `percentile` metric will generate a range of
  40. percentiles: `[ 1, 5, 25, 50, 75, 95, 99 ]`. The response will look like this:
  41. [source,console-result]
  42. --------------------------------------------------
  43. {
  44. ...
  45. "aggregations": {
  46. "load_time_outlier": {
  47. "values": {
  48. "1.0": 5.0,
  49. "5.0": 25.0,
  50. "25.0": 165.0,
  51. "50.0": 445.0,
  52. "75.0": 725.0,
  53. "95.0": 945.0,
  54. "99.0": 985.0
  55. }
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  61. As you can see, the aggregation will return a calculated value for each percentile
  62. in the default range. If we assume response times are in milliseconds, it is
  63. immediately obvious that the webpage normally loads in 10-725ms, but occasionally
  64. spikes to 945-985ms.
  65. Often, administrators are only interested in outliers -- the extreme percentiles.
  66. We can specify just the percents we are interested in (requested percentiles
  67. must be a value between 0-100 inclusive):
  68. [source,console]
  69. --------------------------------------------------
  70. GET latency/_search
  71. {
  72. "size": 0,
  73. "aggs": {
  74. "load_time_outlier": {
  75. "percentiles": {
  76. "field": "load_time",
  77. "percents": [ 95, 99, 99.9 ] <1>
  78. }
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. // TEST[setup:latency]
  84. <1> Use the `percents` parameter to specify particular percentiles to calculate
  85. ==== Keyed Response
  86. By default the `keyed` flag is set to `true` which 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:
  87. [source,console]
  88. --------------------------------------------------
  89. GET latency/_search
  90. {
  91. "size": 0,
  92. "aggs": {
  93. "load_time_outlier": {
  94. "percentiles": {
  95. "field": "load_time",
  96. "keyed": false
  97. }
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. // TEST[setup:latency]
  103. Response:
  104. [source,console-result]
  105. --------------------------------------------------
  106. {
  107. ...
  108. "aggregations": {
  109. "load_time_outlier": {
  110. "values": [
  111. {
  112. "key": 1.0,
  113. "value": 5.0
  114. },
  115. {
  116. "key": 5.0,
  117. "value": 25.0
  118. },
  119. {
  120. "key": 25.0,
  121. "value": 165.0
  122. },
  123. {
  124. "key": 50.0,
  125. "value": 445.0
  126. },
  127. {
  128. "key": 75.0,
  129. "value": 725.0
  130. },
  131. {
  132. "key": 95.0,
  133. "value": 945.0
  134. },
  135. {
  136. "key": 99.0,
  137. "value": 985.0
  138. }
  139. ]
  140. }
  141. }
  142. }
  143. --------------------------------------------------
  144. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  145. ==== Script
  146. The percentile metric supports scripting. For example, if our load times
  147. are in milliseconds but we want percentiles calculated in seconds, we could use
  148. a script to convert them on-the-fly:
  149. [source,console]
  150. --------------------------------------------------
  151. GET latency/_search
  152. {
  153. "size": 0,
  154. "aggs": {
  155. "load_time_outlier": {
  156. "percentiles": {
  157. "script": {
  158. "lang": "painless",
  159. "source": "doc['load_time'].value / params.timeUnit", <1>
  160. "params": {
  161. "timeUnit": 1000 <2>
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. --------------------------------------------------
  169. // TEST[setup:latency]
  170. <1> The `field` parameter is replaced with a `script` parameter, which uses the
  171. script to generate values which percentiles are calculated on
  172. <2> Scripting supports parameterized input just like any other script
  173. 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:
  174. [source,console]
  175. --------------------------------------------------
  176. GET latency/_search
  177. {
  178. "size": 0,
  179. "aggs": {
  180. "load_time_outlier": {
  181. "percentiles": {
  182. "script": {
  183. "id": "my_script",
  184. "params": {
  185. "field": "load_time"
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. --------------------------------------------------
  193. // TEST[setup:latency,stored_example_script]
  194. [[search-aggregations-metrics-percentile-aggregation-approximation]]
  195. ==== Percentiles are (usually) approximate
  196. There are many different algorithms to calculate percentiles. The naive
  197. implementation simply stores all the values in a sorted array. To find the 50th
  198. percentile, you simply find the value that is at `my_array[count(my_array) * 0.5]`.
  199. Clearly, the naive implementation does not scale -- the sorted array grows
  200. linearly with the number of values in your dataset. To calculate percentiles
  201. across potentially billions of values in an Elasticsearch cluster, _approximate_
  202. percentiles are calculated.
  203. The algorithm used by the `percentile` metric is called TDigest (introduced by
  204. Ted Dunning in
  205. https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf[Computing Accurate Quantiles using T-Digests]).
  206. When using this metric, there are a few guidelines to keep in mind:
  207. - Accuracy is proportional to `q(1-q)`. This means that extreme percentiles (e.g. 99%)
  208. are more accurate than less extreme percentiles, such as the median
  209. - For small sets of values, percentiles are highly accurate (and potentially
  210. 100% accurate if the data is small enough).
  211. - As the quantity of values in a bucket grows, the algorithm begins to approximate
  212. the percentiles. It is effectively trading accuracy for memory savings. The
  213. exact level of inaccuracy is difficult to generalize, since it depends on your
  214. data distribution and volume of data being aggregated
  215. The following chart shows the relative error on a uniform distribution depending
  216. on the number of collected values and the requested percentile:
  217. image:images/percentiles_error.png[]
  218. It shows how precision is better for extreme percentiles. The reason why error diminishes
  219. for large number of values is that the law of large numbers makes the distribution of
  220. values more and more uniform and the t-digest tree can do a better job at summarizing
  221. it. It would not be the case on more skewed distributions.
  222. [WARNING]
  223. ====
  224. Percentile aggregations are also
  225. {wikipedia}/Nondeterministic_algorithm[non-deterministic].
  226. This means you can get slightly different results using the same data.
  227. ====
  228. [[search-aggregations-metrics-percentile-aggregation-compression]]
  229. ==== Compression
  230. Approximate algorithms must balance memory utilization with estimation accuracy.
  231. This balance can be controlled using a `compression` parameter:
  232. [source,console]
  233. --------------------------------------------------
  234. GET latency/_search
  235. {
  236. "size": 0,
  237. "aggs": {
  238. "load_time_outlier": {
  239. "percentiles": {
  240. "field": "load_time",
  241. "tdigest": {
  242. "compression": 200 <1>
  243. }
  244. }
  245. }
  246. }
  247. }
  248. --------------------------------------------------
  249. // TEST[setup:latency]
  250. <1> Compression controls memory usage and approximation error
  251. // tag::t-digest[]
  252. The TDigest algorithm uses a number of "nodes" to approximate percentiles -- the
  253. more nodes available, the higher the accuracy (and large memory footprint) proportional
  254. to the volume of data. The `compression` parameter limits the maximum number of
  255. nodes to `20 * compression`.
  256. Therefore, by increasing the compression value, you can increase the accuracy of
  257. your percentiles at the cost of more memory. Larger compression values also
  258. make the algorithm slower since the underlying tree data structure grows in size,
  259. resulting in more expensive operations. The default compression value is
  260. `100`.
  261. A "node" uses roughly 32 bytes of memory, so under worst-case scenarios (large amount
  262. of data which arrives sorted and in-order) the default settings will produce a
  263. TDigest roughly 64KB in size. In practice data tends to be more random and
  264. the TDigest will use less memory.
  265. // end::t-digest[]
  266. ==== HDR Histogram
  267. NOTE: This setting exposes the internal implementation of HDR Histogram and the syntax may change in the future.
  268. https://github.com/HdrHistogram/HdrHistogram[HDR Histogram] (High Dynamic Range Histogram) is an alternative implementation
  269. that can be useful when calculating percentiles for latency measurements as it can be faster than the t-digest implementation
  270. with the trade-off of a larger memory footprint. This implementation maintains a fixed worse-case percentage error (specified
  271. as a number of significant digits). This means that if data is recorded with values from 1 microsecond up to 1 hour
  272. (3,600,000,000 microseconds) in a histogram set to 3 significant digits, it will maintain a value resolution of 1 microsecond
  273. for values up to 1 millisecond and 3.6 seconds (or better) for the maximum tracked value (1 hour).
  274. The HDR Histogram can be used by specifying the `method` parameter in the request:
  275. [source,console]
  276. --------------------------------------------------
  277. GET latency/_search
  278. {
  279. "size": 0,
  280. "aggs": {
  281. "load_time_outlier": {
  282. "percentiles": {
  283. "field": "load_time",
  284. "percents": [ 95, 99, 99.9 ],
  285. "hdr": { <1>
  286. "number_of_significant_value_digits": 3 <2>
  287. }
  288. }
  289. }
  290. }
  291. }
  292. --------------------------------------------------
  293. // TEST[setup:latency]
  294. <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
  295. <2> `number_of_significant_value_digits` specifies the resolution of values for the histogram in number of significant digits
  296. 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
  297. the HDRHistogram if the range of values is unknown as this could lead to high memory usage.
  298. ==== Missing value
  299. The `missing` parameter defines how documents that are missing a value should be treated.
  300. By default they will be ignored but it is also possible to treat them as if they
  301. had a value.
  302. [source,console]
  303. --------------------------------------------------
  304. GET latency/_search
  305. {
  306. "size": 0,
  307. "aggs": {
  308. "grade_percentiles": {
  309. "percentiles": {
  310. "field": "grade",
  311. "missing": 10 <1>
  312. }
  313. }
  314. }
  315. }
  316. --------------------------------------------------
  317. // TEST[setup:latency]
  318. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.