Elasticsearch进阶笔记第十八篇

180 阅读2分钟

Elasticsearch高手进阶篇(36)

深入聚合数据分析_bucket嵌套实现颜色+品牌的多层下钻分析

下钻分析

从颜色到品牌进行下钻分析,每种颜色的平均价格,以及找到每种颜色每个品牌的平均价格

我们可以进行多层次的下钻

比如说,现在红色的电视有4台,同时这4台电视中,有3台是属于长虹的,1台是属于小米的

红色电视中的3台长虹的平均价格是多少? 红色电视中的1台小米的平均价格是多少?

下钻的意思是,已经分了一个组了,比如说颜色的分组,然后还要继续对这个分组内的数据,再分组,比如一个颜色内,还可以分成多个不同的品牌的组,最后对每个最小粒度的分组执行聚合分析操作,这就叫做下钻分析

  • es,下钻分析,就要对bucket进行多层嵌套,多次分组

    • 按照多个维度(颜色+品牌)多层下钻分析,而且学会了每个下钻维度(颜色,颜色+品牌),都可以对每个维度分别执行一次metric聚合操作
 GET /waws_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": 5,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "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
                 }
               }
             ]
           }
         }
       ]
     }
   }
 }

Elasticsearch高手进阶篇(37)

深入聚合数据分析_掌握更多metrics:统计每种颜色电视最大最小价格

更多metrics

  • count:bucket,terms,自动就会有一个doc_count,就相当于是count
  • avg:avg aggs,求平均值
  • max:求一个bucket内,指定field值最大的那个数据
  • min:求一个bucket内,指定field值最小的那个数据
  • sum:求一个bucket内,指定field值的总和

一般来说,90%的常见的数据分析的操作,metric,无非就是count,avg,max,min,sum

 GET /waws_tvs/sales/_search
 {
    "size" : 0,
    "aggs": {
       "colors": {
          "terms": {
             "field": "color"
          },
          "aggs": {
             "avg_price": {"avg": {"field": "price"}},
             "min_price" : {"min": {"field": "price"}}, 
             "max_price" : {"max": {"field": "price"}},
             "sum_price" : {"sum": {"field": "price"}} 
          }
       }
    }
 }

求总和,就可以拿到一个颜色下的所有电视的销售总额

 {
   "took": 4,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 8,
     "max_score": 0,
     "hits": []
   },
   "aggregations": {
     "colors": {
       "doc_count_error_upper_bound": 0,
       "sum_other_doc_count": 0,
       "buckets": [
         {
           "key": "红色",
           "doc_count": 4,
           "max_price": {
             "value": 8000
           },
           "min_price": {
             "value": 1000
           },
           "avg_price": {
             "value": 3250
           },
           "sum_price": {
             "value": 13000
           }
         },
         {
           "key": "绿色",
           "doc_count": 2,
           "max_price": {
             "value": 3000
           },
           "min_price": {
             "value": 1200
           },
           "avg_price": {
             "value": 2100
           },
           "sum_price": {
             "value": 4200
           }
         },
         {
           "key": "蓝色",
           "doc_count": 2,
           "max_price": {
             "value": 2500
           },
           "min_price": {
             "value": 1500
           },
           "avg_price": {
             "value": 2000
           },
           "sum_price": {
             "value": 4000
           }
         }
       ]
     }
   }
 }