• es 聚合操作例子(aggregation)


    es聚合,类似于sql中的group:

    1. Bucket aggregation:满足特定条件的文档的集合
    2. Metric aggregation:数学运算,对文档字段进行统计分析
    3. Pipeline aggregation:对其他聚合结果进行二次聚合
    4. Matrix aggregation:多字段操作,并提供一个结果矩阵

    统计脚本

    get kibana_sample_data_flights/_search
    {
      "size":0,
      "aggs":
      {
        "flight_dest":
        {
           "terms":{
             "field":"DestCountry"
           },
           "aggs": {
             "stats_price":
             {
               "stats": {
                 "field": "AvgTicketPrice"
               }
             },
             "average_price": {
               "avg": {
                 "field": "AvgTicketPrice"
               }
             },
             "max_price":
             {
                "max": {
                  "field": "AvgTicketPrice"
                }
             },
             "min_price":
             {
               "min": {
                 "field": "AvgTicketPrice"
               }
             },
             "weather":
           {
             "terms":
             {
               "field":"DestWeather"
             }
           }
           }
           
        }
      }
    }
    

    结果:

    {
      "took" : 15,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 10000,
          "relation" : "gte"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "flight_dest" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 3187,
          "buckets" : [
            {
              "key" : "IT",
              "doc_count" : 2371,
              "max_price" : {
                "value" : 1195.3363037109375
              },
              "min_price" : {
                "value" : 100.57646942138672
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Clear",
                    "doc_count" : 428
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 424
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 417
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 414
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 182
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 173
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 169
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 164
                  }
                ]
              },
              "average_price" : {
                "value" : 586.9627099618385
              },
              "stats_price" : {
                "count" : 2371,
                "min" : 100.57646942138672,
                "max" : 1195.3363037109375,
                "avg" : 586.9627099618385,
                "sum" : 1391688.585319519
              }
            },
            {
              "key" : "US",
              "doc_count" : 1987,
              "max_price" : {
                "value" : 1199.72900390625
              },
              "min_price" : {
                "value" : 100.14596557617188
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Rain",
                    "doc_count" : 371
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 346
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 345
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 330
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 157
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 155
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 142
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 141
                  }
                ]
              },
              "average_price" : {
                "value" : 595.7743908825026
              },
              "stats_price" : {
                "count" : 1987,
                "min" : 100.14596557617188,
                "max" : 1199.72900390625,
                "avg" : 595.7743908825026,
                "sum" : 1183803.7146835327
              }
            },
            {
              "key" : "CN",
              "doc_count" : 1096,
              "max_price" : {
                "value" : 1198.4901123046875
              },
              "min_price" : {
                "value" : 102.90382385253906
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Sunny",
                    "doc_count" : 209
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 207
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 192
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 173
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 86
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 81
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 79
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 69
                  }
                ]
              },
              "average_price" : {
                "value" : 640.7101617033464
              },
              "stats_price" : {
                "count" : 1096,
                "min" : 102.90382385253906,
                "max" : 1198.4901123046875,
                "avg" : 640.7101617033464,
                "sum" : 702218.3372268677
              }
            },
            {
              "key" : "CA",
              "doc_count" : 944,
              "max_price" : {
                "value" : 1198.8525390625
              },
              "min_price" : {
                "value" : 100.5572509765625
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Clear",
                    "doc_count" : 197
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 173
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 156
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 148
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 80
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 69
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 62
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 59
                  }
                ]
              },
              "average_price" : {
                "value" : 648.7471090413757
              },
              "stats_price" : {
                "count" : 944,
                "min" : 100.5572509765625,
                "max" : 1198.8525390625,
                "avg" : 648.7471090413757,
                "sum" : 612417.2709350586
              }
            },
            {
              "key" : "JP",
              "doc_count" : 774,
              "max_price" : {
                "value" : 1199.4913330078125
              },
              "min_price" : {
                "value" : 103.97209930419922
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Rain",
                    "doc_count" : 152
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 138
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 130
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 123
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 66
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 58
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 57
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 50
                  }
                ]
              },
              "average_price" : {
                "value" : 650.9203447346847
              },
              "stats_price" : {
                "count" : 774,
                "min" : 103.97209930419922,
                "max" : 1199.4913330078125,
                "avg" : 650.9203447346847,
                "sum" : 503812.346824646
              }
            },
            {
              "key" : "RU",
              "doc_count" : 739,
              "max_price" : {
                "value" : 1196.7423095703125
              },
              "min_price" : {
                "value" : 101.0040054321289
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Cloudy",
                    "doc_count" : 149
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 128
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 122
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 117
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 62
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 56
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 55
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 50
                  }
                ]
              },
              "average_price" : {
                "value" : 662.9949632162009
              },
              "stats_price" : {
                "count" : 739,
                "min" : 101.0040054321289,
                "max" : 1196.7423095703125,
                "avg" : 662.9949632162009,
                "sum" : 489953.27781677246
              }
            },
            {
              "key" : "CH",
              "doc_count" : 691,
              "max_price" : {
                "value" : 1196.496826171875
              },
              "min_price" : {
                "value" : 101.3473129272461
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Cloudy",
                    "doc_count" : 135
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 134
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 128
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 115
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 51
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 46
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 41
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 41
                  }
                ]
              },
              "average_price" : {
                "value" : 575.1067587028537
              },
              "stats_price" : {
                "count" : 691,
                "min" : 101.3473129272461,
                "max" : 1196.496826171875,
                "avg" : 575.1067587028537,
                "sum" : 397398.7702636719
              }
            },
            {
              "key" : "GB",
              "doc_count" : 449,
              "max_price" : {
                "value" : 1197.78564453125
              },
              "min_price" : {
                "value" : 111.34574890136719
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Rain",
                    "doc_count" : 93
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 81
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 77
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 71
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 34
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 32
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 31
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 30
                  }
                ]
              },
              "average_price" : {
                "value" : 650.5326856005696
              },
              "stats_price" : {
                "count" : 449,
                "min" : 111.34574890136719,
                "max" : 1197.78564453125,
                "avg" : 650.5326856005696,
                "sum" : 292089.17583465576
              }
            },
            {
              "key" : "AU",
              "doc_count" : 416,
              "max_price" : {
                "value" : 1197.6326904296875
              },
              "min_price" : {
                "value" : 102.2943115234375
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Rain",
                    "doc_count" : 80
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 75
                  },
                  {
                    "key" : "Clear",
                    "doc_count" : 73
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 57
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 38
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 34
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 32
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 27
                  }
                ]
              },
              "average_price" : {
                "value" : 669.5588319668403
              },
              "stats_price" : {
                "count" : 416,
                "min" : 102.2943115234375,
                "max" : 1197.6326904296875,
                "avg" : 669.5588319668403,
                "sum" : 278536.47409820557
              }
            },
            {
              "key" : "PL",
              "doc_count" : 405,
              "max_price" : {
                "value" : 1185.43701171875
              },
              "min_price" : {
                "value" : 104.28328704833984
              },
              "weather" : {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets" : [
                  {
                    "key" : "Clear",
                    "doc_count" : 74
                  },
                  {
                    "key" : "Rain",
                    "doc_count" : 71
                  },
                  {
                    "key" : "Cloudy",
                    "doc_count" : 67
                  },
                  {
                    "key" : "Sunny",
                    "doc_count" : 66
                  },
                  {
                    "key" : "Thunder & Lightning",
                    "doc_count" : 37
                  },
                  {
                    "key" : "Damaging Wind",
                    "doc_count" : 30
                  },
                  {
                    "key" : "Hail",
                    "doc_count" : 30
                  },
                  {
                    "key" : "Heavy Fog",
                    "doc_count" : 30
                  }
                ]
              },
              "average_price" : {
                "value" : 662.4497233072917
              },
              "stats_price" : {
                "count" : 405,
                "min" : 104.28328704833984,
                "max" : 1185.43701171875,
                "avg" : 662.4497233072917,
                "sum" : 268292.1379394531
              }
            }
          ]
        }
      }
    }
    
    
    作者:iBrake
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    将Microsoft SQL Server 2000数据库转换成MySQL数据库
    centos7 升级php版本
    Jquery Ajax方法传递json到action
    2015/12/7
    sql server 2008 评估期已过期
    C# 邮件发送注意事项
    ReSharper warning: Virtual member call in a constructor
    EF code first 生成edmx文件
    EF 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭
    C# 发送邮件
  • 原文地址:https://www.cnblogs.com/Brake/p/14529988.html
Copyright © 2020-2023  润新知