min-aggregation.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. [[search-aggregations-metrics-min-aggregation]]
  2. === Min aggregation
  3. ++++
  4. <titleabbrev>Min</titleabbrev>
  5. ++++
  6. A `single-value` metrics aggregation that keeps track and returns the minimum
  7. value among numeric values extracted from the aggregated documents.
  8. NOTE: The `min` and `max` aggregation operate on the `double` representation of
  9. the data. As a consequence, the result may be approximate when running on longs
  10. whose absolute value is greater than +2^53+.
  11. Computing the min price value across all documents:
  12. [source,console]
  13. --------------------------------------------------
  14. POST /sales/_search?size=0
  15. {
  16. "aggs": {
  17. "min_price": { "min": { "field": "price" } }
  18. }
  19. }
  20. --------------------------------------------------
  21. // TEST[setup:sales]
  22. Response:
  23. [source,console-result]
  24. --------------------------------------------------
  25. {
  26. ...
  27. "aggregations": {
  28. "min_price": {
  29. "value": 10.0
  30. }
  31. }
  32. }
  33. --------------------------------------------------
  34. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  35. As can be seen, the name of the aggregation (`min_price` above) also serves as
  36. the key by which the aggregation result can be retrieved from the returned
  37. response.
  38. ==== Script
  39. If you need to get the `min` of something more complex than a single field,
  40. run the aggregation on a <<runtime,runtime field>>.
  41. [source,console]
  42. ----
  43. POST /sales/_search
  44. {
  45. "size": 0,
  46. "runtime_mappings": {
  47. "price.adjusted": {
  48. "type": "double",
  49. "script": """
  50. double price = doc['price'].value;
  51. if (doc['promoted'].value) {
  52. price *= 0.8;
  53. }
  54. emit(price);
  55. """
  56. }
  57. },
  58. "aggs": {
  59. "min_price": {
  60. "min": { "field": "price.adjusted" }
  61. }
  62. }
  63. }
  64. ----
  65. // TEST[setup:sales]
  66. // TEST[s/_search/_search?filter_path=aggregations/]
  67. ////
  68. [source,console-result]
  69. --------------------------------------------------
  70. {
  71. "aggregations": {
  72. "min_price": {
  73. "value": 8.0
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. ////
  79. ==== Missing value
  80. The `missing` parameter defines how documents that are missing a value should
  81. be treated. By default they will be ignored but it is also possible to treat
  82. them as if they had a value.
  83. [source,console]
  84. --------------------------------------------------
  85. POST /sales/_search
  86. {
  87. "aggs": {
  88. "grade_min": {
  89. "min": {
  90. "field": "grade",
  91. "missing": 10 <1>
  92. }
  93. }
  94. }
  95. }
  96. --------------------------------------------------
  97. // TEST[setup:sales]
  98. <1> Documents without a value in the `grade` field will fall into the same
  99. bucket as documents that have the value `10`.
  100. [[search-aggregations-metrics-min-aggregation-histogram-fields]]
  101. ==== Histogram fields
  102. When `min` is computed on <<histogram,histogram fields>>, the result of the aggregation is the minimum
  103. of all elements in the `values` array. Note, that the `counts` array of the histogram is ignored.
  104. For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
  105. [source,console]
  106. --------------------------------------------------
  107. PUT metrics_index/_doc/1
  108. {
  109. "network.name" : "net-1",
  110. "latency_histo" : {
  111. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  112. "counts" : [3, 7, 23, 12, 6] <2>
  113. }
  114. }
  115. PUT metrics_index/_doc/2
  116. {
  117. "network.name" : "net-2",
  118. "latency_histo" : {
  119. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  120. "counts" : [8, 17, 8, 7, 6] <2>
  121. }
  122. }
  123. POST /metrics_index/_search?size=0
  124. {
  125. "aggs" : {
  126. "min_latency" : { "min" : { "field" : "latency_histo" } }
  127. }
  128. }
  129. --------------------------------------------------
  130. The `min` aggregation will return the minimum value of all histogram fields:
  131. [source,console-result]
  132. --------------------------------------------------
  133. {
  134. ...
  135. "aggregations": {
  136. "min_latency": {
  137. "value": 0.1
  138. }
  139. }
  140. }
  141. --------------------------------------------------
  142. // TESTRESPONSE[skip:test not setup]