min-aggregation.asciidoc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [[search-aggregations-metrics-min-aggregation]]
  2. === Min Aggregation
  3. A `single-value` metrics aggregation that keeps track and returns the minimum
  4. value among numeric values extracted from the aggregated documents. These
  5. values can be extracted either from specific numeric fields in the documents,
  6. or be generated by a provided script.
  7. NOTE: The `min` and `max` aggregation operate on the `double` representation of
  8. the data. As a consequence, the result may be approximate when running on longs
  9. whose absolute value is greater than +2^53+.
  10. Computing the min price value across all documents:
  11. [source,js]
  12. --------------------------------------------------
  13. POST /sales/_search?size=0
  14. {
  15. "aggs" : {
  16. "min_price" : { "min" : { "field" : "price" } }
  17. }
  18. }
  19. --------------------------------------------------
  20. // CONSOLE
  21. // TEST[setup:sales]
  22. Response:
  23. [source,js]
  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. The `min` aggregation can also calculate the maximum of a script. The example
  40. below computes the minimum price:
  41. [source,js]
  42. --------------------------------------------------
  43. POST /sales/_search
  44. {
  45. "aggs" : {
  46. "min_price" : {
  47. "min" : {
  48. "script" : {
  49. "inline" : "doc.price.value"
  50. }
  51. }
  52. }
  53. }
  54. }
  55. --------------------------------------------------
  56. // CONSOLE
  57. // TEST[setup:sales]
  58. This will use the <<modules-scripting-painless, Painless>> scripting language
  59. and no script parameters. To use a file script use the following syntax:
  60. [source,js]
  61. --------------------------------------------------
  62. POST /sales/_search
  63. {
  64. "aggs" : {
  65. "min_price" : {
  66. "min" : {
  67. "script" : {
  68. "file": "my_script",
  69. "params": {
  70. "field": "price"
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. // CONSOLE
  79. // TEST[setup:sales]
  80. TIP: For indexed scripts replace the `file` parameter with an `id` parameter.
  81. ==== Value Script
  82. Let's say that the prices of the documents in our index are in USD, but we
  83. would like to compute the min in EURO (and for the sake of this example, let's
  84. say the conversion rate is 1.2). We can use a value script to apply the
  85. conversion rate to every value before it is aggregated:
  86. [source,js]
  87. --------------------------------------------------
  88. POST /sales/_search
  89. {
  90. "aggs" : {
  91. "min_price_in_euros" : {
  92. "min" : {
  93. "field" : "price",
  94. "script" : {
  95. "inline" : "_value * params.conversion_rate",
  96. "params" : {
  97. "conversion_rate" : 1.2
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. --------------------------------------------------
  105. // CONSOLE
  106. // TEST[setup:sales]
  107. ==== Missing value
  108. The `missing` parameter defines how documents that are missing a value should
  109. be treated. By default they will be ignored but it is also possible to treat
  110. them as if they had a value.
  111. [source,js]
  112. --------------------------------------------------
  113. POST /sales/_search
  114. {
  115. "aggs" : {
  116. "grade_min" : {
  117. "min" : {
  118. "field" : "grade",
  119. "missing": 10 <1>
  120. }
  121. }
  122. }
  123. }
  124. --------------------------------------------------
  125. // CONSOLE
  126. // TEST[setup:sales]
  127. <1> Documents without a value in the `grade` field will fall into the same
  128. bucket as documents that have the value `10`.