聚合中有两个重要的概念:
Bucket : 桶,一类数据的集合。在传统数据概念上就是分组的意思。Bucket中可以再嵌套Bucket,不限深度。
Metric:指标,对一"bucket"中的数据进行统计(最小值、最大值、平均值、求和)
具体示例:
GET /cars/transactions/_search { "size" : 0, "aggs":{ "popular_colors":{//分组规则名称 "terms" : {//分组 "field" : "color" } } }}
组中再分组
GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs":{ "avg_price":{ "avg": { "field":"price" } } } } }}
多维度统计
GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price":{ "avg": { "field": "price" } }, "make":{ "terms": { "field":"make" } } } } } GET /cars/transactions/_search { "size" : 0, "aggs": { "colors": { "terms": { "field": "color" }, "aggs": { "avg_price": { "avg": { "field": "price" } }, "make" : { "terms" : { "field" : "make" }, "aggs":{ "min_price":{"min":{"field":"price"}}, "max_price":{"max":{"field":"price"}} } } } } }}
按区间统计
GET /cars/transactions/_search { "size" : 0, "aggs":{ "price":{ "histogram":{ //区间分组关键字 "field": "price", "interval": 20000 }, "aggs":{ "revenue": { "sum":{ "field" : "price" } } } } }}
按时间统计(区间统计也能完成时间统计的部分功能,但区间统计对时间不敏感,不支持像小时、天、月这样分,且不能很好的排序)
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", //按月分组,quarter按季度统计 "format": "yyyy-MM-dd" //返回的时间也此格式显示 } } }}
默认的,区间统计和时间统计在分组后若组中无文档,则不返回该组的信息,可通过下列配置强制返回
GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { "date_histogram": { "field": "sold", "interval": "month", "format": "yyyy-MM-dd", "min_doc_count" : 0, //组中无数据也要返回该组的信息 "extended_bounds" : { //设置边界 "min" : "2014-01-01", "max" : "2014-12-31" } } } }}
默认地,在统计时是基于query(无query默认是使用match_all)的查询结果上进行统计,即使用query作为作用域scope
GET /cars/transactions/_search { "query" : { "match" : { "make" : "ford" } }, "aggs" : { "colors" : { "terms" : { "field" : "color" } } }}
可加入global关键字来摆脱scope的限制
GET /cars/transactions/_search { "size" : 0, "query" : { "match" : { "make" : "ford" } }, "aggs" : { "single_avg_price": { "avg" : { "field" : "price" } //在scope范围内 }, "all": { "global" : {}, //声明包含所有文档 "aggs" : { "avg_price": { "avg" : { "field" : "price" } //在所有文档上统计 } } } }}
聚合中有两个重要的概念:
Bucket : 桶,一类数据的集合。在传统数据概念上就是分组的意思。Bucket中可以再嵌套Bucket,不限深度。
Metric:指标,对一"bucket"中的数据进行统计(最小值、最大值、平均值、求和)
具体示例:
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"popular_colors" : {//分组规则名称
"terms" : {//分组
"field" : "color"
}
}
}}
组中再分组
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}}
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"make": {
"terms": {
"field": "make"
}
}
}
}
}
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"colors": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": { "avg": { "field": "price" }
},
"make" : {
"terms" : {
"field" : "make"
},
"aggs" : {
"min_price" : { "min": { "field": "price"} },
"max_price" : { "max": { "field": "price"} }
}
}
}
}
}}
按区间统计
GET /cars/transactions/_search
{
"size" : 0,
"aggs":{
"price":{
"histogram":{ //区间分组关键字
"field": "price",
"interval": 20000
},
"aggs":{
"revenue": {
"sum": {
"field" : "price"
}
}
}
}
}}
按时间统计(区间统计也能完成时间统计的部分功能,但区间统计对时间不敏感,不支持像小时、天、月这样分,且不能很好的排序)
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"sales": {
"date_histogram": {
"field": "sold",
"interval": "month", //按月分组,quarter按季度统计
"format": "yyyy-MM-dd" //返回的时间也此格式显示
}
}
}}
默认的,区间统计和时间统计在分组后若组中无文档,则不返回该组的信息,可通过下列配置强制返回
GET /cars/transactions/_search
{
"size" : 0,
"aggs": {
"sales": {
"date_histogram": {
"field": "sold",
"interval": "month",
"format": "yyyy-MM-dd",
"min_doc_count" : 0, //组中无数据也要返回该组的信息
"extended_bounds" : { //设置边界
"min" : "2014-01-01",
"max" : "2014-12-31"
}
}
}
}}
默认地,在统计时是基于query(无query默认是使用match_all)的查询结果上进行统计,即使用query作为作用域scope
GET /cars/transactions/_search
{
"query" : {
"match" : {
"make" : "ford"
}
},
"aggs" : {
"colors" : {
"terms" : {
"field" : "color"
}
}
}}
可加入global关键字来摆脱scope的限制
GET /cars/transactions/_search
{
"size" : 0,
"query" : {
"match" : {
"make" : "ford"
}
},
"aggs" : {
"single_avg_price": {
"avg" : { "field" : "price" } //在scope范围内
},
"all": {
"global" : {}, //声明包含所有文档
"aggs" : {
"avg_price": {
"avg" : { "field" : "price" } //在所有文档上统计
}
}
}
}}