bucket-sort-aggregation.asciidoc 5.4 KB

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