前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch 6.x 学习笔记:21.指标聚合

ElasticSearch 6.x 学习笔记:21.指标聚合

作者头像
程裕强
发布2022-05-06 19:16:58
2170
发布2022-05-06 19:16:58
举报

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics.html

为了方便聚合统计,增加两条文档

代码语言:javascript
复制
PUT my-index/person/5
{
  "name":"程裕强",
  "age":28,
  "salary":10000
}
PUT my-index/person/6
{
  "name":"hadron",
  "age":19,
  "salary":5000
}

21.1 max

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "max_age": {
      "max": {"field": "age"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "max_age": {
      "value": 28
    }
  }
}

21.2 min

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "min_age": {
      "min": {"field": "age"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "min_age": {
      "value": 19
    }
  }
}

21.3 avg

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-avg-aggregation.html

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "avg_salary": {
      "avg": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "avg_salary": {
      "value": 7166.666666666667
    }
  }
}

21.4sum

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "sum_salary": {
      "sum": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "sum_salary": {
      "value": 43000
    }
  }
}

21.5 stats

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "stats_salary": {
      "stats": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "stats_salary": {
      "count": 6,
      "min": 5000,
      "max": 10000,
      "avg": 7166.666666666667,
      "sum": 43000
    }
  }
}

21.6 高级统计

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-extendedstats-aggregation.html

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "stats_salary": {
      "extended_stats": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "stats_salary": {
      "count": 6,
      "min": 5000,
      "max": 10000,
      "avg": 7166.666666666667,
      "sum": 43000,
      "sum_of_squares": 325000000,
      "variance": 2805555.5555555522,
      "std_deviation": 1674.9792701868141,
      "std_deviation_bounds": {
        "upper": 10516.625207040295,
        "lower": 3816.7081262930387
      }
    }
  }
}

21.7 基数统计

https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-aggregations-metrics-cardinality-aggregation.html 工资基数(等级)

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "class_salary": {
      "cardinality": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 44,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "class_salary": {
      "value": 4
    }
  }
}

21.8 文档数量统计

为了方便统计包含某一字段(比如salary)的文档数,这里添加一个不含该字段的文档

代码语言:javascript
复制
PUT my-index/persion/7
{
  "name":"test",
  "age":26
}
代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "doc_count": {
      "value_count": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "doc_count": {
      "value": 6
    }
  }
}

21.9 百分位统计

代码语言:javascript
复制
GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "persion_salary": {
      "percentiles": {"field": "salary"}
    }
  }
}
代码语言:javascript
复制
{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "persion_salary": {
      "values": {
        "1.0": 5050,
        "5.0": 5250,
        "25.0": 6000,
        "50.0": 7000,
        "75.0": 8000,
        "95.0": 9500,
        "99.0": 9900
      }
    }
  }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 21.1 max
  • 21.2 min
  • 21.3 avg
  • 21.4sum
  • 21.5 stats
  • 21.6 高级统计
  • 21.7 基数统计
  • 21.8 文档数量统计
  • 21.9 百分位统计
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档