桶排序聚合
编辑桶排序聚合编辑
一个父管道聚合,它对父多桶聚合的桶进行排序。可以指定一个或多个排序字段以及相应的排序顺序。每个桶可以根据其 _key
、_count
或其子聚合进行排序。此外,还可以设置参数 from
和 size
来截断结果桶。
bucket_sort
聚合与所有管道聚合一样,在所有其他非管道聚合执行后执行。这意味着排序仅适用于从父聚合返回的任何桶。例如,如果父聚合是 terms
并且其 size
设置为 10
,则 bucket_sort
将只对这 10 个返回的术语桶进行排序。
语法编辑
bucket_sort
聚合在孤立状态下看起来像这样
{ "bucket_sort": { "sort": [ { "sort_field_1": { "order": "asc" } }, { "sort_field_2": { "order": "desc" } }, "sort_field_3" ], "from": 1, "size": 3 } }
以下代码段返回与总销售额最高的 3 个月相对应的桶,按降序排列
response = client.search( index: 'sales', body: { size: 0, aggregations: { sales_per_month: { date_histogram: { field: 'date', calendar_interval: 'month' }, aggregations: { total_sales: { sum: { field: 'price' } }, sales_bucket_sort: { bucket_sort: { sort: [ { total_sales: { order: 'desc' } } ], size: 3 } } } } } } ) puts response
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "total_sales": { "sum": { "field": "price" } }, "sales_bucket_sort": { "bucket_sort": { "sort": [ { "total_sales": { "order": "desc" } } ], "size": 3 } } } } } }
以下可能是响应
{ "took": 82, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "sales_per_month": { "buckets": [ { "key_as_string": "2015/01/01 00:00:00", "key": 1420070400000, "doc_count": 3, "total_sales": { "value": 550.0 } }, { "key_as_string": "2015/03/01 00:00:00", "key": 1425168000000, "doc_count": 2, "total_sales": { "value": 375.0 } }, { "key_as_string": "2015/02/01 00:00:00", "key": 1422748800000, "doc_count": 2, "total_sales": { "value": 60.0 } } ] } } }
不排序就截断编辑
也可以使用此聚合来截断结果桶,而不进行任何排序。为此,只需使用 from
和/或 size
参数,而无需指定 sort
。
以下示例只是截断结果,以便只返回第二个桶
response = client.search( index: 'sales', body: { size: 0, aggregations: { sales_per_month: { date_histogram: { field: 'date', calendar_interval: 'month' }, aggregations: { bucket_truncate: { bucket_sort: { from: 1, size: 1 } } } } } } ) puts response
POST /sales/_search { "size": 0, "aggs": { "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "bucket_truncate": { "bucket_sort": { "from": 1, "size": 1 } } } } } }
响应
{ "took": 11, "timed_out": false, "_shards": ..., "hits": ..., "aggregations": { "sales_per_month": { "buckets": [ { "key_as_string": "2015/02/01 00:00:00", "key": 1422748800000, "doc_count": 2 } ] } } }