白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高
生活随笔
收集整理的這篇文章主要介紹了
白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- Terms Aggregation官方文檔
- 案例一 : 統計哪種顏色電視銷量最高
- 模擬數據
- 統計哪種顏色的電視銷量最高
- size 參數 示例
- 外層size
- terms節點下的size
概述
繼續跟中華石杉老師學習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條數據。
. 不設置時,返回全部的聚合結果 。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的白话Elasticsearch32-深入聚合数据分析之案例实战Terms Aggs 统计哪种颜色电视销量最高的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch31-深入
- 下一篇: 白话Elasticsearch33-深入