| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 | [[search-aggregations-metrics-percentile-rank-aggregation]]=== Percentile Ranks AggregationA `multi-value` metrics aggregation that calculates one or more percentile ranksover numeric values extracted from the aggregated documents.  These valuescan be extracted either from specific numeric fields in the documents, orbe generated by a provided script.[NOTE]==================================================Please see <<search-aggregations-metrics-percentile-aggregation-approximation>>and <<search-aggregations-metrics-percentile-aggregation-compression>> for adviceregarding approximation and memory use of the percentile ranks aggregation==================================================Percentile rank show the percentage of observed values which are below certainvalue.  For example, if a value is greater than or equal to 95% of the observed valuesit is said to be at the 95th percentile rank.Assume your data consists of website load times.  You may have a service agreement that95% of page loads completely within 500ms and 99% of page loads complete within 600ms.Let's look at a range of percentiles representing load time:[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs" : {        "load_time_ranks" : {            "percentile_ranks" : {                "field" : "load_time", <1>                "values" : [500, 600]            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency]<1> The field `load_time` must be a numeric fieldThe response will look like this:[source,js]--------------------------------------------------{    ...   "aggregations": {      "load_time_ranks": {         "values" : {            "500.0": 55.00000000000001,            "600.0": 64.0         }      }   }}--------------------------------------------------// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]From this information you can determine you are hitting the 99% load time target but not quitehitting the 95% load time target==== Keyed ResponseBy 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:[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs": {        "load_time_ranks": {            "percentile_ranks": {                "field": "load_time",                "values": [500, 600],                "keyed": false            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency]Response:[source,js]--------------------------------------------------{    ...    "aggregations": {        "load_time_ranks": {            "values": [                {                    "key": 500.0,                    "value": 55.00000000000001                },                {                    "key": 600.0,                    "value": 64.0                }            ]        }    }}--------------------------------------------------// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]==== ScriptThe percentile rank metric supports scripting.  For example, if our load timesare in milliseconds but we want to specify values in seconds, we could usea script to convert them on-the-fly:[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs" : {        "load_time_ranks" : {            "percentile_ranks" : {                "values" : [500, 600],                "script" : {                    "lang": "painless",                    "source": "doc['load_time'].value / params.timeUnit", <1>                    "params" : {                        "timeUnit" : 1000   <2>                    }                }            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency]<1> The `field` parameter is replaced with a `script` parameter, which uses thescript to generate values which percentile ranks are calculated on<2> Scripting supports parameterized input just like any other scriptThis 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:[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs" : {        "load_time_ranks" : {            "percentile_ranks" : {                "values" : [500, 600],                "script" : {                    "id": "my_script",                    "params": {                        "field": "load_time"                    }                }            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency,stored_example_script]==== HDR HistogramNOTE: This setting exposes the internal implementation of HDR Histogram and the syntax may change in the future.https://github.com/HdrHistogram/HdrHistogram[HDR Histogram] (High Dynamic Range Histogram) is an alternative implementationthat can be useful when calculating percentile ranks for latency measurements as it can be faster than the t-digest implementationwith the trade-off of a larger memory footprint. This implementation maintains a fixed worse-case percentage error (specified as anumber of significant digits). This means that if data is recorded with values from 1 microsecond up to 1 hour (3,600,000,000microseconds) in a histogram set to 3 significant digits, it will maintain a value resolution of 1 microsecond for values up to1 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:[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs" : {        "load_time_ranks" : {            "percentile_ranks" : {                "field" : "load_time",                "values" : [500, 600],                "hdr": { <1>                  "number_of_significant_value_digits" : 3 <2>                }            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency]<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<2> `number_of_significant_value_digits` specifies the resolution of values for the histogram in number of significant digitsThe HDRHistogram only supports positive values and will error if it is passed a negative value. It is also not a good idea to usethe HDRHistogram if the range of values is unknown as this could lead to high memory usage.==== Missing valueThe `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 theyhad a value.[source,js]--------------------------------------------------GET latency/_search{    "size": 0,    "aggs" : {        "load_time_ranks" : {            "percentile_ranks" : {                "field" : "load_time",                "values" : [500, 600],                "missing": 10 <1>            }        }    }}--------------------------------------------------// CONSOLE// TEST[setup:latency]<1> Documents without a value in the `load_time` field will fall into the same bucket as documents that have the value `10`.
 |