| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 | [role="xpack"][testenv="basic"][[search-aggregations-metrics-boxplot-aggregation]]=== Boxplot aggregation++++<titleabbrev>Boxplot</titleabbrev>++++A `boxplot` metrics aggregation that computes boxplot of numeric values extracted from the aggregated documents.These values can be generated by a provided script or extracted from specific numeric or<<histogram,histogram fields>> in the documents.The `boxplot` aggregation returns essential information for making a {wikipedia}/Box_plot[box plot]: minimum, maximum,median, first quartile (25th percentile)  and third quartile (75th percentile) values.==== SyntaxA `boxplot` aggregation looks like this in isolation:[source,js]--------------------------------------------------{  "boxplot": {    "field": "load_time"  }}--------------------------------------------------// NOTCONSOLELet's look at a boxplot representing load time:[source,console]--------------------------------------------------GET latency/_search{  "size": 0,  "aggs": {    "load_time_boxplot": {      "boxplot": {        "field": "load_time" <1>      }    }  }}--------------------------------------------------// TEST[setup:latency]<1> The field `load_time` must be a numeric fieldThe response will look like this:[source,console-result]--------------------------------------------------{  ... "aggregations": {    "load_time_boxplot": {      "min": 0.0,      "max": 990.0,      "q1": 165.0,      "q2": 445.0,      "q3": 725.0,      "lower": 0.0,      "upper": 990.0    }  }}--------------------------------------------------// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]In this case, the lower and upper whisker values are equal to the min and max.  In general, these values are the 1.5 *IQR range, which is to say the nearest values to `q1 - (1.5 * IQR)` and `q3 + (1.5 * IQR)`.  Since this is an approximation, the given valuesmay not actually be observed values from the data, but should be within a reasonable error bound of them.  While the Boxplot aggregationdoesn't directly return outlier points, you can check if `lower > min` or `upper < max` to see if outliers exist on either side, and thenquery for them directly.==== ScriptThe boxplot metric supports scripting.  For example, if our load timesare in milliseconds but we want values calculated in seconds, we could usea script to convert them on-the-fly:[source,console]--------------------------------------------------GET latency/_search{  "size": 0,  "aggs": {    "load_time_boxplot": {      "boxplot": {        "script": {          "lang": "painless",          "source": "doc['load_time'].value / params.timeUnit", <1>          "params": {            "timeUnit": 1000                                    <2>          }        }      }    }  }}--------------------------------------------------// TEST[setup:latency]<1> The `field` parameter is replaced with a `script` parameter, which uses thescript to generate values which percentiles 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 astored script use the following syntax:[source,console]--------------------------------------------------GET latency/_search{  "size": 0,  "aggs": {    "load_time_boxplot": {      "boxplot": {        "script": {          "id": "my_script",          "params": {            "field": "load_time"          }        }      }    }  }}--------------------------------------------------// TEST[setup:latency,stored_example_script][[search-aggregations-metrics-boxplot-aggregation-approximation]]==== Boxplot values are (usually) approximateThe algorithm used by the `boxplot` metric is called TDigest (introduced byTed Dunning inhttps://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf[Computing Accurate Quantiles using T-Digests]).[WARNING]====Boxplot as other percentile aggregations are also{wikipedia}/Nondeterministic_algorithm[non-deterministic].This means you can get slightly different results using the same data.====[[search-aggregations-metrics-boxplot-aggregation-compression]]==== CompressionApproximate algorithms must balance memory utilization with estimation accuracy.This balance can be controlled using a `compression` parameter:[source,console]--------------------------------------------------GET latency/_search{  "size": 0,  "aggs": {    "load_time_boxplot": {      "boxplot": {        "field": "load_time",        "compression": 200    <1>      }    }  }}--------------------------------------------------// TEST[setup:latency]<1> Compression controls memory usage and approximation errorinclude::percentile-aggregation.asciidoc[tags=t-digest]==== 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,console]--------------------------------------------------GET latency/_search{  "size": 0,  "aggs": {    "grade_boxplot": {      "boxplot": {        "field": "grade",        "missing": 10     <1>      }    }  }}--------------------------------------------------// TEST[setup:latency]<1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.
 |