示例1
创建索引和导入数据
创建索引:
PUT student
{
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
},
"height": {
"type": "integer"
}
}
}
}
使用 _bulk
创建多个文档:
POST _bulk
{ "index" : { "_index" : "student", "_id" : "1" } }
{ "name" : "张三", "age": 12 }
{ "index" : { "_index" : "student", "_id" : "2" } }
{ "name" : "李四", "age": 10, "height": 112 }
{ "index" : { "_index" : "student", "_id" : "3" } }
{ "name" : "王五", "age": 11, "height": 108 }
{ "index" : { "_index" : "student", "_id" : "4" } }
{ "name" : "陈六", "age": 11, "height": 111 }
一共4条数据,其中张三
没有身高 height 的数据。
查询所有数据
# 请求
GET student/_search
# 响应
... 省略
总人数
# 请求
POST student/_count
# 响应
{
"count" : 4,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
11 岁的学生总人数
方法1:
# 请求
POST student/_count
{
"query": {
"bool": {
"must": [
{
"term": {"age": 11}
}
]
}
}
}
# 响应
{
"count" : 2,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
方法2:查询后进行聚合:
# 请求
POST student/_search
{
"query": {
"bool": {
"must": [
{"term": {"age": 11} }
]
}
},
"aggs":{
"age_count": {
"terms": {"field": "age"}
}
},
"size": 0
}
# 响应
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 2
}
]
}
}
}
各个年龄的人数分布
方法1:
# 请求
POST student/_search
{
"aggs":{
"age_count": {
"terms": {"field": "age"}
}
},
"size": 0
}
# 响应
{
"took" : 71,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_count" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 2
},
{
"key" : 10,
"doc_count" : 1
},
{
"key" : 12,
"doc_count" : 1
}
]
}
}
}
可以看到,11 岁的有2个,10岁的1个,12岁的1个。
注意,在 ES 的关键词中 aggs 和 aggregations 等价。
方式2:
# 请求
POST student/_search
{
"size": 0,
"aggregations": {
"group_by_age": {
"aggregations": {
"count_age": {
"value_count": {
"field": "_index"
}
}
},
"terms": {
"field": "age"
}
}
}
}
# 响应
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 2,
"count_age" : {
"value" : 2
}
},
{
"key" : 10,
"doc_count" : 1,
"count_age" : {
"value" : 1
}
},
{
"key" : 12,
"doc_count" : 1,
"count_age" : {
"value" : 1
}
}
]
}
}
}
各个身高的人数分布
# 请求
POST student/_search
{
"aggs":{
"group_by_height": {
"terms": {"field": "height"}
}
},
"size": 0
}
# 响应
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_height" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 108,
"doc_count" : 1
},
{
"key" : 111,
"doc_count" : 1
},
{
"key" : 112,
"doc_count" : 1
}
]
}
}
}
无身高数据的张三
未被统计。
学生的平均年龄、最小年龄、最大年龄、年龄之和
方式1:
# 请求
POST student/_search
{
"aggs":{
"age_stat": {
"stats": {"field": "age"}
}
},
"size": 0
}
# 响应
{
"took" : 45,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_stat" : {
"count" : 4,
"min" : 10.0,
"max" : 12.0,
"avg" : 11.0,
"sum" : 44.0
}
}
}
stats
指令,会计算出指定字段的 count、min、max、avg、sum。
方式2:
# 请求
POST student/_search
{
"aggs":{
"age_avg": {
"avg": {"field": "age"}
},
"age_sum": {
"sum": {"field": "age"}
},
"age_min": {
"min": {"field": "age"}
},
"age_max": {
"max": {"field": "age"}
},
"age_count": {
"value_count": {"field": "age"}
}
},
"size": 0
}
# 响应
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"age_sum" : {
"value" : 44.0
},
"age_min" : {
"value" : 10.0
},
"age_avg" : {
"value" : 11.0
},
"age_count" : {
"value" : 4
},
"age_max" : {
"value" : 12.0
}
}
}
每个年龄的平均身高是多少?
# 请求
POST student/_search
{
"size": 0,
"aggregations": {
"group_by_age": {
"aggregations": {
"avg_height": {
"avg": {
"field": "height"
}
}
},
"terms": {
"field": "age"
}
}
}
}
# 响应
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 11,
"doc_count" : 2,
"avg_height" : {
"value" : 109.5
}
},
{
"key" : 10,
"doc_count" : 1,
"avg_height" : {
"value" : 112.0
}
},
{
"key" : 12,
"doc_count" : 1,
"avg_height" : {
"value" : null
}
}
]
}
}
}
获取每个年龄的平均身高,并按照年龄从小到大排序
方式1:
# 请求
POST student/_search
{
"size": 0,
"aggregations": {
"group_by_age": {
"aggregations": {
"avg_height": {
"avg": {
"field": "height"
}
}
},
"terms": {
"field": "age",
"order": {
"_term": "asc"
}
}
}
}
}
# 响应 (响应中指出 _term 已经废弃,应使用 _key)
#! Deprecation: Deprecated aggregation order key [_term] used, replaced by [_key]
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 10,
"doc_count" : 1,
"avg_height" : {
"value" : 112.0
}
},
{
"key" : 11,
"doc_count" : 2,
"avg_height" : {
"value" : 109.5
}
},
{
"key" : 12,
"doc_count" : 1,
"avg_height" : {
"value" : null
}
}
]
}
}
}
方式2:
POST student/_search
{
"size": 0,
"aggregations": {
"group_by_age": {
"aggregations": {
"avg_height": {
"avg": {
"field": "height"
}
},
"bucket_sort_by_avg_height": {
"bucket_sort": {
"sort": [
{"_key": {"order": "asc"}}
]
}
}
},
"terms": {
"field": "age"
}
}
}
}
获取每个年龄的平均身高,并按照平均身高从大到小排序
# 请求
POST student/_search
{
"size": 0,
"aggregations": {
"group_by_age": {
"aggregations": {
"avg_height": {
"avg": {
"field": "height"
}
},
"bucket_sort_by_avg_height": {
"bucket_sort": {
"sort": [
{"avg_height": {"order": "desc"}}
]
}
}
},
"terms": {
"field": "age"
}
}
}
}
# 响应
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"group_by_age" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 10,
"doc_count" : 1,
"avg_height" : {
"value" : 112.0
}
},
{
"key" : 11,
"doc_count" : 2,
"avg_height" : {
"value" : 109.5
}
}
]
}
}
}