1
0

percentile-aggregation.asciidoc 12 KB

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