概述
继续跟中华石杉老师学习ES,第32篇
课程地址: https://www.roncoo.com/view/55
Terms Aggregation官方文档
官方文档: 戳这里
详细说明,参考官网即可,下面我们用示例来演示下
案例一 : 统计哪种颜色电视销量最高
模拟数据
创建索引
PUT /tvs { "mappings": { "sales": { "properties": { "price": { "type": "long" }, "color": { "type": "keyword" }, "brand": { "type": "keyword" }, "sold_date": { "type": "date" } } } } }
模拟一批数据
POST /tvs/sales/_bulk { "index": {}} { "price" : 1000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-10-28" } { "index": {}} { "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 3000, "color" : "绿色", "brand" : "小米", "sold_date" : "2016-05-18" } { "index": {}} { "price" : 1500, "color" : "蓝色", "brand" : "TCL", "sold_date" : "2016-07-02" } { "index": {}} { "price" : 1200, "color" : "绿色", "brand" : "TCL", "sold_date" : "2016-08-19" } { "index": {}} { "price" : 2000, "color" : "红色", "brand" : "长虹", "sold_date" : "2016-11-05" } { "index": {}} { "price" : 8000, "color" : "红色", "brand" : "三星", "sold_date" : "2017-01-01" } { "index": {}} { "price" : 2500, "color" : "蓝色", "brand" : "小米", "sold_date" : "2017-02-12" }
原始数据:
统计哪种颜色的电视销量最高
DSL
GET /tvs/sales/_search { "size": 0, "aggs": { "popular_colors": { "terms": { "field": "color" } } } }
解读:
size:只获取聚合结果,而不要执行聚合的原始数据
aggs:固定语法,要对一份数据执行分组聚合操作
popular_colors:就是对每个aggs,都要起一个名字,自定义,叫啥都行
terms:根据字段的值进行分组
field:根据指定的字段的值进行分组
类比官方介绍
返回结果:
hits.hits:我们指定了size是0,所以hits.hits就是空的,否则会把执行聚合的那些原始数据给你返回回来
aggregations:聚合结果
popular_color:我们指定的某个聚合的名称
buckets:根据我们指定的field划分出的buckets
key:每个bucket对应的那个值
doc_count:这个bucket分组内,有多少个数据
类比官网说明
每种颜色对应的bucket中的数据的数量,其实就是这种颜色的销量
默认的排序规则:按照doc_count降序排序
size 参数 示例
外层size
当我们 外层不加size的时候,会返回执行聚合的那些原始数据
GET /tvs/sales/_search { "aggs": { "popular_color": { "terms": { "field": "color" } } } }
返回
{ "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 1, "hits": [ { "_index": "tvs", "_type": "sales", "_id": "QzGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 2000, "color": "红色", "brand": "长虹", "sold_date": "2016-11-05" } }, { "_index": "tvs", "_type": "sales", "_id": "PzGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 2000, "color": "红色", "brand": "长虹", "sold_date": "2016-11-05" } }, { "_index": "tvs", "_type": "sales", "_id": "QDGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 3000, "color": "绿色", "brand": "小米", "sold_date": "2016-05-18" } }, { "_index": "tvs", "_type": "sales", "_id": "QjGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 1200, "color": "绿色", "brand": "TCL", "sold_date": "2016-08-19" } }, { "_index": "tvs", "_type": "sales", "_id": "RDGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 8000, "color": "红色", "brand": "三星", "sold_date": "2017-01-01" } }, { "_index": "tvs", "_type": "sales", "_id": "PjGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 1000, "color": "红色", "brand": "长虹", "sold_date": "2016-10-28" } }, { "_index": "tvs", "_type": "sales", "_id": "QTGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 1500, "color": "蓝色", "brand": "TCL", "sold_date": "2016-07-02" } }, { "_index": "tvs", "_type": "sales", "_id": "RTGrtGwBCp8vhw_gCmb9", "_score": 1, "_source": { "price": 2500, "color": "蓝色", "brand": "小米", "sold_date": "2017-02-12" } } ] }, "aggregations": { "popular_color": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "红色", "doc_count": 4 }, { "key": "绿色", "doc_count": 2 }, { "key": "蓝色", "doc_count": 2 } ] } } }
当把外层的 size设置为1 ,返回1条执行聚合的那些原始数据
设置为0 ,不返回执行聚合的那些原始数据
terms节点下的size
返回了bucket 中1条数据。
. 不设置时,返回全部的聚合结果 。