percentile-aggregation.asciidoc 12 KB

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