2014-01-06 17:35:51 -05:00
[[search-aggregations-metrics-percentile-aggregation]]
2014-05-12 19:35:58 -04:00
=== Percentiles Aggregation
2014-03-07 03:50:50 -05:00
2014-01-06 17:35:51 -05:00
A `multi-value` metrics aggregation that calculates one or more percentiles
over numeric values extracted from the aggregated documents. These values
can be extracted either from specific numeric fields in the documents, or
be generated by a provided script.
Percentiles show the point at which a certain percentage of observed values
2014-05-12 19:35:58 -04:00
occur. For example, the 95th percentile is the value which is greater than 95%
2014-01-06 17:35:51 -05:00
of the observed values.
2014-05-12 19:35:58 -04:00
Percentiles are often used to find outliers. In normal distributions, the
0.13th and 99.87th percentiles represents three standard deviations from the
2014-01-06 17:35:51 -05:00
mean. Any data which falls outside three standard deviations is often considered
an anomaly.
2014-05-12 19:35:58 -04:00
When a range of percentiles are retrieved, they can be used to estimate the
2014-01-06 17:35:51 -05:00
data distribution and determine if the data is skewed, bimodal, etc.
Assume your data consists of website load times. The average and median
load times are not overly useful to an administrator. The max may be interesting,
but it can be easily skewed by a single slow response.
Let's look at a range of percentiles representing load time:
2019-09-05 10:11:25 -04:00
[source,console]
2014-01-06 17:35:51 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2014-01-06 17:35:51 -05:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2014-01-06 17:35:51 -05:00
"aggs" : {
2014-05-12 19:35:58 -04:00
"load_time_outlier" : {
"percentiles" : {
2014-01-06 17:35:51 -05:00
"field" : "load_time" <1>
2014-05-12 19:35:58 -04:00
}
2014-01-06 17:35:51 -05:00
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2014-01-06 17:35:51 -05:00
<1> The field `load_time` must be a numeric field
2014-05-12 19:35:58 -04:00
By default, the `percentile` metric will generate a range of
2014-01-06 17:35:51 -05:00
percentiles: `[ 1, 5, 25, 50, 75, 95, 99 ]`. The response will look like this:
2019-09-06 16:09:09 -04:00
[source,console-result]
2014-01-06 17:35:51 -05:00
--------------------------------------------------
{
...
"aggregations": {
"load_time_outlier": {
2014-05-07 11:36:44 -04:00
"values" : {
2018-02-15 03:23:20 -05:00
"1.0": 5.0,
"5.0": 25.0,
"25.0": 165.0,
2017-08-02 17:47:27 -04:00
"50.0": 445.0,
2018-02-15 03:23:20 -05:00
"75.0": 725.0,
"95.0": 945.0,
"99.0": 985.0
2014-05-07 11:36:44 -04:00
}
2014-01-06 17:35:51 -05:00
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2014-01-06 17:35:51 -05:00
As you can see, the aggregation will return a calculated value for each percentile
2014-05-12 19:35:58 -04:00
in the default range. If we assume response times are in milliseconds, it is
2018-07-02 14:12:49 -04:00
immediately obvious that the webpage normally loads in 10-725ms, but occasionally
spikes to 945-985ms.
2014-01-06 17:35:51 -05:00
Often, administrators are only interested in outliers -- the extreme percentiles.
2014-05-12 19:35:58 -04:00
We can specify just the percents we are interested in (requested percentiles
2014-01-06 17:35:51 -05:00
must be a value between 0-100 inclusive):
2019-09-05 10:11:25 -04:00
[source,console]
2014-01-06 17:35:51 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2014-01-06 17:35:51 -05:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2014-01-06 17:35:51 -05:00
"aggs" : {
2014-05-12 19:35:58 -04:00
"load_time_outlier" : {
"percentiles" : {
2014-01-06 17:35:51 -05:00
"field" : "load_time",
"percents" : [95, 99, 99.9] <1>
2014-05-12 19:35:58 -04:00
}
2014-01-06 17:35:51 -05:00
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2014-01-06 17:35:51 -05:00
<1> Use the `percents` parameter to specify particular percentiles to calculate
2017-04-18 09:57:50 -04:00
==== Keyed Response
2014-01-06 17:35:51 -05:00
2017-04-18 09:57:50 -04:00
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:
2019-09-05 10:11:25 -04:00
[source,console]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2017-04-18 09:57:50 -04:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2017-04-18 09:57:50 -04:00
"aggs": {
2017-08-02 17:47:27 -04:00
"load_time_outlier": {
2017-04-18 09:57:50 -04:00
"percentiles": {
2017-08-02 17:47:27 -04:00
"field": "load_time",
2017-04-18 09:57:50 -04:00
"keyed": false
}
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2017-04-18 09:57:50 -04:00
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-04-18 09:57:50 -04:00
--------------------------------------------------
{
...
"aggregations": {
2017-08-02 17:47:27 -04:00
"load_time_outlier": {
2017-04-18 09:57:50 -04:00
"values": [
{
"key": 1.0,
2018-02-15 03:23:20 -05:00
"value": 5.0
2017-04-18 09:57:50 -04:00
},
{
"key": 5.0,
2018-02-15 03:23:20 -05:00
"value": 25.0
2017-04-18 09:57:50 -04:00
},
{
"key": 25.0,
2018-02-15 03:23:20 -05:00
"value": 165.0
2017-04-18 09:57:50 -04:00
},
{
"key": 50.0,
2017-08-02 17:47:27 -04:00
"value": 445.0
2017-04-18 09:57:50 -04:00
},
{
"key": 75.0,
2018-02-15 03:23:20 -05:00
"value": 725.0
2017-04-18 09:57:50 -04:00
},
{
"key": 95.0,
2018-02-15 03:23:20 -05:00
"value": 945.0
2017-04-18 09:57:50 -04:00
},
{
"key": 99.0,
2018-02-15 03:23:20 -05:00
"value": 985.0
2017-04-18 09:57:50 -04:00
}
]
}
}
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2014-01-06 17:35:51 -05:00
==== Script
The percentile metric supports scripting. For example, if our load times
are in milliseconds but we want percentiles calculated in seconds, we could use
a script to convert them on-the-fly:
2019-09-05 10:11:25 -04:00
[source,console]
2014-01-06 17:35:51 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2014-01-06 17:35:51 -05:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2014-01-06 17:35:51 -05:00
"aggs" : {
2014-05-12 19:35:58 -04:00
"load_time_outlier" : {
"percentiles" : {
2015-05-12 05:37:22 -04:00
"script" : {
2016-06-27 09:55:16 -04:00
"lang": "painless",
2017-06-09 11:29:25 -04:00
"source": "doc['load_time'].value / params.timeUnit", <1>
2015-05-12 05:37:22 -04:00
"params" : {
"timeUnit" : 1000 <2>
}
2014-01-06 17:35:51 -05:00
}
2014-05-12 19:35:58 -04:00
}
2014-01-06 17:35:51 -05:00
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2014-01-06 17:35:51 -05:00
<1> The `field` parameter is replaced with a `script` parameter, which uses the
script to generate values which percentiles are calculated on
<2> Scripting supports parameterized input just like any other script
2017-05-17 17:42:25 -04:00
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:
2015-05-12 05:37:22 -04:00
2019-09-05 10:11:25 -04:00
[source,console]
2015-05-12 05:37:22 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2015-05-12 05:37:22 -04:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2015-05-12 05:37:22 -04:00
"aggs" : {
"load_time_outlier" : {
"percentiles" : {
"script" : {
2017-06-09 11:29:25 -04:00
"id": "my_script",
2017-08-02 17:47:27 -04:00
"params": {
"field": "load_time"
2015-05-12 05:37:22 -04:00
}
}
}
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency,stored_example_script]
2015-05-12 05:37:22 -04:00
2014-06-06 10:25:21 -04:00
[[search-aggregations-metrics-percentile-aggregation-approximation]]
2014-01-06 17:35:51 -05:00
==== Percentiles are (usually) approximate
2014-05-12 19:35:58 -04:00
There are many different algorithms to calculate percentiles. The naive
2014-01-06 17:35:51 -05:00
implementation simply stores all the values in a sorted array. To find the 50th
2014-03-04 07:35:49 -05:00
percentile, you simply find the value that is at `my_array[count(my_array) * 0.5]`.
2014-01-06 17:35:51 -05:00
Clearly, the naive implementation does not scale -- the sorted array grows
linearly with the number of values in your dataset. To calculate percentiles
across potentially billions of values in an Elasticsearch cluster, _approximate_
percentiles are calculated.
The algorithm used by the `percentile` metric is called TDigest (introduced by
2014-05-12 19:35:58 -04:00
Ted Dunning in
2014-01-06 17:35:51 -05:00
https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf[Computing Accurate Quantiles using T-Digests]).
When using this metric, there are a few guidelines to keep in mind:
- Accuracy is proportional to `q(1-q)`. This means that extreme percentiles (e.g. 99%)
are more accurate than less extreme percentiles, such as the median
- For small sets of values, percentiles are highly accurate (and potentially
100% accurate if the data is small enough).
- As the quantity of values in a bucket grows, the algorithm begins to approximate
2014-05-12 19:35:58 -04:00
the percentiles. It is effectively trading accuracy for memory savings. The
exact level of inaccuracy is difficult to generalize, since it depends on your
2014-01-06 17:35:51 -05:00
data distribution and volume of data being aggregated
2014-03-14 07:22:48 -04:00
The following chart shows the relative error on a uniform distribution depending
on the number of collected values and the requested percentile:
image:images/percentiles_error.png[]
It shows how precision is better for extreme percentiles. The reason why error diminishes
for large number of values is that the law of large numbers makes the distribution of
values more and more uniform and the t-digest tree can do a better job at summarizing
it. It would not be the case on more skewed distributions.
2014-06-06 10:25:21 -04:00
[[search-aggregations-metrics-percentile-aggregation-compression]]
2014-01-06 17:35:51 -05:00
==== Compression
Approximate algorithms must balance memory utilization with estimation accuracy.
This balance can be controlled using a `compression` parameter:
2019-09-05 10:11:25 -04:00
[source,console]
2014-01-06 17:35:51 -05:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2014-01-06 17:35:51 -05:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2014-01-06 17:35:51 -05:00
"aggs" : {
2014-05-12 19:35:58 -04:00
"load_time_outlier" : {
"percentiles" : {
2014-01-06 17:35:51 -05:00
"field" : "load_time",
2016-07-27 03:48:35 -04:00
"tdigest": {
"compression" : 200 <1>
}
2014-05-12 19:35:58 -04:00
}
2014-01-06 17:35:51 -05:00
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2014-01-06 17:35:51 -05:00
<1> Compression controls memory usage and approximation error
2014-05-12 19:35:58 -04:00
The TDigest algorithm uses a number of "nodes" to approximate percentiles -- the
2014-01-06 17:35:51 -05:00
more nodes available, the higher the accuracy (and large memory footprint) proportional
2014-05-12 19:35:58 -04:00
to the volume of data. The `compression` parameter limits the maximum number of
2014-05-13 03:11:12 -04:00
nodes to `20 * compression`.
2014-01-06 17:35:51 -05:00
Therefore, by increasing the compression value, you can increase the accuracy of
your percentiles at the cost of more memory. Larger compression values also
make the algorithm slower since the underlying tree data structure grows in size,
resulting in more expensive operations. The default compression value is
`100`.
2014-05-13 03:11:12 -04:00
A "node" uses roughly 32 bytes of memory, so under worst-case scenarios (large amount
2014-01-06 17:35:51 -05:00
of data which arrives sorted and in-order) the default settings will produce a
2014-05-13 03:11:12 -04:00
TDigest roughly 64KB in size. In practice data tends to be more random and
2014-01-06 17:35:51 -05:00
the TDigest will use less memory.
2015-05-07 10:46:40 -04:00
2015-07-20 07:23:21 -04:00
==== HDR Histogram
2017-07-18 08:06:22 -04:00
NOTE: This setting exposes the internal implementation of HDR Histogram and the syntax may change in the future.
2015-07-20 07:23:21 -04:00
2016-07-27 03:48:35 -04:00
https://github.com/HdrHistogram/HdrHistogram[HDR Histogram] (High Dynamic Range Histogram) is an alternative implementation
that can be useful when calculating percentiles for latency measurements as it can be faster than the t-digest implementation
with the trade-off of a larger memory footprint. This implementation maintains a fixed worse-case percentage error (specified
as a number of significant digits). This means that if data is recorded with values from 1 microsecond up to 1 hour
(3,600,000,000 microseconds) in a histogram set to 3 significant digits, it will maintain a value resolution of 1 microsecond
2015-07-20 07:23:21 -04:00
for values up to 1 millisecond and 3.6 seconds (or better) for the maximum tracked value (1 hour).
The HDR Histogram can be used by specifying the `method` parameter in the request:
2019-09-05 10:11:25 -04:00
[source,console]
2015-07-20 07:23:21 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2015-07-20 07:23:21 -04:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2015-07-20 07:23:21 -04:00
"aggs" : {
"load_time_outlier" : {
"percentiles" : {
"field" : "load_time",
"percents" : [95, 99, 99.9],
2016-07-27 03:48:35 -04:00
"hdr": { <1>
"number_of_significant_value_digits" : 3 <2>
}
2015-07-20 07:23:21 -04:00
}
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2016-07-27 03:48:35 -04:00
<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
2015-07-20 07:23:21 -04:00
<2> `number_of_significant_value_digits` specifies the resolution of values for the histogram in number of significant digits
2016-07-27 03:48:35 -04:00
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
2015-07-20 07:23:21 -04:00
the HDRHistogram if the range of values is unknown as this could lead to high memory usage.
2015-05-07 10:46:40 -04:00
==== Missing value
The `missing` parameter defines how documents that are missing a value should be treated.
By default they will be ignored but it is also possible to treat them as if they
had a value.
2019-09-05 10:11:25 -04:00
[source,console]
2015-05-07 10:46:40 -04:00
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET latency/_search
2015-05-07 10:46:40 -04:00
{
2017-08-02 17:47:27 -04:00
"size": 0,
2015-05-07 10:46:40 -04:00
"aggs" : {
"grade_percentiles" : {
"percentiles" : {
"field" : "grade",
"missing": 10 <1>
}
}
}
}
--------------------------------------------------
2017-08-02 17:47:27 -04:00
// TEST[setup:latency]
2015-05-07 10:46:40 -04:00
<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.