min-bucket-aggregation.asciidoc 3.3 KB

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