• ElasticSearch聚合查询


    1. 数据准备
    索引创建
    PUT product
    {
      "mappings": {
        "properties": {
          "createtime": {
            "type": "date"
          },
          "desc": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            },
            "analyzer": "ik_max_word"
          },
          "lv": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "analyzer": "ik_max_word",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "pice": {
            "type": "long"
          },
          "tags": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "type": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
    数据插入
    PUT  /product/_doc/1
    {
      "name":"小米手机",
      "desc":"手机中的战斗机",
      "pice":3999,
      "lv":"旗舰机",
      "type":"手机",
      "createtime":"2020-10-01",
      "tags":["性价比","发烧","不卡顿"]
    }
    
    PUT  /product/_doc/2
    {
      "name":"小米NFC手机",
      "desc":"支持全功能NFC,手机中的滑翔机",
      "pice":4999,
      "lv":"旗舰机",
      "type":"手机",
      "createtime":"2020-05-21",
      "tags":["性价比","发烧","公交卡"]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    分组查询

    # 不同标签商品数量(按照结果数量降序),和不同类型的商品数量
    GET /product/_search
    {
      "size": 0, 
      "aggs": {
        "tags_group": {
          "terms": {
            "field": "tags.keyword",
            "order": {
              "_count": "desc"
            }
          }
        },
        "type_group": {
          "terms": {
            "field": "type.keyword"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    指标查询

    查询pice的最大值和平均值、以及所有指标聚合值
    {
      "size": 0,
      "aggs": {
        "pice_avg": {
          "avg": {
            "field": "pice"
          }
        },
        "max_pice": {
          "max": {
            "field": "pice"
          }
        },
        "stats_pice": {
          "stats": {
            "field": "pice"
          }
        }
      }
    }
    根据name去重
    {
      "size": 0, 
      "aggs": {
        "name_count": {
          "cardinality": {
            "field": "name.keyword"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    管道聚合

    # 平均价格最低的商品分类
    GET /product/_search
    {
      "size": 0,
      "aggs": {
        "type_group": {
          "terms": {
            "field": "type.keyword"
          },
          "aggs": {
            "avg_pice": {
              "avg": {
                "field": "pice"
              }
            }
          }
        },
        "min_baucket":{
          "min_bucket": {
            "buckets_path": "type_group>avg_pice"
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    基于查询结果的聚合

    统计电视的平均价格
    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "type.keyword": {
                  "value": "电视"
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "tags_agg": {
          "avg": {
            "field": "pice"
          }
        }
      }
    }
    
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "type.keyword": {
                  "value": "电视"
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "tags_agg": {
          "avg": {
            "field": "pice"
          }
        }
      }
    }
     针对聚合后的结果做过滤
    {
      "aggs": {
        "tags_agg": {
          "terms": {
            "field": "tags.keyword"
          }
        }
      },
      "post_filter": {
        "term": {
          "tags.keyword": "性价比"
        }
      }
    }
    
    # 价格大于三千的 价格最小值,平均值 ,所有数据的平均值
    GET /product/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "pice": {
                  "gte": 3000
                }
              }
            }
          ]
        }
      },
      "size": 0,
      "aggs": {
        "min_pice": {
          "min": {
            "field": "pice"
          }
        },
        "avg_pice": {
          "avg": {
            "field": "pice"
          }
        },
        "all_avg_pic": {
          "global": {}, //取消了外层的条件过滤
          "aggs": {
            "avg_pic": {
              "avg": {
                "field": "pice"
              }
            }
          }
        },
        "muti_avg_pic": {
          "filter": {  // 结合外层条件取交集
            "range": {
              "pice": {
                "gte": 4000
              }
            }
          },
          "aggs": {
            "avg_pic": {
              "avg": {
                "field": "pice"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    聚合排序

    过滤出手机耳机 再根据类型分组,计算各统计聚合值(平均,最大,最小),最好喝根据最小值排序
    {
      "size": 0,
      "query": {
        "bool": {
          "filter": {
            "terms": {
              "type.keyword": [
                "手机",
                "耳机"
              ]
            }
          }
        }
      },
      "aggs": {
        "avg_tag_pice": {
          "terms": {
            "field": "type.keyword",
            "order": {
              "pic_stats.min": "desc"
            }
          },
          "aggs": {
            "pic_stats": {
              "stats": {
                "field": "pice"
              }
            }
          }
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    常用聚合函数

    histogram 函数
    统计价格在每个区段(间隔200)的产品数量
    {
      "size": 0, 
      "aggs": {
        "pice_histogram": {
          "histogram": {
            "field": "pice",
            "interval": 200,  # 分割间隔
            "keyed": false,  # true,则返回 key_value形式
            "min_doc_count": 1, # 满足结果大于等于1的带才返回
            "missing": 0 #  空值默认
          }
        }
      }
    }
    date_histogram 函数
    统计每月产品数量
    {
      "size": 0, 
      "aggs": {
        "create_time_histogram": {
          "date_histogram": {
            "field": "createtime",
            "calendar_interval": "month",  # 分割间隔  "fixed_interval"  间隔小最大单位 天
            "format": "yyyy-MM",   # 日期格式
            "extended_bounds": {  # 统计数据时间区段
              "min": "2020-01",
              "max": "2020-12"
            },
            "order": { # 排序
              "_count": "desc"
            }
          }
        }
      }
    }
    统计每月产品数量,再做累加
    {
      "size": 0, 
      "aggs": {
        "create_time_histogram": {
          "date_histogram": {
            "field": "createtime",
            "calendar_interval": "month",
            "min_doc_count": 0,
            "format": "yyyy-MM", 
            "extended_bounds": {
              "min": "2020-01",
              "max": "2020-12"
            }
          },
          "aggs": {  
            "sum_age": { # 求每月的总和
              "sum": {
                "field": "pice"
              }
            },
            "pice_cumulative_sum":{ # 累加每月总和
              "cumulative_sum": {
                "buckets_path": "sum_age"
              }
            }
          }
        }
      }
    }
    percentiles 函数 百分比占比统计, 数量越大统计越准确
    {
      "size": 0, 
      "aggs": {
        "pice_percentiles": {
          "percentiles": {
            "field": "pice",
            "percents": [
              1,
              5,
              25,
              50,
              75,
              95,
              99
            ]
          }
        }
      }
    }
    percentile_ranks 函数 范围占比统计 数量越大统计越准确
    {
      "size": 0, 
      "aggs": {
        "pice_percentiles": {
          "percentile_ranks": {
            "field": "pice",
            "values": [
              2000,
              4000,
              6000
            ]
          }
        }
      }
    }
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
  • 相关阅读:
    linux 基础,你掌握了几个?
    可视化剪辑,账号矩阵管理,视频分发,聚合私信多功能一体化营销工具 源代码开发部署方案
    MySQL在线修改表结构-PerconaTookit工具
    MySQL中的行锁
    中值滤波,均值滤波,高斯滤波,双边滤波,联合双边滤波介绍
    【黄啊码】用PHP7性能居然是5.6的三倍?赶紧看看它有什么新特性-续
    在Linux系统安装Kafka
    代码随想录算法训练营19期第50天
    一种计算整数位1个数的新方法
    《Qt5.9 C++开发指南》
  • 原文地址:https://blog.csdn.net/pys52055/article/details/136437604