数据准备:
PUT /shop { "settings": { "number_of_shards": 3, "number_of_replicas": 2 } } PUT /shop/_mapping/goods { "properties": { "brand": { "type": "keyword" }, "price": { "type": "float" }, "model": { "type": "keyword" } } } POST /shop/goods/_bulk {"index": {}} {"price" : 2299.00, "model" : "小米8", "brand" : "小米"} {"index": {}} {"price" : 4499.00, "model" : "Mate 20", "brand" : "华为"} {"index": {}} {"price" : 3299.00, "model" : "小米Mix3", "brand" : "小米"} {"index": {}} {"price" : 1199.00, "model" : "荣耀9i", "brand" : "华为"} {"index": {}} {"price" : 2799.00, "model" : "R17", "brand" : "OPPO"} {"index": {}} {"price" : 729.00, "model" : "红米6", "brand" : "小米"} {"index": {}} {"price" : 2799.00, "model" : "X23", "brand" : "VIVO"} {"index": {}} {"price" : 1799.00, "model" : "K1", "brand" : "OPPO"}
一、聚合为桶
按照手机的品牌brand划分为桶
查询指令:
GET /shop/_search { "size": 0, "aggs": { "brand_aggs": { "terms": { "field": "brand" } } } }
- size: 查询条数,这里设置为0,因为我们不关心搜索到的数据,只关心聚合结果,提高效率
- aggs:声明这是一个聚合查询,是aggregations的缩写
- popular_colors:给这次聚合起一个名字,任意。
- terms:划分桶的方式,这里是根据词条划分
- field:划分桶的字段
查询结果:
{ "took": 6, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "brand_aggs": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "小米", "doc_count": 3 }, { "key": "OPPO", "doc_count": 2 }, { "key": "华为", "doc_count": 2 }, { "key": "VIVO", "doc_count": 1 } ] } } }
- hits:查询结果为空,因为我们设置了size为0
- aggregations:聚合的结果
- brand_aggs:我们定义的聚合名称
- buckets:查找到的桶,每个不同的brand字段值都会形成一个桶
- key:这个桶对应的brand字段的值
- doc_count:这个桶中的文档数量
二、桶内度量
为聚合结果添加求价格平均值的度量
查询指令:
GET /shop/_search { "size": 0, "aggs": { "brand_aggs": { "terms": { "field": "brand" }, "aggs": { "price_aggs": { "avg": { "field": "price" } } } } } }
- aggs:我们在上一个aggs(brand_aggs)中添加新的aggs。可见度量也是一个聚合
- price_aggs:聚合的名称
- avg:度量的类型,这里是求平均值
- field:度量运算的字段
查询结果:
{ "took": 5, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 8, "max_score": 0, "hits": [] }, "aggregations": { "brand_aggs": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "小米", "doc_count": 3, "price_aggs": { "value": 2109 } }, { "key": "OPPO", "doc_count": 2, "price_aggs": { "value": 2299 } }, { "key": "华为", "doc_count": 2, "price_aggs": { "value": 2849 } }, { "key": "VIVO", "doc_count": 1, "price_aggs": { "value": 2799 } } ] } } }
可以看到每个桶中都有自己的 price_aggs 字段,这是度量聚合的结果