boxplot-aggregation.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. [role="xpack"]
  2. [[search-aggregations-metrics-boxplot-aggregation]]
  3. === Boxplot aggregation
  4. ++++
  5. <titleabbrev>Boxplot</titleabbrev>
  6. ++++
  7. A `boxplot` metrics aggregation that computes boxplot of numeric values extracted from the aggregated documents.
  8. These values can be generated from specific numeric or <<histogram,histogram fields>> in the documents.
  9. The `boxplot` aggregation returns essential information for making a {wikipedia}/Box_plot[box plot]: minimum, maximum,
  10. median, first quartile (25th percentile) and third quartile (75th percentile) values.
  11. ==== Syntax
  12. A `boxplot` aggregation looks like this in isolation:
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "boxplot": {
  17. "field": "load_time"
  18. }
  19. }
  20. --------------------------------------------------
  21. // NOTCONSOLE
  22. Let's look at a boxplot representing load time:
  23. [source,console]
  24. --------------------------------------------------
  25. GET latency/_search
  26. {
  27. "size": 0,
  28. "aggs": {
  29. "load_time_boxplot": {
  30. "boxplot": {
  31. "field": "load_time" <1>
  32. }
  33. }
  34. }
  35. }
  36. --------------------------------------------------
  37. // TEST[setup:latency]
  38. <1> The field `load_time` must be a numeric field
  39. The response will look like this:
  40. [source,console-result]
  41. --------------------------------------------------
  42. {
  43. ...
  44. "aggregations": {
  45. "load_time_boxplot": {
  46. "min": 0.0,
  47. "max": 990.0,
  48. "q1": 165.0,
  49. "q2": 445.0,
  50. "q3": 725.0,
  51. "lower": 0.0,
  52. "upper": 990.0
  53. }
  54. }
  55. }
  56. --------------------------------------------------
  57. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  58. In this case, the lower and upper whisker values are equal to the min and max. In general, these values are the 1.5 *
  59. 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 values
  60. may not actually be observed values from the data, but should be within a reasonable error bound of them. While the Boxplot aggregation
  61. doesn't directly return outlier points, you can check if `lower > min` or `upper < max` to see if outliers exist on either side, and then
  62. query for them directly.
  63. ==== Script
  64. If you need to create a boxplot for values that aren't indexed exactly you
  65. should create a <<runtime,runtime field>> and get the boxplot of that. For
  66. example, if your load times are in milliseconds but you want values calculated
  67. in seconds, use a runtime field to convert them:
  68. [source,console]
  69. ----
  70. GET latency/_search
  71. {
  72. "size": 0,
  73. "runtime_mappings": {
  74. "load_time.seconds": {
  75. "type": "long",
  76. "script": {
  77. "source": "emit(doc['load_time'].value / params.timeUnit)",
  78. "params": {
  79. "timeUnit": 1000
  80. }
  81. }
  82. }
  83. },
  84. "aggs": {
  85. "load_time_boxplot": {
  86. "boxplot": { "field": "load_time.seconds" }
  87. }
  88. }
  89. }
  90. ----
  91. // TEST[setup:latency]
  92. // TEST[s/_search/_search?filter_path=aggregations/]
  93. // TEST[s/"timeUnit": 1000/"timeUnit": 10/]
  94. ////
  95. [source,console-result]
  96. --------------------------------------------------
  97. {
  98. "aggregations": {
  99. "load_time_boxplot": {
  100. "min": 0.0,
  101. "max": 99.0,
  102. "q1": 16.5,
  103. "q2": 44.5,
  104. "q3": 72.5,
  105. "lower": 0.0,
  106. "upper": 99.0
  107. }
  108. }
  109. }
  110. --------------------------------------------------
  111. ////
  112. [[search-aggregations-metrics-boxplot-aggregation-approximation]]
  113. ==== Boxplot values are (usually) approximate
  114. The algorithm used by the `boxplot` metric is called TDigest (introduced by
  115. Ted Dunning in
  116. https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf[Computing Accurate Quantiles using T-Digests]).
  117. [WARNING]
  118. ====
  119. Boxplot as other percentile aggregations are also
  120. {wikipedia}/Nondeterministic_algorithm[non-deterministic].
  121. This means you can get slightly different results using the same data.
  122. ====
  123. [[search-aggregations-metrics-boxplot-aggregation-compression]]
  124. ==== Compression
  125. Approximate algorithms must balance memory utilization with estimation accuracy.
  126. This balance can be controlled using a `compression` parameter:
  127. [source,console]
  128. --------------------------------------------------
  129. GET latency/_search
  130. {
  131. "size": 0,
  132. "aggs": {
  133. "load_time_boxplot": {
  134. "boxplot": {
  135. "field": "load_time",
  136. "compression": 200 <1>
  137. }
  138. }
  139. }
  140. }
  141. --------------------------------------------------
  142. // TEST[setup:latency]
  143. <1> Compression controls memory usage and approximation error
  144. include::percentile-aggregation.asciidoc[tags=t-digest]
  145. ==== Missing value
  146. The `missing` parameter defines how documents that are missing a value should be treated.
  147. By default they will be ignored but it is also possible to treat them as if they
  148. had a value.
  149. [source,console]
  150. --------------------------------------------------
  151. GET latency/_search
  152. {
  153. "size": 0,
  154. "aggs": {
  155. "grade_boxplot": {
  156. "boxplot": {
  157. "field": "grade",
  158. "missing": 10 <1>
  159. }
  160. }
  161. }
  162. }
  163. --------------------------------------------------
  164. // TEST[setup:latency]
  165. <1> Documents without a value in the `grade` field will fall into the same bucket as documents that have the value `10`.