日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析

發布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 案例
    • 需求
    • 解決
      • Step1.對每種顏色進行bucket分組
      • Step2.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格
      • Step3.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了
      • Step4.對每種顏色進行bucket分組 , 然后對每個分組再次計算平均價格 , 緊接再對每種顏色按照brand分組,直接寫到和 color_avg_price 并列的地方就可以了。 最后對品牌進行metrics操作,即求每種品牌的平均價格,所以要在 brand 這個bucket中,再次aggs


概述

繼續跟中華石杉老師學習ES,第34篇

課程地址: https://www.roncoo.com/view/55


案例

原始數據:


需求

在白話Elasticsearch33-深入聚合數據分析之案例實戰Terms Aggregation + Avg Aggregation ( bucket + metrics ) 中,我們演示了 對顏色進行bucket操作以后,再計算每種顏色的平均價格的metrics操作。

假設 又來了個新需求: 從顏色到品牌進行下鉆分析,每種顏色的平均價格,以及找到每種顏色每個品牌的平均價格

那就需要進行多層次的下鉆分析

舉個例子:比如說,我們現在的索引中紅色的電視有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"}}}}}}} }

到這里,就一步步完成了該需求,來看下返回結果吧

返回:

{"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}}]}}]}} }

校驗下

原始數據:

我們通過ES算出來的數據:

對比下原始數據,符合預期,至此實現了該需求的DSL 。

總結

以上是生活随笔為你收集整理的白话Elasticsearch34-深入聚合数据分析之案例实战bucket嵌套实现颜色+品牌的多层下钻分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。