• elasticsearch7 分组统计


    批量插入数据如下:

    POST /cars/_bulk
    { “index”: {}}
    { “价格” : 10000, “颜色” : “red”, “品牌” : “honda”, “售出时间” : “2014-10-28” }
    { “index”: {}}
    { “价格” : 20000, “颜色” : “red”, “品牌” : “honda”, “售出时间” : “2014-11-05” }
    { “index”: {}}
    { “价格” : 30000, “颜色” : “green”, “品牌” : “ford”, “售出时间” : “2014-05-18” }
    { “index”: {}}
    { “价格” : 15000, “颜色” : “blue”, “品牌” : “toyota”, “售出时间” : “2014-07-02” }
    { “index”: {}}
    { “价格” : 12000, “颜色” : “green”, “品牌” : “toyota”, “售出时间” : “2014-08-19” }
    { “index”: {}}
    { “价格” : 20000, “颜色” : “red”, “品牌” : “honda”, “售出时间” : “2014-11-05” }
    { “index”: {}}
    { “价格” : 80000, “颜色” : “red”, “品牌” : “bmw”, “售出时间” : “2014-01-01” }
    { “index”: {}}
    { “价格” : 25000, “颜色” : “blue”, “品牌” : “ford”, “售出时间” : “2014-02-12” }

    分组统计写法

      GET /cars/_search
       {
            "aggs": {
                "分组名": {
                    "terms": {
                        "field": "颜色"
                    },
                    "aggs": {
                        "平均值": {
                            "avg": {
                                "field": "价格"
                            }
                        },
                        "计数": {
                            "terms": {
                                "field": "品牌"
                            }
                        }
                    }
                }
            }
      }
      ### 按照颜色字段分组统计,求价格的分组平均值,和分组后的区域计数
      ###
        avg 平均
        max 最大
        min 最小
        sum 合计 
    terms 计数
    
    • 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

    自动分段

    ###以每隔5000的价格进行自动分段

            GET /cars/_search
            {
            "size": 0,
            "aggs": {
                "price_histogram": {
                "histogram": {
                    "field": "价格",
                    "interval": 5000,
                    "min_doc_count": 1
                }
                }
            }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    自定义分段

    GET /cars/_search
     {
            "size": 0,
            "aggs": {
                "price_range": {
                "range": {
                    "field": "价格",
                    "ranges": [
                    {
                        "from": 5000,
                        "to": 15000
                    },
                    {
                        "from": 15000,
                        "to": 17000
                    },
                    {
                        "from": 17000,
                        "to": 25000
                    },
                    {
                        "from": 25000,
                        "to": 39000
                    },
                    {
                        "from": 39000               
                    }
                    ]
                }
                }
            }
    }
    
    • 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
  • 相关阅读:
    osgEarth示例分析——osgearth_imageoverlay
    构建高效沟通桥梁 —— 测试团队中的快速反馈机制建设
    黑灰产套现城市消费券大揭秘
    idea修改颜色
    23.2 Bootstrap 卡片
    Hadoop3 - MapReduce 并行机制
    进程间通讯(匿名管道,命名管道,共享空间)
    C语言char与short取反以及符号判断问题
    总结:SpringBoot常用注解
    D1. Balance (Easy version)(暴力&数论)
  • 原文地址:https://blog.csdn.net/zdyah/article/details/126768421