桶排序聚合
编辑桶排序聚合
编辑一个父管道聚合,对其父多桶聚合的桶进行排序。可以指定零个或多个排序字段以及相应的排序顺序。每个桶可以基于其 _key
、_count
或其子聚合进行排序。此外,可以设置参数 from
和 size
以截断结果桶。
像所有管道聚合一样,bucket_sort
聚合在所有其他非管道聚合之后执行。这意味着排序仅适用于从父聚合返回的任何桶。例如,如果父聚合是 terms
并且其 size
设置为 10
,则 bucket_sort
将仅对返回的 10 个 term 桶进行排序。
语法
编辑一个独立的 bucket_sort
聚合看起来像这样
{ "bucket_sort": { "sort": [ { "sort_field_1": { "order": "asc" } }, { "sort_field_2": { "order": "desc" } }, "sort_field_3" ], "from": 1, "size": 3 } }
以下代码片段按降序返回总销售额最高的 3 个月对应的桶
resp = client.search( index="sales", 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 } } } } }, ) print(resp)
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
const response = await client.search({ index: "sales", 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, }, }, }, }, }, }); console.log(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
。
以下示例仅截断结果,以便仅返回第二个桶
resp = client.search( index="sales", size=0, aggs={ "sales_per_month": { "date_histogram": { "field": "date", "calendar_interval": "month" }, "aggs": { "bucket_truncate": { "bucket_sort": { "from": 1, "size": 1 } } } } }, ) print(resp)
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
const response = await client.search({ index: "sales", size: 0, aggs: { sales_per_month: { date_histogram: { field: "date", calendar_interval: "month", }, aggs: { bucket_truncate: { bucket_sort: { from: 1, size: 1, }, }, }, }, }, }); console.log(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 } ] } } }