• 聚合查询2.0


    一、三种聚合分类

    1、分桶聚合:把具有一类或者某些属性的事物放到一个桶中

     2、指标聚合:比较有代表性的指标作为聚合条件

     3、管道聚合:对聚合的结果二次聚合

      语法:buckets_path

    二、三种聚合演示

    1、桶聚合

     2、指标聚合

     

     1 #按照name去重的数量
     2 GET product/_search
     3 {
     4   "size": 0, 
     5   "aggs": {
     6     "name_count": {
     7       "cardinality": {
     8         "field": "name.keyword"
     9       }
    10     }
    11   }
    12 }

    3、管道聚合:二次聚合

     1 #统计平均价格最低的商品分类
     2 GET product/_search
     3 {
     4   "size": 0, 
     5   "aggs": {
     6     "type_bucket": {
     7       "terms": {
     8         "field": "type.keyword"        先分类
     9       },
    10       "aggs": {
    11         "price_bucket": {
    12           "avg": {
    13             "field": "price"           计算平均价格
    14           }
    15         }
    16       }
    17     },
    18     "min_bucket":{
    19       "min_bucket": 
    20         "buckets_path": "type_bucket>price_bucket"    第一个aggs下的name>平均价格下的name
    21       }
    22     }
    23   }
    24 }
     1 #统计每个商品类型中 不同档次分类商品中 平均价格最低的档次
     2 GET product/_search
     3 {
     4   "size": 0,
     5   "aggs": {
     6     "type_bucket": {                    商品类型
     7       "terms": {
     8         "field": "type.keyword"
     9       },
    10       "aggs": {
    11         "lv_bucket": {                  档次
    12           "terms": {
    13             "field": "lv.keyword"
    14           },
    15           "aggs": {
    16             "price_avg": {              平均价格
    17               "avg": {
    18                 "field": "price"
    19               }
    20             }
    21           }
    22         },
    23         "min_bucket":{                  平均价格最低的档次:档次>平均价格
    24           "min_bucket": {
    25             "buckets_path": "lv_bucket>price_avg"
    26           }
    27         }
    28       }
    29     }
    30   }
    31 }

     三、基于聚合结果的聚合

     1 #基于聚合结果的聚合
     2 GET product/_search
     3 {
     4   "size": 0,
     5   "aggs": {
     6     "<agg_name>": {
     7       "<agg_type>": {
     8         "field":"<field_name>"
     9       },
    10       "aggs": {
    11         "<agg_name_child>": {
    12           "<agg_type>": {
    13             "field":"<field_name>"
    14           }
    15         }
    16       }
    17     }
    18   }
    19 }

    四、基于查询结果的聚合

    基于查询结果的聚合:查询条件query和聚合条件aggs放在同一目录

     1 GET product/_search
     2 {
     3   "size": 0, 
     4   "query": {
     5     "range": {
     6       "price": {
     7         "gte": 4000
     8       }
     9     }
    10   },
    11   "aggs": {
    12     "tags_bucket": {
    13       "terms": {
    14         "field": "tags.keyword"
    15       }
    16     }
    17   }
    18 }

    五、基于聚合的查询

     1 #基于聚合的查询
     2 GET product/_search
     3 {
     4   "aggs": {
     5     "tags_bucket": {
     6       "terms": {
     7         "field": "tags.keyword"
     8       }
     9     }
    10   },
    11   "post_filter": {              使用post_filter后就会使得aggs聚合优先执行
    12     "term": {
    13       "tags.keyword": "性价比"
    14     }
    15   }
    16 }

    六、聚合排序

     1 #多级聚合
     2 GET product/_search
     3 {
     4   "size": 0, 
     5   "aggs": {
     6     "first_sort": {
     7       "terms": {
     8         "field": "type.keyword",
     9         "order": {
    10           "_count": "desc"
    11         }
    12       },
    13       "aggs": {
    14         "second_sort": {
    15           "terms": {
    16             "field": "lv.keyword",
    17             "order": {
    18               "_count": "asc"
    19             }
    20           }
    21         }
    22       }
    23     }
    24   }
    25 }

    七、常用聚合函数

    1、直方图

     1 GET product/_search?size=0
     2 {
     3   "aggs": {
     4     "price_histogram": {
     5       "histogram": {           间隔
     6         "field": "price",      需要间隔的字段      
     7         "interval": 1000,      间隔数据
     8         "keyed": true,         展示结果是数组还是键值对
     9         "min_doc_count": 1,    展示结果大于多少的数据
    10         "missing": 1999        将field字段为空的数据补充为对应的值
    11       }
    12     }
    13   }
    14 }

    2、时间直方图

     1 GET product/_search?size=0
     2 {
     3   "aggs": {
     4     "my_date_histogram": {
     5       "date_histogram": {
     6         "field": "createtime",
     7         "calendar_interval": "month",
     8         "min_doc_count": 0,               当存在extended_bounds时,此字段值必须为0
     9         "format": "yyyy-MM", 
    10         "extended_bounds": {              想显示的时间,当无数据时也需要展示
    11           "min": "2020-01",
    12           "max": "2020-12"
    13         }
    14       }
    15     }
    16   }
    17 }

    cumulative_sum可以做累加

    3、百分位统计

    percentile_rank

    作者:http://cnblogs.com/lyc-code/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权力。

  • 相关阅读:
    qt自定义的串口类判断断开
    ubuntu16.04永久修改有线接口名称(enp0s3->eth0)
    记录一下读过的书
    Qt 主界面卡死
    Mysql5.7及版本以上导入sql提示Incorrect date value: '0000-00-00' for column
    webpack打包css
    ant-design-vue中的a-directory-tree更换图标
    解决php-fpm占用内存过高问题
    centos,解压源代码安装,没有configure文件
    OSS存储上遇到The difference between the request time and the current time is too large
  • 原文地址:https://www.cnblogs.com/lyc-code/p/15240219.html
Copyright © 2020-2023  润新知