ES 聚合排序
本文基于ES 聚合查询基础编写,测试数据的构建在ES 聚合查询中查找.
1、_key排序
按每个桶的键值数值排序
GET food/_search { "size": 0, "aggs": { "tags_agg": { "terms": { "field": "Tags.keyword", "size": 30,//查询多少条结果 "order": { "_key": "desc"//键值数值排序 } } } } }
搜索结果如下:
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "tags_agg" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "非常好吃", "doc_count" : 1 }, { "key" : "进口", "doc_count" : 1 }, { "key" : "超级贵", "doc_count" : 1 }, { "key" : "贵", "doc_count" : 1 }, { "key" : "营养价值高", "doc_count" : 1 }, { "key" : "营养", "doc_count" : 3 }, { "key" : "绿色蔬菜", "doc_count" : 2 }, { "key" : "白色蔬菜", "doc_count" : 1 }, { "key" : "水果", "doc_count" : 3 }, { "key" : "有点贵", "doc_count" : 1 }, { "key" : "易种植", "doc_count" : 1 }, { "key" : "性价比", "doc_count" : 2 }, { "key" : "好吃", "doc_count" : 1 }, { "key" : "国外", "doc_count" : 1 }, { "key" : "便宜", "doc_count" : 1 } ] } } }
2、_count排序
按照每个桶的数量进行排序
GET food/_search { "size": 0, "aggs": { "tags_agg": { "terms": { "field": "Tags.keyword", "size": 30,//查询多少条结果 "order": { "_count": "desc" //按照每个桶的数量降序排序 } } } } }
搜索结果如下:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "tags_agg" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "水果", "doc_count" : 3 }, { "key" : "营养", "doc_count" : 3 }, { "key" : "性价比", "doc_count" : 2 }, { "key" : "绿色蔬菜", "doc_count" : 2 }, { "key" : "便宜", "doc_count" : 1 }, { "key" : "国外", "doc_count" : 1 }, { "key" : "好吃", "doc_count" : 1 }, { "key" : "易种植", "doc_count" : 1 }, { "key" : "有点贵", "doc_count" : 1 }, { "key" : "白色蔬菜", "doc_count" : 1 }, { "key" : "营养价值高", "doc_count" : 1 }, { "key" : "贵", "doc_count" : 1 }, { "key" : "超级贵", "doc_count" : 1 }, { "key" : "进口", "doc_count" : 1 }, { "key" : "非常好吃", "doc_count" : 1 } ] } } }
3、多级聚合排序
现在需要对食物类型进行降序排序,每种食物类型按照食物品级进行降序排序,代码如下:
GET food/_search { "size": 0, "aggs": { "type_bucket": { "terms": { "field": "Type.keyword", "order": { "_count": "desc" } }, "aggs": { "level_bucket": { "terms": { "field": "Level.keyword", "order": { "_count": "desc" } } } } } } }
搜索结果如下:
{ "took" : 396, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 7, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "type_bucket" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "水果", "doc_count" : 4, "level_bucket" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "高级水果", "doc_count" : 3 }, { "key" : "普通水果", "doc_count" : 1 } ] } }, { "key" : "蔬菜", "doc_count" : 3, "level_bucket" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "普通蔬菜", "doc_count" : 2 }, { "key" : "中等蔬菜", "doc_count" : 1 } ] } } ] } } }
首先按照食物的类型进行降序排序,第一级的分桶确实是按照数量进行排序的,所以是对的,然后在食物类型的基础上分别统计食物的品级.可以各个类型的第二级分桶也是按照数量进行降序排序的,也是对的.
4、深层嵌套排序
现在需要统计各个食物类型的分桶数量,排序的依据是各个食物类型的食物的最大值进行降序排序
GET food/_search { "size": 0, "aggs": { "type_aggs": { "terms": { "field": "Type.keyword", "order": { "aggs_stats.max": "desc" } }, "aggs": { "aggs_stats":{ "stats": { "field": "Price" } } } } } }
搜索结果如下:
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 7, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "type_aggs" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "水果", "doc_count" : 4, "aggs_stats" : { "count" : 4, "min" : 11.109999656677246, "max" : 300.1099853515625, "avg" : 127.85999655723572, "sum" : 511.43998622894287 } }, { "key" : "蔬菜", "doc_count" : 3, "aggs_stats" : { "count" : 3, "min" : 11.109999656677246, "max" : 66.11000061035156, "avg" : 29.77666664123535, "sum" : 89.32999992370605 } } ] } } }
接下去在对价格进行过滤,然后在统计,代码如下:
GET food/_search { "size": 0, "aggs": { "type_aggs": { "terms": { "field": "Type.keyword", "order": { "price_aggs>stats_aggs.min": "asc" } }, "aggs": { "price_aggs":{ //过滤部分数据,不进行指标聚合,这里相当于没有操作 "filter": { "terms": { "Type.keyword": ["蔬菜","水果"] } }, "aggs": { "stats_aggs": { "stats": { "field": "Price" } } } } } } } }
搜索结果如下:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 7, "relation" : "eq" }, "max_score" : null, "hits" : [ ] }, "aggregations" : { "type_aggs" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "水果", "doc_count" : 4, "price_aggs" : { "doc_count" : 4, "stats_aggs" : { "count" : 4, "min" : 11.109999656677246, "max" : 300.1099853515625, "avg" : 127.85999655723572, "sum" : 511.43998622894287 } } }, { "key" : "蔬菜", "doc_count" : 3, "price_aggs" : { "doc_count" : 3, "stats_aggs" : { "count" : 3, "min" : 11.109999656677246, "max" : 66.11000061035156, "avg" : 29.77666664123535, "sum" : 89.32999992370605 } } } ] } } }