白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析
生活随笔
收集整理的這篇文章主要介紹了
白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 案例
- 需求
- 解決
- Step1.對每種顏色進行bucket分組
- Step2.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格
- Step3.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了
- Step4.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了。 最后對品牌進行metrics操作,即求每種品牌的平均價格,所以要在 brand 這個bucket中,再次aggs
概述
繼續(xù)跟中華石杉老師學(xué)習(xí)ES,第34篇
課程地址: https://www.roncoo.com/view/55
案例
原始數(shù)據(jù):
需求
在白話Elasticsearch33-深入聚合數(shù)據(jù)分析之案例實戰(zhàn)Terms Aggregation + Avg Aggregation ( bucket + metrics ) 中,我們演示了 對顏色進行bucket操作以后,再計算每種顏色的平均價格的metrics操作。
假設(shè) 又來了個新需求: 從顏色到品牌進行下鉆分析,每種顏色的平均價格,以及找到每種顏色每個品牌的平均價格
那就需要進行多層次的下鉆分析了
舉個例子:比如說,我們現(xiàn)在的索引中紅色的電視有4臺,同時這4臺電視中,有3臺是屬于長虹的,1臺是屬于小米的
那如何計算出 : 紅色電視中的3臺長虹的平均價格是多少? 紅色電視中的1臺小米的平均價格是多少?
解決
Step1.對每種顏色進行bucket分組
GET /tvs/sales/_search {"size": 0 ,"aggs": {"group_by_color": {"terms": {"field": "color"}}} }返回
Step2.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格
GET /tvs/sales/_search {"size": 0,"aggs": {"group_by_color": {"terms": {"field": "color"},"aggs": {"color_avg_price": {"avg": {"field": "price"}}}}} }返回:
Step3.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了
GET /tvs/sales/_search {"size": 0,"aggs": {"group_by_color": {"terms": {"field": "color"},"aggs": {"color_avg_price": {"avg": {"field": "price"}},"group_by_brand": {"terms": {"field": "brand"}}}}} }返回
{"took": 1,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 0,"hits": []},"aggregations": {"group_by_color": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "紅色","doc_count": 4,"color_avg_price": {"value": 3250},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "長虹","doc_count": 3},{"key": "三星","doc_count": 1}]}},{"key": "綠色","doc_count": 2,"color_avg_price": {"value": 2100},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1},{"key": "小米","doc_count": 1}]}},{"key": "藍色","doc_count": 2,"color_avg_price": {"value": 2000},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1},{"key": "小米","doc_count": 1}]}}]}} }Step4.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了。 最后對品牌進行metrics操作,即求每種品牌的平均價格,所以要在 brand 這個bucket中,再次aggs
GET /tvs/sales/_search {"size": 0 ,"aggs": {"group_by_color": {"terms": {"field": "color"},"aggs": {"color_avg_price": {"avg": {"field": "price"}},"group_by_brand":{"terms": {"field": "brand"},"aggs": {"brand_avg_price": {"avg": {"field": "price"}}}}}}} }到這里,就一步步完成了該需求,來看下返回結(jié)果吧
返回:
{"took": 4,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 8,"max_score": 0,"hits": []},"aggregations": {"group_by_color": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "紅色","doc_count": 4,"color_avg_price": {"value": 3250},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "長虹","doc_count": 3,"brand_avg_price": {"value": 1666.6666666666667}},{"key": "三星","doc_count": 1,"brand_avg_price": {"value": 8000}}]}},{"key": "綠色","doc_count": 2,"color_avg_price": {"value": 2100},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1,"brand_avg_price": {"value": 1200}},{"key": "小米","doc_count": 1,"brand_avg_price": {"value": 3000}}]}},{"key": "藍色","doc_count": 2,"color_avg_price": {"value": 2000},"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1,"brand_avg_price": {"value": 1500}},{"key": "小米","doc_count": 1,"brand_avg_price": {"value": 2500}}]}}]}} }校驗下
原始數(shù)據(jù):
我們通過ES算出來的數(shù)據(jù):
對比下原始數(shù)據(jù),符合預(yù)期,至此實現(xiàn)了該需求的DSL 。
總結(jié)
以上是生活随笔為你收集整理的白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch33-深入
- 下一篇: 白话Elasticsearch35-深入