bucket-sort-aggregation.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. [[search-aggregations-pipeline-bucket-sort-aggregation]]
  2. === Bucket Sort Aggregation
  3. A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation.
  4. Zero or more sort fields may be specified together with the corresponding sort order.
  5. Each bucket may be sorted based on its `_key`, `_count` or its sub-aggregations.
  6. In addition, parameters `from` and `size` may be set in order to truncate the result buckets.
  7. NOTE: The `bucket_sort` aggregation, like all pipeline aggregations, is executed after all other non-pipeline aggregations.
  8. This means the sorting only applies to whatever buckets are already returned from the parent aggregation. For example,
  9. if the parent aggregation is `terms` and its `size` is set to `10`, the `bucket_sort` will only sort over those 10
  10. returned term buckets.
  11. ==== Syntax
  12. A `bucket_sort` aggregation looks like this in isolation:
  13. [source,js]
  14. --------------------------------------------------
  15. {
  16. "bucket_sort": {
  17. "sort": [
  18. { "sort_field_1": { "order": "asc" } }, <1>
  19. { "sort_field_2": { "order": "desc" } },
  20. "sort_field_3"
  21. ],
  22. "from": 1,
  23. "size": 3
  24. }
  25. }
  26. --------------------------------------------------
  27. // NOTCONSOLE
  28. <1> Here, `sort_field_1` is the bucket path to the variable to be used as the primary sort and its order
  29. is ascending.
  30. [[bucket-sort-params]]
  31. .`bucket_sort` Parameters
  32. [options="header"]
  33. |===
  34. |Parameter Name |Description |Required |Default Value
  35. |`sort` |The list of fields to sort on. See <<sort-search-results,`sort`>> for more details. |Optional |
  36. |`from` |Buckets in positions prior to the set value will be truncated. |Optional | `0`
  37. |`size` |The number of buckets to return. Defaults to all buckets of the parent aggregation. |Optional |
  38. |`gap_policy` |The policy to apply when gaps are found in the data (see <<gap-policy>> for more
  39. details)|Optional |`skip`
  40. |===
  41. The following snippet returns the buckets corresponding to the 3 months with the highest total sales in descending order:
  42. [source,console]
  43. --------------------------------------------------
  44. POST /sales/_search
  45. {
  46. "size": 0,
  47. "aggs": {
  48. "sales_per_month": {
  49. "date_histogram": {
  50. "field": "date",
  51. "calendar_interval": "month"
  52. },
  53. "aggs": {
  54. "total_sales": {
  55. "sum": {
  56. "field": "price"
  57. }
  58. },
  59. "sales_bucket_sort": {
  60. "bucket_sort": {
  61. "sort": [
  62. { "total_sales": { "order": "desc" } } <1>
  63. ],
  64. "size": 3 <2>
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. --------------------------------------------------
  72. // TEST[setup:sales]
  73. <1> `sort` is set to use the values of `total_sales` in descending order
  74. <2> `size` is set to `3` meaning only the top 3 months in `total_sales` will be returned
  75. And the following may be the response:
  76. [source,console-result]
  77. --------------------------------------------------
  78. {
  79. "took": 82,
  80. "timed_out": false,
  81. "_shards": ...,
  82. "hits": ...,
  83. "aggregations": {
  84. "sales_per_month": {
  85. "buckets": [
  86. {
  87. "key_as_string": "2015/01/01 00:00:00",
  88. "key": 1420070400000,
  89. "doc_count": 3,
  90. "total_sales": {
  91. "value": 550.0
  92. }
  93. },
  94. {
  95. "key_as_string": "2015/03/01 00:00:00",
  96. "key": 1425168000000,
  97. "doc_count": 2,
  98. "total_sales": {
  99. "value": 375.0
  100. },
  101. },
  102. {
  103. "key_as_string": "2015/02/01 00:00:00",
  104. "key": 1422748800000,
  105. "doc_count": 2,
  106. "total_sales": {
  107. "value": 60.0
  108. },
  109. }
  110. ]
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // TESTRESPONSE[s/"took": 82/"took": $body.took/]
  116. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  117. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  118. ==== Truncating without sorting
  119. It is also possible to use this aggregation in order to truncate the result buckets
  120. without doing any sorting. To do so, just use the `from` and/or `size` parameters
  121. without specifying `sort`.
  122. The following example simply truncates the result so that only the second bucket is returned:
  123. [source,console]
  124. --------------------------------------------------
  125. POST /sales/_search
  126. {
  127. "size": 0,
  128. "aggs": {
  129. "sales_per_month": {
  130. "date_histogram": {
  131. "field": "date",
  132. "calendar_interval": "month"
  133. },
  134. "aggs": {
  135. "bucket_truncate": {
  136. "bucket_sort": {
  137. "from": 1,
  138. "size": 1
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. --------------------------------------------------
  146. // TEST[setup:sales]
  147. Response:
  148. [source,console-result]
  149. --------------------------------------------------
  150. {
  151. "took": 11,
  152. "timed_out": false,
  153. "_shards": ...,
  154. "hits": ...,
  155. "aggregations": {
  156. "sales_per_month": {
  157. "buckets": [
  158. {
  159. "key_as_string": "2015/02/01 00:00:00",
  160. "key": 1422748800000,
  161. "doc_count": 2
  162. }
  163. ]
  164. }
  165. }
  166. }
  167. --------------------------------------------------
  168. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  169. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  170. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]