percentile-aggregation.asciidoc 10 KB

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