max-aggregation.asciidoc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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,console]
  12. --------------------------------------------------
  13. POST /sales/_search?size=0
  14. {
  15. "aggs" : {
  16. "max_price" : { "max" : { "field" : "price" } }
  17. }
  18. }
  19. --------------------------------------------------
  20. // TEST[setup:sales]
  21. Response:
  22. [source,console-result]
  23. --------------------------------------------------
  24. {
  25. ...
  26. "aggregations": {
  27. "max_price": {
  28. "value": 200.0
  29. }
  30. }
  31. }
  32. --------------------------------------------------
  33. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  34. As can be seen, the name of the aggregation (`max_price` above) also serves as
  35. the key by which the aggregation result can be retrieved from the returned
  36. response.
  37. ==== Script
  38. The `max` aggregation can also calculate the maximum of a script. The example
  39. below computes the maximum price:
  40. [source,console]
  41. --------------------------------------------------
  42. POST /sales/_search
  43. {
  44. "aggs" : {
  45. "max_price" : {
  46. "max" : {
  47. "script" : {
  48. "source" : "doc.price.value"
  49. }
  50. }
  51. }
  52. }
  53. }
  54. --------------------------------------------------
  55. // TEST[setup:sales]
  56. This will use the <<modules-scripting-painless, Painless>> scripting language
  57. and no script parameters. To use a stored script use the following syntax:
  58. [source,console]
  59. --------------------------------------------------
  60. POST /sales/_search
  61. {
  62. "aggs" : {
  63. "max_price" : {
  64. "max" : {
  65. "script" : {
  66. "id": "my_script",
  67. "params": {
  68. "field": "price"
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. --------------------------------------------------
  76. // TEST[setup:sales,stored_example_script]
  77. ==== Value Script
  78. Let's say that the prices of the documents in our index are in USD, but we
  79. would like to compute the max in EURO (and for the sake of this example, let's
  80. say the conversion rate is 1.2). We can use a value script to apply the
  81. conversion rate to every value before it is aggregated:
  82. [source,console]
  83. --------------------------------------------------
  84. POST /sales/_search
  85. {
  86. "aggs" : {
  87. "max_price_in_euros" : {
  88. "max" : {
  89. "field" : "price",
  90. "script" : {
  91. "source" : "_value * params.conversion_rate",
  92. "params" : {
  93. "conversion_rate" : 1.2
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. --------------------------------------------------
  101. // TEST[setup:sales]
  102. ==== Missing value
  103. The `missing` parameter defines how documents that are missing a value should
  104. be treated. By default they will be ignored but it is also possible to treat
  105. them as if they had a value.
  106. [source,console]
  107. --------------------------------------------------
  108. POST /sales/_search
  109. {
  110. "aggs" : {
  111. "grade_max" : {
  112. "max" : {
  113. "field" : "grade",
  114. "missing": 10 <1>
  115. }
  116. }
  117. }
  118. }
  119. --------------------------------------------------
  120. // TEST[setup:sales]
  121. <1> Documents without a value in the `grade` field will fall into the same
  122. bucket as documents that have the value `10`.