Percentile Ranks Aggregation

A multi-value metrics aggregation that calculates one or more percentile ranks over numeric values extracted from the aggregated documents. These values can be generated by a provided script or extracted from specific numeric or histogram fields in the documents.

Please see Percentiles are (usually) approximate and Compression for advice regarding approximation and memory use of the percentile ranks aggregation

Percentile rank show the percentage of observed values which are below certain value. For example, if a value is greater than or equal to 95% of the observed values it is said to be at the 95th percentile rank.

Assume your data consists of website load times. You may have a service agreement that 95% of page loads complete within 500ms and 99% of page loads complete within 600ms.

Let’s look at a range of percentiles representing load time:

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "field": "load_time",
  8. "values": [ 500, 600 ]
  9. }
  10. }
  11. }
  12. }

The field load_time must be a numeric field

The response will look like this:

  1. {
  2. ...
  3. "aggregations": {
  4. "load_time_ranks": {
  5. "values": {
  6. "500.0": 90.01,
  7. "600.0": 100.0
  8. }
  9. }
  10. }
  11. }

From this information you can determine you are hitting the 99% load time target but not quite hitting the 95% load time target

Keyed Response

By default the keyed flag is set to true 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:

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "field": "load_time",
  8. "values": [ 500, 600 ],
  9. "keyed": false
  10. }
  11. }
  12. }
  13. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "load_time_ranks": {
  5. "values": [
  6. {
  7. "key": 500.0,
  8. "value": 90.01
  9. },
  10. {
  11. "key": 600.0,
  12. "value": 100.0
  13. }
  14. ]
  15. }
  16. }
  17. }

Script

The percentile rank metric supports scripting. For example, if our load times are in milliseconds but we want to specify values in seconds, we could use a script to convert them on-the-fly:

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "values": [ 500, 600 ],
  8. "script": {
  9. "lang": "painless",
  10. "source": "doc['load_time'].value / params.timeUnit",
  11. "params": {
  12. "timeUnit": 1000
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

The field parameter is replaced with a script parameter, which uses the script to generate values which percentile ranks are calculated on

Scripting supports parameterized input just like any other script

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:

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "values": [ 500, 600 ],
  8. "script": {
  9. "id": "my_script",
  10. "params": {
  11. "field": "load_time"
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }

HDR Histogram

This setting exposes the internal implementation of HDR Histogram and the syntax may change in the future.

HDR Histogram (High Dynamic Range Histogram) is an alternative implementation that can be useful when calculating percentile ranks 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 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 hdr object in the request:

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "field": "load_time",
  8. "values": [ 500, 600 ],
  9. "hdr": {
  10. "number_of_significant_value_digits": 3
  11. }
  12. }
  13. }
  14. }
  15. }

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

number_of_significant_value_digits specifies the resolution of values for the histogram in number of significant digits

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 the HDRHistogram if the range of values is unknown as this could lead to high memory usage.

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.

  1. GET latency/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "load_time_ranks": {
  6. "percentile_ranks": {
  7. "field": "load_time",
  8. "values": [ 500, 600 ],
  9. "missing": 10
  10. }
  11. }
  12. }
  13. }

Documents without a value in the load_time field will fall into the same bucket as documents that have the value 10.