max-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. [[search-aggregations-metrics-max-aggregation]]
  2. === Max Aggregation
  3. A `single-value` metrics aggregation that keeps track and returns the maximum
  4. value among the 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 max price value across all documents
  11. [source,js]
  12. --------------------------------------------------
  13. POST /sales/_search?size=0
  14. {
  15. "aggs" : {
  16. "max_price" : { "max" : { "field" : "price" } }
  17. }
  18. }
  19. --------------------------------------------------
  20. // CONSOLE
  21. // TEST[setup:sales]
  22. Response:
  23. [source,js]
  24. --------------------------------------------------
  25. {
  26. ...
  27. "aggregations": {
  28. "max_price": {
  29. "value": 200.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 (`max_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 `max` aggregation can also calculate the maximum of a script. The example
  40. below computes the maximum price:
  41. [source,js]
  42. --------------------------------------------------
  43. POST /sales/_search
  44. {
  45. "aggs" : {
  46. "max_price" : {
  47. "max" : {
  48. "script" : {
  49. "source" : "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 stored script use the following syntax:
  60. [source,js]
  61. --------------------------------------------------
  62. POST /sales/_search
  63. {
  64. "aggs" : {
  65. "max_price" : {
  66. "max" : {
  67. "script" : {
  68. "id": "my_script",
  69. "params": {
  70. "field": "price"
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. --------------------------------------------------
  78. // CONSOLE
  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 max 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,js]
  86. --------------------------------------------------
  87. POST /sales/_search
  88. {
  89. "aggs" : {
  90. "max_price_in_euros" : {
  91. "max" : {
  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. // CONSOLE
  105. // TEST[setup:sales]
  106. ==== Missing value
  107. The `missing` parameter defines how documents that are missing a value should
  108. be treated. By default they will be ignored but it is also possible to treat
  109. them as if they had a value.
  110. [source,js]
  111. --------------------------------------------------
  112. POST /sales/_search
  113. {
  114. "aggs" : {
  115. "grade_max" : {
  116. "max" : {
  117. "field" : "grade",
  118. "missing": 10 <1>
  119. }
  120. }
  121. }
  122. }
  123. --------------------------------------------------
  124. // CONSOLE
  125. // TEST[setup:sales]
  126. <1> Documents without a value in the `grade` field will fall into the same
  127. bucket as documents that have the value `10`.