min-aggregation.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. These
  8. values can be extracted either from specific numeric fields in the documents,
  9. or be generated by a provided script.
  10. NOTE: The `min` and `max` aggregation operate on the `double` representation of
  11. the data. As a consequence, the result may be approximate when running on longs
  12. whose absolute value is greater than +2^53+.
  13. Computing the min price value across all documents:
  14. [source,console]
  15. --------------------------------------------------
  16. POST /sales/_search?size=0
  17. {
  18. "aggs": {
  19. "min_price": { "min": { "field": "price" } }
  20. }
  21. }
  22. --------------------------------------------------
  23. // TEST[setup:sales]
  24. Response:
  25. [source,console-result]
  26. --------------------------------------------------
  27. {
  28. ...
  29. "aggregations": {
  30. "min_price": {
  31. "value": 10.0
  32. }
  33. }
  34. }
  35. --------------------------------------------------
  36. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  37. As can be seen, the name of the aggregation (`min_price` above) also serves as
  38. the key by which the aggregation result can be retrieved from the returned
  39. response.
  40. ==== Script
  41. The `min` aggregation can also calculate the minimum of a script. The example
  42. below computes the minimum price:
  43. [source,console]
  44. --------------------------------------------------
  45. POST /sales/_search
  46. {
  47. "aggs": {
  48. "min_price": {
  49. "min": {
  50. "script": {
  51. "source": "doc.price.value"
  52. }
  53. }
  54. }
  55. }
  56. }
  57. --------------------------------------------------
  58. // TEST[setup:sales]
  59. This will use the <<modules-scripting-painless, Painless>> scripting language
  60. and no script parameters. To use a stored script use the following syntax:
  61. [source,console]
  62. --------------------------------------------------
  63. POST /sales/_search
  64. {
  65. "aggs": {
  66. "min_price": {
  67. "min": {
  68. "script": {
  69. "id": "my_script",
  70. "params": {
  71. "field": "price"
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. --------------------------------------------------
  79. // TEST[setup:sales,stored_example_script]
  80. ==== Value Script
  81. Let's say that the prices of the documents in our index are in USD, but we
  82. would like to compute the min in EURO (and for the sake of this example, let's
  83. say the conversion rate is 1.2). We can use a value script to apply the
  84. conversion rate to every value before it is aggregated:
  85. [source,console]
  86. --------------------------------------------------
  87. POST /sales/_search
  88. {
  89. "aggs": {
  90. "min_price_in_euros": {
  91. "min": {
  92. "field": "price",
  93. "script": {
  94. "source": "_value * params.conversion_rate",
  95. "params": {
  96. "conversion_rate": 1.2
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. --------------------------------------------------
  104. // TEST[setup:sales]
  105. ==== Missing value
  106. The `missing` parameter defines how documents that are missing a value should
  107. be treated. By default they will be ignored but it is also possible to treat
  108. them as if they had a value.
  109. [source,console]
  110. --------------------------------------------------
  111. POST /sales/_search
  112. {
  113. "aggs": {
  114. "grade_min": {
  115. "min": {
  116. "field": "grade",
  117. "missing": 10 <1>
  118. }
  119. }
  120. }
  121. }
  122. --------------------------------------------------
  123. // TEST[setup:sales]
  124. <1> Documents without a value in the `grade` field will fall into the same
  125. bucket as documents that have the value `10`.
  126. [[search-aggregations-metrics-min-aggregation-histogram-fields]]
  127. ==== Histogram fields
  128. When `min` is computed on <<histogram,histogram fields>>, the result of the aggregation is the minimum
  129. of all elements in the `values` array. Note, that the `counts` array of the histogram is ignored.
  130. For example, for the following index that stores pre-aggregated histograms with latency metrics for different networks:
  131. [source,console]
  132. --------------------------------------------------
  133. PUT metrics_index/_doc/1
  134. {
  135. "network.name" : "net-1",
  136. "latency_histo" : {
  137. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  138. "counts" : [3, 7, 23, 12, 6] <2>
  139. }
  140. }
  141. PUT metrics_index/_doc/2
  142. {
  143. "network.name" : "net-2",
  144. "latency_histo" : {
  145. "values" : [0.1, 0.2, 0.3, 0.4, 0.5], <1>
  146. "counts" : [8, 17, 8, 7, 6] <2>
  147. }
  148. }
  149. POST /metrics_index/_search?size=0
  150. {
  151. "aggs" : {
  152. "min_latency" : { "min" : { "field" : "latency_histo" } }
  153. }
  154. }
  155. --------------------------------------------------
  156. The `min` aggregation will return the minimum value of all histogram fields:
  157. [source,console-result]
  158. --------------------------------------------------
  159. {
  160. ...
  161. "aggregations": {
  162. "min_latency": {
  163. "value": 0.1
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. // TESTRESPONSE[skip:test not setup]