ES elasticsearch 各种聚合

准备数据
为了更好地展示,我们首先来把我们之前的 twitter 的数据做一点小的修改。我们添加一个新的字段 DOB (date of birth),也就是生日的意思。同时,我们也对 province,city 及 country 字段的类型做了调整,并把它们作为 keyword。我们来做如下的操作:

DELETE twitter

PUT twitter
{
"mappings": {
"properties": {
"DOB": {
"type": "date"
},
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"city": {
"type": "keyword"
},
"country": {
"type": "keyword"
},
"location": {
"type": "geo_point"
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"province": {
"type": "keyword"
},
"uid": {
"type": "long"
},
"user": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
然后,我们再次使用 bulk API 来把我们的数据导入到 Elasticsearch 中:

POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}, "DOB": "1999-04-01"}
{"index":{"_index":"twitter","_id":2}}
{"user":"老刘","message":"出发,下一站云南!","uid":3,"age":22,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}, "DOB": "1997-04-01"}
{"index":{"_index":"twitter","_id":3}}
{"user":"李四","message":"happy birthday!","uid":4,"age":25,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}, "DOB": "1994-04-01"}
{"index":{"_index":"twitter","_id":4}}
{"user":"老贾","message":"123,gogogo","uid":5,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}, "DOB": "1989-04-01"}
{"index":{"_index":"twitter","_id":5}}
{"user":"老王","message":"Happy BirthDay My Friend!","uid":6,"age":26,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}, "DOB": "1993-04-01"}
{"index":{"_index":"twitter","_id":6}}
{"user":"老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":28,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}, "DOB": "1991-04-01"}
我们已经成功地导入数据。我们要特别注意的是,并不是所有的字段都可以做聚合的。一般来说,具有 keyword 或者数值类型的字段是可以做聚合的。我们可以通过 _field_caps 接口来进行查询:

GET twitter/_field_caps?fields=country
上面的命令给出的结果为:

{
"indices" : [
"twitter"
],
"fields" : {
"country" : {
"keyword" : {
"type" : "keyword",
"searchable" : true,
"aggregatable" : true
}
}
}
}
它表明这个 country 字段既是可以搜索的也是可以聚合的。如果大家不喜欢这种命令,我们可以可以直接到 twitter 索引的 index pattern 中进行查看:

我们需要为 twitter 索引创建一个 index pattern。从上面我们可以看出来 address 字段是可以搜索的,因为它是一个 text 的类型字段,但是它不可以进行聚合。同时,我们可以看出来 address.keyword 这个字段是可以同时搜索的和聚合的。

聚合操作
简单地说,聚合的语法是这样的:

"aggregations" : {
"<aggregation_name>" : {
"<aggregation_type>" : {
<aggregation_body>
}
[,"meta" : { [<meta_data_body>] } ]?
[,"aggregations" : { [<sub_aggregation>]+ } ]?
}
[,"<aggregation_name_2>" : { ... } ]*
}
通常,我们也可以使用 aggs 来代替上面的 “aggregations”。

下面,我们来针对我们的数据来进行一些简单的操作,这样可以使得大家更加明白一些。

range聚合
我们可以把用户进行年龄分段,查出来在不同的年龄段的用户:

GET twitter/_search
{
"size": 0,
"aggs": {
"age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 22
},
{
"from": 22,
"to": 25
},
{
"from": 25,
"to": 30
}
]
}
}
}
}
在这里,我们使用 range 类型的聚合。在上面我们定义了不同的年龄段。通过上面的查询,我们可以得到不同年龄段的 bucket。显示的结果是:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age" : {
"buckets" : [
{
"key" : "20.0-22.0",
"from" : 20.0,
"to" : 22.0,
"doc_count" : 1
},
{
"key" : "22.0-25.0",
"from" : 22.0,
"to" : 25.0,
"doc_count" : 1
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 3
}
]
}
}
}
在上面,我们也注意到,我们把 size 设置为0。这是因为针对聚合,我们并不关心返回的结果,也就是在默认情况下返回的10个文档。这样做的好处是更快的响应以及较小的负载。当然,额外的好处是可以缓存聚合的结果。在第二次进行同样的聚合时,速度会比以前快。我们可以阅读另外一篇文章 “cache 在 Elasticsearch 中的应用” 。假如我们设置为1的话,我们可以看到如下的输出:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"user" : "张三",
"message" : "今儿天气不错啊,出去转转去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中国",
"address" : "中国北京市海淀区",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
},
"DOB" : "1999-04-01"
}
}
]
},
"aggregations" : {
"age" : {
"buckets" : [
{
"key" : "20.0-22.0",
"from" : 20.0,
"to" : 22.0,
"doc_count" : 1
},
{
"key" : "22.0-25.0",
"from" : 22.0,
"to" : 25.0,
"doc_count" : 1
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 3
}
]
}
}
}
从这里,我们可以看出来,在我们的输出中,也看到了其中的一个文档的输出。

我们可以在 bucket 聚合之下,做 sub-aggregation:

GET twitter/_search
{
"size": 0,
"aggs": {
"age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 22
},
{
"from": 22,
"to": 25
},
{
"from": 25,
"to": 30
}
]
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
上面的意思是我们针对每个桶 20-22,22-25,25-30,分别计算它们的平均年龄。上面显示的结果是:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age" : {
"buckets" : [
{
"key" : "20.0-22.0",
"from" : 20.0,
"to" : 22.0,
"doc_count" : 1,
"avg_age" : {
"value" : 20.0
}
},
{
"key" : "22.0-25.0",
"from" : 22.0,
"to" : 25.0,
"doc_count" : 1,
"avg_age" : {
"value" : 22.0
}
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 3,
"avg_age" : {
"value" : 26.333333333333332
}
}
]
}
}
}
从上面我们可以看出来每个桶的平均年龄是多少。

你甚至可以在 sub-aggregation 之下做更多的聚合,比如:

GET twitter/_search
{
"size": 0,
"aggs": {
"age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 22
},
{
"from": 22,
"to": 25
},
{
"from": 25,
"to": 30
}
]
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
},
"min_age": {
"min": {
"field": "age"
}
},
"max_age": {
"max": {
"field": "age"
}
}
}
}
}
}
在上面,我们除了得到平均的年龄,也可以得到最大及最小的年龄。上面查询的结果为:

{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age" : {
"buckets" : [
{
"key" : "20.0-22.0",
"from" : 20.0,
"to" : 22.0,
"doc_count" : 1,
"max_age" : {
"value" : 20.0
},
"avg_age" : {
"value" : 20.0
},
"min_age" : {
"value" : 20.0
}
},
{
"key" : "22.0-25.0",
"from" : 22.0,
"to" : 25.0,
"doc_count" : 1,
"max_age" : {
"value" : 22.0
},
"avg_age" : {
"value" : 22.0
},
"min_age" : {
"value" : 22.0
}
},
{
"key" : "25.0-30.0",
"from" : 25.0,
"to" : 30.0,
"doc_count" : 3,
"max_age" : {
"value" : 28.0
},
"avg_age" : {
"value" : 26.333333333333332
},
"min_age" : {
"value" : 25.0
}
}
]
}
}
}
更多关于 bucket 聚合,请参阅我的文章 “Elasticsearch:透彻理解 Elasticsearch 中的 Bucket aggregation”。

Filters 聚合
在上面,我们使用 ranges 把数据分成不同的 bucket。通常这样的方法只适合字段为数字的字段。我们按照同样的思路,可以使用 filter 来对数据进行分类。在这种方法中,我们甚至可以针对非数字字段来进行建立不同的 bucket。这类聚合我们称之为 Filter aggregagation。定义一个多存储桶聚合,其中每个存储桶都与一个过滤器相关联。 每个存储桶将收集与其关联的过滤器匹配的所有文档。我们可以使用如下的例子:

GET twitter/_search
{
"size": 0,
"aggs": {
"by_cities": {
"filters": {
"filters": {
"beijing": {
"match": {
"city": "北京"
}
},
"shanghai": {
"match": {
"city": "上海"
}
}
}
}
}
}
}
在上面的例子中,我们使用 filters 来分别针对 “北京” 和 “上海” 两地的文档进行统计:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"by_cities" : {
"buckets" : {
"beijing" : {
"doc_count" : 5
},
"shanghai" : {
"doc_count" : 1
}
}
}
}
}
上面显示来自 “北京” 的有 5 个文档,而来自 “上海” 的只有一个文档。在上面我们为每个 filter 都取了一个名字,当然,我们如果不关心名称的话,我们甚至可以有如下的简洁格式:

GET twitter/_search
{
"size": 0,
"aggs": {
"by_cities": {
"filters": {
"filters": [
{
"match": {
"city": "北京"
}
},
{
"match": {
"city": "上海"
}
}
]
}
}
}
}
上面的返回结果为:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"by_cities" : {
"buckets" : [
{
"doc_count" : 5
},
{
"doc_count" : 1
}
]
}
}
}
Filter 聚合
在当前文档集上下文中定义与指定过滤器匹配的所有文档的单个存储桶。 通常,这将用于将当前聚合上下文缩小到一组特定的文档。这类聚合被称之为 Filter aggregation。你也可以理解为是上面的 Filters aggregation 的特殊情况,在它里面只含有一个 filter 的 Filters aggregation。我们以一个例子来讲:

GET twitter/_search
{
"size": 0,
"aggs": {
"beijing": {
"filter": {
"match": {
"city": "北京"
}
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
在上面,我们针对 “北京” 的文档,并求一个平均年龄。上面查询的结果为:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"beijing" : {
"doc_count" : 5,
"avg_age" : {
"value" : 24.6
}
}
}
}
date_range 聚合
我们可以使用 date_range 来统计在某个时间段里的文档数:

POST twitter/_search
{
"size": 0,
"aggs": {
"birth_range": {
"date_range": {
"field": "DOB",
"format": "yyyy-MM-dd",
"ranges": [
{
"from": "1989-01-01",
"to": "1990-01-01"
},
{
"from": "1991-01-01",
"to": "1992-01-01"
}
]
}
}
}
}
在上面我们查询出生年月(DOB)从1989-01-01到1990-01-01及从1991-01-01到1992-01-01的文档。显示的结果是:

"aggregations" : {
"birth_range" : {
"buckets" : [
{
"key" : "1989-01-01-1990-01-01",
"from" : 5.99616E11,
"from_as_string" : "1989-01-01",
"to" : 6.31152E11,
"to_as_string" : "1990-01-01",
"doc_count" : 1
},
{
"key" : "1991-01-01-1992-01-01",
"from" : 6.62688E11,
"from_as_string" : "1991-01-01",
"to" : 6.94224E11,
"to_as_string" : "1992-01-01",
"doc_count" : 1
}
]
}
terms聚合
我们也可以通过 term 聚合来查询某一个关键字出现的频率。在如下的 term 聚合中,我们想寻找在所有的文档出现 ”Happy birthday” 里按照城市进行分类的一个聚合。

GET twitter/_search
{
"query": {
"match": {
"message": "happy birthday"
}
},
"size": 0,
"aggs": {
"city": {
"terms": {
"field": "city",
"size": 10
}
}
}
}
注意这里的 10 指的是前 10 名的城市。聚合的结果是:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"city" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "北京",
"doc_count" : 2
},
{
"key" : "上海",
"doc_count" : 1
}
]
}
}
}
在上面,我们可以看出来,在所有的含有 "Happy birthday" 的文档中,有两个是来自北京的,有一个是来自上海。

在正常的情况下,聚合是按照 doc_count 来进行排序的,也就是说哪一个 key 的 doc_count 越多,那么它就排在第一位,以后依次排序。如果你想按照 key 进行排序的话,你可以尝试如下的方法:

GET twitter/_search
{
"size": 0,
"aggs": {
"top_cities": {
"terms": {
"field": "city",
"order": {
"_key": "asc"
}
}
}
}
}
上面返回的结果是:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"top_cities" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "上海",
"doc_count" : 1
},
{
"key" : "北京",
"doc_count" : 5
}
]
}
}
}
在这里,我们看到和之前不一样的排序。doc_count 为 1 的 “上海” 反而排到前面去了,虽然这个和我们之前的 asc (上升)排序是有点不太一致的地方 (按照拼音 shanghai 应该比 beijing 要大)。这个和语言的处理有关,但是我们确实看到我们是可以控制这个排序的。你也可以使用 _count 来进行升序的排列:

GET twitter/_search
{
"size": 0,
"aggs": {
"top_cities": {
"terms": {
"field": "city",
"order": {
"_count": "asc"
}
}
}
}
}
你也可以通过在 term 聚合中指定顺序来使用 nested aggreation 的结果进行排序,而不是按计数对结果进行排序:

GET twitter/_search
{
"size": 0,
"aggs": {
"top_cities": {
"terms": {
"field": "city",
"order": {
"avg_age": "desc"
}
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
在上面排序是按照 nested aggregation 的结果 avg_age 来按照降序来排列的。显示的结果为:

"aggregations" : {
"top_cities" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "上海",
"doc_count" : 1,
"avg_age" : {
"value" : 28.0
}
},
{
"key" : "北京",
"doc_count" : 5,
"avg_age" : {
"value" : 24.6
}
}
]
}
}
我们也可以使用 script 来生成一个在索引里没有的术语来进行统计。比如,我们可以通过如下的 script 来生成一个对文档人出生年份的统计:

POST twitter/_search
{
"size": 0,
"aggs": {
"birth_year": {
"terms": {
"script": {
"source": "2019 - doc['age'].value"
},
"size": 10
}
}
}
}
在上面,我们通过脚本:

"script": {
"source": "2019 - doc['age'].value"
}
根据年龄来生成出生的年月来进行统计:

"aggregations" : {
"birth_year" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "1989",
"doc_count" : 1
},
{
"key" : "1991",
"doc_count" : 1
},
{
"key" : "1993",
"doc_count" : 1
},
{
"key" : "1994",
"doc_count" : 1
},
{
"key" : "1997",
"doc_count" : 1
},
{
"key" : "1999",
"doc_count" : 1
}
]
}
}
在上面我们可以看到 key 为1991,1993,1994等。这些 key 在我们原有的字段中根本就不存在。

Histogram Aggregation
基于多桶值源的汇总,可以应用于从文档中提取的数值或数值范围值。 它根据值动态构建固定大小(也称为间隔)的存储桶。

GET twitter/_search
{
"size": 0,
"aggs": {
"age_distribution": {
"histogram": {
"field": "age",
"interval": 2
}
}
}
}
显示结果:

"aggregations" : {
"age_distribution" : {
"buckets" : [
{
"key" : 20.0,
"doc_count" : 1
},
{
"key" : 22.0,
"doc_count" : 1
},
{
"key" : 24.0,
"doc_count" : 1
},
{
"key" : 26.0,
"doc_count" : 1
},
{
"key" : 28.0,
"doc_count" : 1
},
{
"key" : 30.0,
"doc_count" : 1
}
]
}
}
上面显示从20-22年龄段,有一个文档。从22-24也有一个文档。

同样地,我们也可以进行 sub aggregation,并按照 sub aggregation 的值进行排序:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_distribution": {
"histogram": {
"field": "age",
"interval": 2,
"order": {
"avg_age": "desc"
}
},
"aggs": {
"avg_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
上面返回的结果是:

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_distribution" : {
"buckets" : [
{
"key" : 30.0,
"doc_count" : 1,
"avg_age" : {
"value" : 30.0
}
},
{
"key" : 28.0,
"doc_count" : 1,
"avg_age" : {
"value" : 28.0
}
},
{
"key" : 26.0,
"doc_count" : 1,
"avg_age" : {
"value" : 26.0
}
},
{
"key" : 24.0,
"doc_count" : 1,
"avg_age" : {
"value" : 25.0
}
},
{
"key" : 22.0,
"doc_count" : 1,
"avg_age" : {
"value" : 22.0
}
},
{
"key" : 20.0,
"doc_count" : 1,
"avg_age" : {
"value" : 20.0
}
}
]
}
}
}
显然这个是按照 avg_age 的值进行降序排列的。

date_histogram
这种聚合类似于正常的直方图,但只能与日期或日期范围值一起使用。 由于日期在 Elasticsearch 中内部以长值表示,因此也可以但不准确地对日期使用正常的直方图。

GET twitter/_search
{
"size": 0,
"aggs": {
"age_distribution": {
"date_histogram": {
"field": "DOB",
"interval": "year"
}
}
}
}
在上面我们使用 DOB 来作为 date_histogram 的字段来进行聚合统计。我们按照每隔一年这样的时间间隔来进行。显示结果:

"aggregations" : {
"age_distribution" : {
"buckets" : [
{
"key_as_string" : "1989-01-01T00:00:00.000Z",
"key" : 599616000000,
"doc_count" : 1
},
{
"key_as_string" : "1990-01-01T00:00:00.000Z",
"key" : 631152000000,
"doc_count" : 0
},
{
"key_as_string" : "1991-01-01T00:00:00.000Z",
"key" : 662688000000,
"doc_count" : 1
},
{
"key_as_string" : "1992-01-01T00:00:00.000Z",
"key" : 694224000000,
"doc_count" : 0
},
{
"key_as_string" : "1993-01-01T00:00:00.000Z",
"key" : 725846400000,
"doc_count" : 1
},
{
"key_as_string" : "1994-01-01T00:00:00.000Z",
"key" : 757382400000,
"doc_count" : 1
},
{
"key_as_string" : "1995-01-01T00:00:00.000Z",
"key" : 788918400000,
"doc_count" : 0
},
{
"key_as_string" : "1996-01-01T00:00:00.000Z",
"key" : 820454400000,
"doc_count" : 0
},
{
"key_as_string" : "1997-01-01T00:00:00.000Z",
"key" : 852076800000,
"doc_count" : 1
},
{
"key_as_string" : "1998-01-01T00:00:00.000Z",
"key" : 883612800000,
"doc_count" : 0
},
{
"key_as_string" : "1999-01-01T00:00:00.000Z",
"key" : 915148800000,
"doc_count" : 1
}
]
}
上面的结果显示 DOB 从 1989-01-01 到 1990-01-01 有一个文档。从 1990-01-01 到 1991-01-01 区间没有一个文档。

cardinality 聚合
我们也可以使用 cardinality 聚合来统计到底有多少个城市:

GET twitter/_search
{
"size": 0,
"aggs": {
"number_of_cities": {
"cardinality": {
"field": "city"
}
}
}
}
运行上面的查询,我们可以看到结果是:

{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"number_of_cities" : {
"value" : 2
}
}
}
它显示我们有两个城市:北京及上海。它们在文档中虽然出现多次,但是从唯一性上,只有两个城市。

Metric 聚合
我们可以使用 Metrics 来统计我们的数值数据,比如我们想知道所有用户的平均年龄是多少?我们可以用下面的聚合:

GET twitter/_search
{
"size": 0,
"aggs": {
"average_age": {
"avg": {
"field": "age"
}
}
}
}
我们的返回的结果是:

"aggregations" : {
"average_age" : {
"value" : 25.166666666666668
}
}
所有人的平均年龄是 25.166666666666668岁。

我们也可以对只在北京的用户文档进行统计:

POST twitter/_search
{
"size": 0,
"query": {
"match": {
"city": "北京"
}
},
"aggs": {
"average_age_beijing": {
"avg": {
"field": "age"
}
}
}
}
上面我们先查询到所有在北京的用户,然后再对这些文档进行求年龄的平均值。返回的结果:

"aggregations" : {
"average_age_beijing" : {
"value" : 24.6
}
}
聚合通常在查询搜索结果上执行。 Elasticsearch 提供了一个特殊的 global 聚合,该全局全局对所有文档执行,而不受查询的影响。

POST twitter/_search
{
"size": 0,
"query": {
"match": {
"city": "北京"
}
},
"aggs": {
"average_age_beijing": {
"avg": {
"field": "age"
}
},
"average_age_all": {
"global": {},
"aggs": {
"age_global_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}
在上面我们在 average_age_all 里添加了一个 gobal 的聚合,这个平均值将会使用所有的6个文档而不是限于在这个查询的5个北京的文档。返回的结果是:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"average_age_beijing" : {
"value" : 24.6
},
"average_age_all" : {
"doc_count" : 6,
"age_global_avg" : {
"value" : 25.166666666666668
}
}
}
}
我们也可以对整个年龄进行一个统计,比如:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_stats": {
"stats": {
"field": "age"
}
}
}
}
统计的结果如下:

"aggregations" : {
"age_stats" : {
"count" : 6,
"min" : 20.0,
"max" : 30.0,
"avg" : 25.166666666666668,
"sum" : 151.0
}
}
在这里,我们可以看到到底有多少条数据,并且最大,最小的,平均值及加起来的合都在这里一起显示。

如果你想了解更多的细节,你可以使用 extended_stats:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_stats": {
"extended_stats": {
"field": "age"
}
}
}
}
上面返回的结果为:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_stats" : {
"count" : 6,
"min" : 20.0,
"max" : 30.0,
"avg" : 25.166666666666668,
"sum" : 151.0,
"sum_of_squares" : 3869.0,
"variance" : 11.472222222222248,
"std_deviation" : 3.3870669054835996,
"std_deviation_bounds" : {
"upper" : 31.940800477633868,
"lower" : 18.392532855699468
}
}
}
}
对于一些不知道上面的 std_derivation 和 varianace 的开发者来说,我们举如下的一个例子:

假如你有一个系列的数据: 32,111,138,28,59,77,97,那么它的平均值为 (32+111+138+28+59+77+97) / 7 = 77.4。针对每个值,找出和平均值之间的差异:

32 - 77.4 = -45.4
111 - 77.4 = 33.6
138 - 77.4 = 60.6
28 - 77.4 = -49.4
59 - 77.4 = -18.4
77 - 77.4 = - 0.4
97 - 77.4 = 19.6
针对上面的每个值,他们的平方值为:

(-45.4)^2 = 2061.16
(33.6)^2 = 1128.96
(60.6)^2 = 3672.36
(-49.4)^2 = 2440.36
(-18.4)^2 = 338.56
(- 0.4)^2 = 0.16
(19.6)^2 = 384.16
variance 的值其实就是上面的差值平方和的平均值:

variance = (2061.16+1128.96+3672.36+2440.36+338.56+0.16+384.16) / 7 = 1432.2
那么 std_derivation 的值其实就是上面值的平方根值:std_derivation = sqrt(variance) = sqrt(1432.2) = 37.84。
如果你想对多个指标进行统计并显示它们之间的关系,你可以使用 matrix_stats:

GET twitter/_search
{
"size": 0,
"aggs": {
"matrix_stats": {
"matrix_stats": {
"fields": ["age", "uid"]
}
}
}
}
上面,我们把两个整型字段 uid 及 age 放入进行统计。上面的查询显示的结果为:

{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"matrix_stats" : {
"doc_count" : 6,
"fields" : [
{
"name" : "uid",
"count" : 6,
"mean" : 4.5,
"variance" : 3.5,
"skewness" : 0.0,
"kurtosis" : 1.7314285714285715,
"covariance" : {
"uid" : 3.5,
"age" : 5.7
},
"correlation" : {
"uid" : 1.0,
"age" : 0.8211574455173661
}
},
{
"name" : "age",
"count" : 6,
"mean" : 25.166666666666668,
"variance" : 13.76666666666667,
"skewness" : -0.143450283024544,
"kurtosis" : 1.8030533098042432,
"covariance" : {
"uid" : 5.7,
"age" : 13.76666666666667
},
"correlation" : {
"uid" : 0.8211574455173661,
"age" : 1.0
}
}
]
}
}
}
我们也可以只得到这个年龄的最大值:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_max": {
"max": {
"field": "age"
}
}
}
}
显示的结果:

"aggregations" : {
"age_max" : {
"value" : 30.0
}
}
我们也可以在同一个请求里聚合多个指标,比如在如下的请求中,我们可以同时得到最大及最小的值:

GET twitter/_search
{
"size": 0,
"aggs": {
"Min": {
"min": {
"field": "age"
}
},
"Max": {
"max": {
"field": "age"
}
}
}
}
聚合通常适用于从聚合文档集中提取的值。 可以使用聚合体内的字段键从特定字段提取这些值,也可以使用脚本提取这些值。我们可以通过 script 的方法来对我们的 aggregtion 结果进行重新计算:

GET twitter/_search
{
"size": 0,
"aggs": {
"average_age_1.5": {
"avg": {
"field": "age",
"script": {
"source": "_value * params.correction",
"params": {
"correction": 1.5
}
}
}
}
}
}
上面的这个聚合可以帮我们计算平均值再乘以 1.5 倍的结果。运行一下的结果如下:

"aggregations" : {
"average_age_1.5" : {
"value" : 37.75
}
}
显然我们的结果是之前的 25.166666666666668 的1.5倍。

我们也可以直接使用 script 的方法来进行聚合。在这种情况下,我们可以不指定特定的field。我们可能把很多项进行综合处理,并把这个结果来进行聚合:

GET twitter/_search
{
"size": 0,
"aggs": {
"average_2_times_age": {
"avg": {
"script": {
"source": "doc['age'].value * params.times",
"params": {
"times": 2.0
}
}
}
}
}
}
在这里我们完全没有使用 field 这个项。我们直接使用 script 来形成我们的聚合:

"aggregations" : {
"average_2_times_age" : {
"value" : 50.333333333333336
}
}
Percentile aggregation
百分位数(percentile)表示观察值出现一定百分比的点。 例如,第 95 个百分位数是大于观察值的 95% 的值。该聚合针对从聚合文档中提取的数值计算一个或多个百分位数。 这些值可以从文档中的特定数字字段中提取,也可以由提供的脚本生成。

百分位通常用于查找离群值。 在正态分布中,第 0.13 和第 99.87 个百分位数代表与平均值的三个标准差。 任何超出三个标准偏差的数据通常被视为异常。这在统计的角度是非常有用的。

假如我们考虑如下的一个系列的数据:

77, 78, 85, 86, 86, 86, 87, 87, 88, 94, 99, 103, 111

在上面的 13 个数据中,它的中间位数据为 87,这是因为这个系列的数据是奇数个。我们再来考虑下面偶数个系列的数据:

77, 78, 85, 86, 86, 86, 87, 87, 94, 98, 99, 103

那么显然上面数据的中间为: (86 + 87)/2 = 86.5。

我们现在来通过一个简单的例子来展示 Percentile aggregation 的用法:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_quartiles": {
"percentiles": {
"field": "age",
"percents": [
25,
50,
75,
100
]
}
}
}
}
在上面,我们使用了以叫做 age 的字段。它是一个数值的字段。我们通过 percentile aggregation 可以得到 25%,50% 及75% 的人在什么范围。显示结果是:

"aggregations" : {
"age_quartiles" : {
"values" : {
"25.0" : 22.0,
"50.0" : 25.5,
"75.0" : 28.0,
"100.0" : 30.0
}
}
}
我们可以看到25%的人平均年龄是低于22.0岁,而50%的人的年龄是低于25.5岁,而所有的人的年龄都是低于30岁的。这里的50%的年龄和我们之前计算的平均年龄是不一样的。

GET twitter/_search
{
"size": 0,
"aggs": {
"avarage_age": {
"avg": {
"field": "age"
}
}
}
}
这个平均年龄是:

"aggregations" : {
"avarage_age" : {
"value" : 25.166666666666668
}
}
在上面,我们能够查出来,50%的人的年龄是多少,但是在实际的应用中,我们有时也很希望知道满足我们的 SLA (Service Level Aggreement) 百分比是多少,这样你可以找到自己服务的差距,比如达到一个标准的百分比是多少。针对我们的例子,我们可以使用 Percentile Ranks Aggregation。我们使用如下的查询:

GET twitter/_search
{
"size": 0,
"aggs": {
"age_40_percentage": {
"percentile_ranks": {
"field": "age",
"values": [
40
]
}
}
}
}
上面返回的结果是:

{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_40_percentage" : {
"values" : {
"40.0" : 62.5
}
}
}
}
上面的结果表明,有62.5%的人的年龄是在40岁以下的。在我们的实际应用中,比如我们输入一个指标,我们可以看出来有多少比例是在那个指标以内的。这样我们可以看出来我们的 SLA 是否满足条件。

我们可以使用 string stats 聚合来对 string 类型的数据进行统计。

GET twitter/_search
{
"size": 0,
"aggs": {
"message_stats": {
"string_stats": {
"field": "message.keyword"
}
}
}
}
上面显示的结果为:

"aggregations" : {
"message_stats" : {
"count" : 6,
"min_length" : 9,
"max_length" : 37,
"avg_length" : 18.166666666666668,
"entropy" : 5.406152357759698
}
}
它表明 message 字段的最短长度为 9,最长长度为 37,而它的平均长度为 18.17。

更为复杂的聚合
我们可以结合上面的 bucket 聚合及 metric 聚合形成更为复杂的搜索:

GET twitter/_search
{
"size": 0,
"aggs": {
"cities": {
"terms": {
"field": "city",
"order": {
"average_age": "desc"
},
"size": 5
},
"aggs": {
"average_age": {
"avg": {
"field": "age"
}
}
}
}
}
}
在上面,我们首先通过 terms 来生成每个城市的桶聚合,让后在每个桶里计算所有文档的平均年龄。在正常的情况下,这个排序是按照每个城市里文档的多少由多到少来排序的。在我们上面的搜索中,我们特意添加 average_age 来进行降序排序。这样返回的结果如下:

"aggregations" : {
"cities" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "上海",
"doc_count" : 1,
"average_age" : {
"value" : 28.0
}
},
{
"key" : "北京",
"doc_count" : 5,
"average_age" : {
"value" : 24.6
}
}
]
}
上面显示,有两个城市:上海及北京。在上海城市中有1个文档,而在北京城市里有5个文档。同时,我们也计算出来每个城市的平均年龄。由于我们使用了 average_age 来进行降排序,在我们的结果中,我们可以看到 “上海” 城市排在前面,这是因为上海城市的平均年龄比北京的平均年龄高。

Missing 聚合
我们可以通过这个聚合来统计出来缺失某个字段的文档个数。我们先添加如下的一个文档:

PUT twitter/_doc/7
{
"user": "张三",
"message": "今儿天气不错啊,出去转转去",
"uid": 2,
"city": "北京",
"province": "北京",
"country": "中国",
"address": "中国北京市海淀区",
"location": {
"lat": "39.970718",
"lon": "116.325747"
},
"DOB": "1999-04-01"
}
在上面的文档中,我们故意漏掉 age 这个字段。我们使用如下的聚合来查询有多少文档缺失 age 这个字段:

GET twitter/_search
{
"size": 0,
"aggs": {
"total_missing_age": {
"missing": {
"field": "age"
}
}
}
}
上面聚合显示的结果为:

{
"took" : 603,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"total_missing_age" : {
"doc_count" : 1
}
}
}
上面显示一个文档没有 age 这个字段。

如果你想对 aggregation 有更多的了解的话,那么可以阅读我的另外文章

Elasticsearch:aggregation介绍
Elasticsearch:pipeline aggregation 介绍
Elasticsearch:透彻理解Elasticsearch中的Bucket aggregation
Analyzer简介
我们知道 Elasticsearch 可以实现秒级的搜索速度,其中很重要的一个原因就当一个文档被存储的时候,同时它也对文档的数据进行了索引(indexing)。这样在以后的搜索中,就可以变得很快。简单地说,当一个文档进入到 Elasticsearch 时,它会经历如下的步骤:

中间的那部分就叫做 Analyzer。我们可以看出来,它分为三个部分:Char Filters, Tokenizer及 Token Filter。它们的作用分别如下:

Char Filter: 字符过滤器的工作是执行清除任务,例如剥离HTML标记。
Tokenizer: 下一步是将文本拆分为称为标记的术语。 这是由 tokenizer 完成的。 可以基于任何规则(例如空格)来完成拆分。 有关 tokennizer 的更多详细信息,请访问以下 URL:https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenizers.html。
Token filter: 一旦创建了 token,它们就会被传递给 token filter,这些过滤器会对 token 进行规范化。 Token filter 可以更改 token,删除术语或向 token 添加术语。


Elasticsearch 已经提供了比较丰富的 analyzer。我们可以自己创建自己的 token analyzer,甚至可以利用已经有的 char filter,tokenizer 及 token filter 来重新组合成一个新的 analyzer,并可以对文档中的每一个字段分别定义自己的 analyzer。如果大家对 analyzer 比较感兴趣的话,请参阅我们的网址https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-analyzers.html。

你也可以阅读我的另外一篇关于 analyzer 的文章 “Elasticsearch: analyzer”。

在默认的情况下,standard analyzer 是 Elasticsearch 的默认分析器:

没有 Char Filter
使用 standard tokonizer
把字符串变为小写,同时有选择地删除一些 stop words 等。默认的情况下 stop words 为 _none_,也即不过滤任何 stop words。


下面我们简单地展示一下我们的 analyzer 是如何实现的。

GET twitter/_analyze
{
"text": [
"Happy Birthday"
],
"analyzer": "standard"
}


原文链接:Elastic 中国社区官方博客

螃蟹在剥我的壳,笔记本在写我,漫天的我落在枫叶上雪花上,而你在想我。 --章怀柔
原文地址:https://www.cnblogs.com/lovezhr/p/15129843.html