Elasticsearch进阶笔记第二十二篇

116 阅读1分钟

Elasticsearch高手进阶篇(45)

深入聚合数据分析_排序:按每种颜色的平均销售额降序排序

之前的话,排序,是按照每个bucket的doc_count降序来排的

但是假如说,我们现在统计出来每个颜色的电视的销售额,需要按照销售额降序排序

  • 未进行排序的结果
 {
   "took": 1,
   "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,
           "avg_price": {
             "value": 3250
           }
         },
         {
           "key": "绿色",
           "doc_count": 2,
           "avg_price": {
             "value": 2100
           }
         },
         {
           "key": "蓝色",
           "doc_count": 2,
           "avg_price": {
             "value": 2000
           }
         }
       ]
     }
   }
 }
 ​
 {
   "took": 1,
   "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,
           "avg_price": {
             "value": 3250
           }
         },
         {
           "key": "绿色",
           "doc_count": 2,
           "avg_price": {
             "value": 2100
           }
         },
         {
           "key": "蓝色",
           "doc_count": 2,
           "avg_price": {
             "value": 2000
           }
         }
       ]
     }
   }
 }
  • 进行排序的结果
 GET /waws_tvs/sales/_search 
 {
   "size": 0,
   "aggs": {
     "group_by_color": {
       "terms": {
         "field": "color",
         "order": {
           "avg_price": "asc"
         }
       },
       "aggs": {
         "avg_price": {
           "avg": {
             "field": "price"
           }
         }
       }
     }
   }
 }
 
 {
   "took": 2,
   "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": 2,
           "avg_price": {
             "value": 2000
           }
         },
         {
           "key": "绿色",
           "doc_count": 2,
           "avg_price": {
             "value": 2100
           }
         },
         {
           "key": "红色",
           "doc_count": 4,
           "avg_price": {
             "value": 3250
           }
         }
       ]
     }
   }
 }

Elasticsearch高手进阶篇(46)

深入聚合数据分析_颜色+品牌下钻分析时按最深层metric进行排序

 GET /waws_tvs/sales/_search 
 {
   "size": 0,
   "aggs": {
     "group_by_color": {
       "terms": {
         "field": "color"
       },
       "aggs": {
         "group_by_brand": {
           "terms": {
             "field": "brand",
             "order": {
               "avg_price": "desc"
             }
           },
           "aggs": {
             "avg_price": {
               "avg": {
                 "field": "price"
               }
             }
           }
         }
       }
     }
   }
 }
 ​
 {
   "took": 2,
   "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,
           "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
                 }
               }
             ]
           }
         }
       ]
     }
   }
 }