白话Elasticsearch44-深入聚合数据分析之案例实战_颜色+品牌下钻分析时按最深层metric进行排序
生活随笔
收集整理的這篇文章主要介紹了
白话Elasticsearch44-深入聚合数据分析之案例实战_颜色+品牌下钻分析时按最深层metric进行排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 案例
概述
繼續跟中華石杉老師學習ES,第44篇
課程地址: https://www.roncoo.com/view/55
案例
需求: 顏色+品牌下鉆分析時按最深層metric進行排序
先看下默認的排序規則:
返回:
{"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,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "長虹","doc_count": 3,"avg_price": {"value": 1666.6666666666667}},{"key": "三星","doc_count": 1,"avg_price": {"value": 8000}}]}},{"key": "綠色","doc_count": 2,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1,"avg_price": {"value": 1200}},{"key": "小米","doc_count": 1,"avg_price": {"value": 3000}}]}},{"key": "藍色","doc_count": 2,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "TCL","doc_count": 1,"avg_price": {"value": 1500}},{"key": "小米","doc_count": 1,"avg_price": {"value": 2500}}]}}]}} }截取下Red 來看下默認的排序規則 (doc_count 降序)
如果想按照平均價格 降序排列呢? 我們這里有2層下鉆,而平均價格是在第二層下鉆里 ,即我們這里的主題【按最深層metric進行排序】
GET /tvs/sales/_search {"aggs": {"group_by_color": {"terms": {"field": "color"},"aggs": {"group_by_brand": {"terms": {"field": "brand","order": {"avg_price": "desc"}},"aggs": {"avg_price": {"avg": {"field": "price"}}}}}}},"size": 0 }返回結果:
{"took": 2,"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,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "三星","doc_count": 1,"avg_price": {"value": 8000}},{"key": "長虹","doc_count": 3,"avg_price": {"value": 1666.6666666666667}}]}},{"key": "綠色","doc_count": 2,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "小米","doc_count": 1,"avg_price": {"value": 3000}},{"key": "TCL","doc_count": 1,"avg_price": {"value": 1200}}]}},{"key": "藍色","doc_count": 2,"group_by_brand": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "小米","doc_count": 1,"avg_price": {"value": 2500}},{"key": "TCL","doc_count": 1,"avg_price": {"value": 1500}}]}}]}} }同樣的,我們也截取下Red 來看下默認的排序規則 (doc_count 降序)
已經按照我們指定的 avg_price 降序排列啦。
總結
以上是生活随笔為你收集整理的白话Elasticsearch44-深入聚合数据分析之案例实战_颜色+品牌下钻分析时按最深层metric进行排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch43-深入
- 下一篇: 白话Elasticsearch45-深入