min-bucket-aggregation.asciidoc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. [[search-aggregations-pipeline-min-bucket-aggregation]]
  2. === Min bucket aggregation
  3. ++++
  4. <titleabbrev>Min bucket</titleabbrev>
  5. ++++
  6. A sibling pipeline aggregation which identifies the bucket(s) with the minimum value of a specified metric in a sibling aggregation
  7. and outputs both the value and the key(s) of the bucket(s). The specified metric must be numeric and the sibling aggregation must
  8. be a multi-bucket aggregation.
  9. ==== Syntax
  10. A `min_bucket` aggregation looks like this in isolation:
  11. [source,js]
  12. --------------------------------------------------
  13. {
  14. "min_bucket": {
  15. "buckets_path": "the_sum"
  16. }
  17. }
  18. --------------------------------------------------
  19. // NOTCONSOLE
  20. [[min-bucket-params]]
  21. .`min_bucket` Parameters
  22. [options="header"]
  23. |===
  24. |Parameter Name |Description |Required |Default Value
  25. |`buckets_path` |The path to the buckets we wish to find the minimum for (see <<buckets-path-syntax>> for more
  26. details) |Required |
  27. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  28. details)|Optional | `skip`
  29. |`format` |format to apply to the output value of this aggregation |Optional |`null`
  30. |===
  31. The following snippet calculates the minimum of the total monthly `sales`:
  32. [source,console]
  33. --------------------------------------------------
  34. POST /sales/_search
  35. {
  36. "size": 0,
  37. "aggs": {
  38. "sales_per_month": {
  39. "date_histogram": {
  40. "field": "date",
  41. "calendar_interval": "month"
  42. },
  43. "aggs": {
  44. "sales": {
  45. "sum": {
  46. "field": "price"
  47. }
  48. }
  49. }
  50. },
  51. "min_monthly_sales": {
  52. "min_bucket": {
  53. "buckets_path": "sales_per_month>sales" <1>
  54. }
  55. }
  56. }
  57. }
  58. --------------------------------------------------
  59. // TEST[setup:sales]
  60. <1> `buckets_path` instructs this min_bucket aggregation that we want the minimum value of the `sales` aggregation in the
  61. `sales_per_month` date histogram.
  62. And the following may be the response:
  63. [source,console-result]
  64. --------------------------------------------------
  65. {
  66. "took": 11,
  67. "timed_out": false,
  68. "_shards": ...,
  69. "hits": ...,
  70. "aggregations": {
  71. "sales_per_month": {
  72. "buckets": [
  73. {
  74. "key_as_string": "2015/01/01 00:00:00",
  75. "key": 1420070400000,
  76. "doc_count": 3,
  77. "sales": {
  78. "value": 550.0
  79. }
  80. },
  81. {
  82. "key_as_string": "2015/02/01 00:00:00",
  83. "key": 1422748800000,
  84. "doc_count": 2,
  85. "sales": {
  86. "value": 60.0
  87. }
  88. },
  89. {
  90. "key_as_string": "2015/03/01 00:00:00",
  91. "key": 1425168000000,
  92. "doc_count": 2,
  93. "sales": {
  94. "value": 375.0
  95. }
  96. }
  97. ]
  98. },
  99. "min_monthly_sales": {
  100. "keys": ["2015/02/01 00:00:00"], <1>
  101. "value": 60.0
  102. }
  103. }
  104. }
  105. --------------------------------------------------
  106. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  107. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  108. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  109. <1> `keys` is an array of strings since the minimum value may be present in multiple buckets