- 导入数据:
POST /cars/transactions/_bulk
{ “index”: {}}
{ “price” : 10000, “color” : “red”, “make” : “honda”, “sold” : “2014-10-28” }
{ “index”: {}}
{ “price” : 20000, “color” : “red”, “make” : “honda”, “sold” : “2014-11-05” }
{ “index”: {}}
{ “price” : 30000, “color” : “green”, “make” : “ford”, “sold” : “2014-05-18” }
{ “index”: {}}
{ “price” : 15000, “color” : “blue”, “make” : “toyota”, “sold” : “2014-07-02” }
{ “index”: {}}
{ “price” : 12000, “color” : “green”, “make” : “toyota”, “sold” : “2014-08-19” }
{ “index”: {}}
{ “price” : 20000, “color” : “red”, “make” : “honda”, “sold” : “2014-11-05” }
{ “index”: {}}
{ “price” : 80000, “color” : “red”, “make” : “bmw”, “sold” : “2014-01-01” }
{ “index”: {}}
{ “price” : 25000, “color” : “blue”, “make” : “ford”, “sold” : “2014-02-12” }
通过head插件看到新建的索引cars的所有数据如下图,例如第一条记录,表示售价30000,汽车颜色是绿色,品牌是ford,销售时间是2014年5月8日:
最简单的聚合:terms桶第一个聚合命令是terms桶,相当于SQL中的group by,将所有记录按照颜色聚合,执行以下查询命令:
GET /cars/transactions/_search
{
“size”:0,
“aggs”:{
“popular_colors”:{
“terms”: {
“field”: “color”
}
}
}
}
收到响应如下:
{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 8,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“popular_colors” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [
{
“key” : “red”,
"doc
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
_count" : 4
},
{
“key” : “blue”,
“doc_count” : 2
},
{
“key” : “green”,
“doc_count” : 2
}
]
}
}
}
现在对查询命令中的参数做出解释:
-
size设置为0,这样返回的hits字段为空(hits不是我们本次查询关心的内容),这样可以提高查询速度;
-
aggs:聚合操作都被至于aggs之下,注意aggs是顶层参数,另外使用aggregations替代aggs也可以;
-
popular_colors:为聚合的类型指定名称,本次是按照颜色来聚合的,所以起名为popular_colors,响应内容中可以看到该字段的聚合结果如下:
aggregations" : {
“popular_colors” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [
{
“key” : “red”,
“doc_count” : 4
},
{
“key” : “blue”,
“doc_count” : 2
},
…
-
terms:在聚合的时候,桶的类型有很多种,terms是常用的一种,作用是按照指定字段来聚合,例如本例指定了color字段,所以所有color为red的文档聚合到一个桶,green的文档聚合到另一个桶,实际上桶类型是有很多种的,常见的类型在后面的实战中会用到,更多详细内容请参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.0/search-aggregations-bucket.html
-
field的值就是terms桶指定的聚合字段,这里是color字段;
-
接下来看看返回的信息,aggregations就是聚合结果,popular_colors是我们指定的别名,buckets是个json数组,里面的每个json对象都是一个桶,里面的doc_count就是记录数;例如结果中的第一条记录就是红色汽车的销售记录;
-
上面的示例返回的是每个桶中的文档数量,接下es支持丰富的指标,例如平均值(Avg)、最大值(Max)、最小值(Min)、累加和(Sum)等,接下来试试累加和的用法;
-
下面请求的作用是统计每种颜色汽车的销售总额:
GET /cars/transactions/_search
{
“size”:0,
“aggs”:{
“colors”:{
“terms”: {
“field”: “color”
},
“aggs”:{
“sales”:{
“sum”:{
“field”:“price”
}
}
}
}
}
}
收到响应如下:
{
“took” : 17,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 8,
“max_score” : 0.0,
“hits” : [ ]
},
“aggregations” : {
“colors” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [
{
“key” : “red”,
“doc_count” : 4,
“sales” : {
“value” : 130000.0
}
},
{
“key” : “blue”,
“doc_count” : 2,
“sales” : {
“value” : 40000.0
}
},
{
“key” : “green”,
“doc_count” : 2,
“sales” : {
“value” : 42000.0
}
}
]
}
}
}
- 请求参数和第一次的请求相比,按颜色聚合的参数不变,但是内部多了个aggs对象,详细的说明如下:
GET /cars/transactions/_search
{
“size”:0,
“aggs”:{ ------和前面一样,指定聚合操作
“colors”:{ ------别名
“terms”: { ------桶类型是按指定字段聚合
“field”: “color” ------按照color字段聚合
},
“aggs”:{ ------新增的aggs对象,用于处理聚合在每个桶内的文档
“sales”:{ ------别名
“sum”:{ ------度量指标是指定字段求和
“field”:“price” —求和的字段是price
}
}
}
}
}
}
- 对响应的数据说明如下:
“aggregations” : { ------聚合结果
“colors” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [ ------这个json数组的每个对象代表一个桶
{
“key” : “red”, ------该桶将所有color等于red的文档聚合进来
“doc_count” : 4, ------有4个color等于red的文档



