elasticSearch核心概念的介绍(十):指标聚合查询

聚合查询之指标查询

上一章介绍基本的范围、布尔、排序查询,有兴趣的可以参考一下
elasticSearch核心概念的介绍(九):范围、布尔、排序查询
这一章我们介绍了聚合查询的指标聚合

ES 聚合查询是什么
  • 聚合查询时数据库中重要的功能特性,完成对一个查询得到的数据集的聚合计算,如:找出某字段(或计算表达式的结果)的最大值、最小值、计算和、平均值等。ES作为搜索引擎,同样提供了强大的聚合分析能力。
  • 对一个数据集求最大、最小、和、平均值等指标的聚合,在ES 中成为指标聚合
  • 而关系型数据中除了有集合函数外,还可以对查询出的数据进行分组group by,再在祖上进行指标聚合,在ES 中称为 桶聚合
max最大数
min最小数
sum求和
avg平均数
value_count统计非空字段的文档数
Cardinality值去重
stats统计 count max min avg sum5个值
Extended stats比stats多4个统计:平方和、方差、标准差、平均值加/减两个标准差的区间
Percentiles占比百分位对应的值统计,默认返回[1,5,25,50,75,95,99]分位上的值

查询出平均球衣号

  • 请求

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match":{
                "name":"库里"
            }
        },
        "aggs":{
            "avgJer":{
               "avg":{
                    "field":"jerse_no"
               }
            }
        }
    }
    '
    
  • 响应

    {
        "took": 3,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 0.26706278,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "l4cuG38B4RxnBaUYPdnZ",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 30
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mIcuG38B4RxnBaUYWNko",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 23
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mYcuG38B4RxnBaUYadlM",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 35
                    }
                }
            ]
        },
        "aggregations": {
            "avgJer": {
                "value": 29.333333333333332
            }
        }
    }
    

Cardinality 去重

  • 请求

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match":{
                "name":"库里"
            }
        },
        "aggs":{
            "avgJer":{
               "cardinality":{
                    "field":"position"
               }
            }
        }
    }
    '
    
  • 响应

    {
        "took": 21,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 0.26706278,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "l4cuG38B4RxnBaUYPdnZ",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 30
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mIcuG38B4RxnBaUYWNko",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 23
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mYcuG38B4RxnBaUYadlM",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 35
                    }
                }
            ]
        },
        "aggregations": {
            "avgJer": {
                "value": 1
            }
        }
    }
    

stats 查出聚合集合

  • 请求

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    
    {
        "query":{
            "match":{
                "name":"库里"
            }
        },
        "aggs":{
            "statsAge":{
               "stats":{
                    "field":"jerse_no"
               }
            }
        }
    }
    '
    
  • 响应

    {
        "took": 10,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 0.26706278,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "l4cuG38B4RxnBaUYPdnZ",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 30
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mIcuG38B4RxnBaUYWNko",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 23
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mYcuG38B4RxnBaUYadlM",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 35
                    }
                }
            ]
        },
        "aggregations": {
            "statsAge": {
                "count": 3,
                "min": 23.0,
                "max": 35.0,
                "avg": 29.333333333333332,
                "sum": 88.0
            }
        }
    }
    

Extended stats

  • 请求

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    {
        "query":{
            "match":{
                "name":"库里"
            }
        },
        "aggs":{
            "extendStats":{
               "extended_stats":{
                    "field":"jerse_no"
               }
            }
        }
    }
    
    '
    
  • 响应

    {
        "took": 20,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 3,
                "relation": "eq"
            },
            "max_score": 0.26706278,
            "hits": [
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "l4cuG38B4RxnBaUYPdnZ",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 30
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mIcuG38B4RxnBaUYWNko",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 23
                    }
                },
                {
                    "_index": "nba",
                    "_type": "_doc",
                    "_id": "mYcuG38B4RxnBaUYadlM",
                    "_score": 0.26706278,
                    "_source": {
                        "name": "库里",
                        "team_name": "勇士",
                        "position": "组织后卫",
                        "play_year": "10",
                        "jerse_no": 35
                    }
                }
            ]
        },
        "aggregations": {
            "extendStats": {
                "count": 3,
                "min": 23.0,
                "max": 35.0,
                "avg": 29.333333333333332,
                "sum": 88.0,
                "sum_of_squares": 2654.0,
                "variance": 24.22222222222217,
                "std_deviation": 4.921607686744462,
                "std_deviation_bounds": {
                    "upper": 39.17654870682226,
                    "lower": 19.490117959844408
                }
            }
        }
    }
    

Percentiles 统计球衣号的占比

  • 请求

    curl -X POST "http://172.25.45.150:9200/nba/_search" -H 'Content-Type:application/json' -d '
    
    {
        "query":{
            "match":{
                "name":"库里"
            }
        },
        "aggs":{
            "percentJerse":{
               "percentiles":{
                    "field":"jerse_no"
               }
            }
        }
    }
    '
    
    • 当前我们也可以自定义百分位数,不使用默认的参数如下

      {
          "query":{
              "match":{
                  "name":"库里"
              }
          },
          "aggs":{
              "percentJerse":{
                 "percentiles":{
                      "field":"jerse_no",
                      "percents":[
                          20,
                          50,
                          75
                      ]
                 }
              }
          }
      }
      
      
  • 响应

{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.26706278,
        "hits": [
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "l4cuG38B4RxnBaUYPdnZ",
                "_score": 0.26706278,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "组织后卫",
                    "play_year": "10",
                    "jerse_no": 30
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "mIcuG38B4RxnBaUYWNko",
                "_score": 0.26706278,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "组织后卫",
                    "play_year": "10",
                    "jerse_no": 23
                }
            },
            {
                "_index": "nba",
                "_type": "_doc",
                "_id": "mYcuG38B4RxnBaUYadlM",
                "_score": 0.26706278,
                "_source": {
                    "name": "库里",
                    "team_name": "勇士",
                    "position": "组织后卫",
                    "play_year": "10",
                    "jerse_no": 35
                }
            }
        ]
    },
    "aggregations": {
        "percentJerse": {
            "values": {
                "1.0": 22.999999999999996,
                "5.0": 23.0,
                "25.0": 24.75,
                "50.0": 30.0,
                "75.0": 33.75,
                "95.0": 35.0,
                "99.0": 35.0
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈橙橙丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值