Elasticsearch 聚合操作
阿新 • • 發佈:2019-02-06
style arc keyword get number har mode ood nbsp
數據準備:
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 字段,這是度量聚合的結果
Elasticsearch 聚合操作