zoukankan      html  css  js  c++  java
  • 36.分组聚合操作—bucket进行多层嵌套

    主要知识点:

    • 分组聚合操作—嵌套bucket

       

       

    本讲以前面电商实例,从颜色到品牌进行下钻分析,每种颜色的平均价格,以及找到每种颜色每个品牌的平均价格。

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

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

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

       

    语法:

       

    GET /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"

    }

    }

    }

    }

    }

    }

    }

    }

    执行结果(部分):

       

    "aggregations": {

    "group_by_color": {

    "doc_count_error_upper_bound": 0,

    "sum_other_doc_count": 0,

    "buckets": [

    {

    "key": "红色",

    "doc_count": 4,

    "color_to_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": {

    "doc_count_error_upper_bound": 0,

    "sum_other_doc_count": 0,

    "buckets": [

    {

    "key": 2000,

    "doc_count": 2

    },

    {

    "key": 1000,

    "doc_count": 1

    }

    ]

    }

    },

    {

    "key": "三星",

    "doc_count": 1,

    "brand_avg_price": {

    "doc_count_error_upper_bound": 0,

    "sum_other_doc_count": 0,

    "buckets": [

    {

    "key": 8000,

    "doc_count": 1

    }

    ]

    }

    }

    ]

    }

    }

    一定要注意这种写法,要注意这些语句的层级关系。

  • 相关阅读:
    定时任务:crontab: installing new crontab
    报错:Sqoop Failing this attempt. Failing the application.
    数据库优化
    vscode ,保存失败提示:关于Failed to save 'package.json': The content of the file is newer. Please..解决办法
    php -1 month 的问题
    sql 中convert和cast区别
    [SQL case when的两种用法]
    Android编译Lame库(Mp3编解码库)
    AndroidStudio使用Cmake编译armeabi-v7a,arm64-v8a的so库
    图片压缩原理
  • 原文地址:https://www.cnblogs.com/liuqianli/p/8535800.html
Copyright © 2011-2022 走看看