# Elasticsearch--Aggregation详细总结（聚合统计）

Elasticsearch的Aggregation功能也异常强悍。

Aggregation共分为三种：Metric Aggregations、Bucket Aggregations、 Pipeline Aggregations。下面将分别进行总结。

#########################################

1、Metric Aggregations

1>Avg Aggregation  #计算出字段平均值

{
"aggs" : {
"avg_grade" : { "avg" : { "field" : "grade" } }
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"avg": {
}
}
}
}

2>Cardinality Aggregation  #计算出字段的唯一值。相当于sql中的distinct

{
"aggs" : {
"author_count" : {
"cardinality" : {
"field" : "author"
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"author_count": {
"cardinality": {
"field": "author"
}
}
}
}

{
"aggs" : {
"grades_stats" : { "extended_stats" : { "field" : "grade" } }
}
}

{
"query": {
"match_all": {}
},
"aggs": {
"extended_stats": {
}
}
}
}

{
...

"aggregations": {
"count": 9,
"min": 72,
"max": 99,
"avg": 86,
"sum": 774,
"sum_of_squares": 67028,
"variance": 51.55555555555556,
"std_deviation": 7.180219742846005,
"std_deviation_bounds": {
"upper": 100.36043948569201,
"lower": 71.63956051430799
}
}
}
}

4>Geo Bounds Aggregation #计算出所有的地理坐标将会落在一个矩形区域。比如说朝阳区域有很多饭店，我就可以用一个矩形把这些饭店都圈起来，看看范围。

{
"query" : {
"match" : { "business_type" : "shop" }
},
"aggs" : {
"viewport" : {
"geo_bounds" : {
"field" : "location",
"wrap_longitude" : true
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"viewport": {
"geo_bounds": {
"field": "location",
"wrap_longitude": true
}
}
}
}

{
...

"aggregations": {
"viewport": {
"bounds": {
"top_left": {
"lat": 80.45,
"lon": -160.22
},
"bottom_right": {
"lat": 40.65,
"lon": 42.57
}
}
}
}
}

5>Geo Centroid Aggregation   #计算出所有文档的大概的中心点。比如说某个地区盗窃犯罪很多，那我这样就可以看到这片区域到底哪个点（街道）偷盗事件最猖狂。

{
"query" : {
"match" : { "crime" : "burglary" }
},
"aggs" : {
"centroid" : {
"geo_centroid" : {
"field" : "location"
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"centroid": {
"geo_centroid": {
"field": "location"
}
}
}
}

6>Max Aggregation  #求最大值

{
"aggs" : {
"max_price" : { "max" : { "field" : "price" } }
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}

7>Min Aggregation #求最小值

{
"aggs" : {
"min_price" : { "min" : { "field" : "price" } }
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"min_price": {
"min": {
"field": "price"
}
}
}
}

8>Percentiles Aggregation #百分比统计。可以看出你网站的所有页面。加载时间的差异。

{
"aggs" : {
"percentiles" : {
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"percentiles": {
}
}
}
}

{
...

"aggregations": {
"values" : {
"1.0": 15,
"5.0": 20,
"25.0": 23,
"50.0": 25,
"75.0": 29,
"95.0": 60,
"99.0": 150
}
}
}
}

9>Percentile Ranks Aggregation #看看15毫秒和30毫秒内大概有多少页面加载完。

{
"aggs" : {
"percentile_ranks" : {
"values" : [15, 30]
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"percentile_ranks": {
"values": [
15,
30
]
}
}
}
}

{
...

"aggregations": {
"values" : {
"15": 92,
"30": 100
}
}
}
}

10>Stats Aggregation  #最大、最小、和、平均值。一起求出来

{
"aggs" : {
"grades_stats" : { "stats" : { "field" : "grade" } }
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"stats": {
}
}
}
}

11>Sum Aggregation #求和

"aggs" : {
"intraday_return" : { "sum" : { "field" : "change" } }
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"sum": {
"field": "change"
}
}
}
}

12>Top hits Aggregation  #较为常用的统计。获取到每组前n条数据。相当于sql 中 group by 后取出前n条。

{
"aggs": {
"top-tags": {
"terms": {
"field": "tags",
"size": 3
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"sort": [
{
"last_activity_date": {
"order": "desc"
}
}
],
"_source": {
"include": [
"title"
]
},
"size" : 1
}
}
}
}
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"all_interests": {
"terms": {
"field": "zxw_id",
"size": 100
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"size": 1
}
}
}
}
}
}

14>Value Count Aggregation  #数量统计，看看这个字段一共有多少个不一样的数值。

{
"aggs" : {
"grades_count" : { "value_count" : { "field" : "grade" } }
}
}

GET index/type/_search?search_type=count
{
"query": {
"match_all": {}
},
"aggs": {
"value_count": {
}
}
}
}

2、Bucket Aggregations 这是第二种类型的统计（用的也是最多的，最实用的。）。后续也是抄写，各位自己看吧。有问题需要讨论的=》1250134974@qq.com发邮件.

3、#这是第三中类型的聚合。