聚合查询
-
# 聚合查询
GET /lib3/user/_search
{
"size": 0, #
"aggs": {
"price_of_sum": { # 取名
"sum": {
"field": "price"
}
}
}
} -
min
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"price_of_min": {
"min": {
"field": "price"
}
}
}
} -
max
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"price_of_max": {
"max": {
"field": "price"
}
}
}
} -
avg
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"price_of_avg": {
"avg": {
"field": "price"
}
}
}
} -
cardinality:求基数(去重后统计总数)
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"price_of_cardinality": {
"cardinality": {
"field": "price"
}
}
}
}
# 执行结果
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"price_of_cardinality": {
"value": 4
}
}
} -
terms:分组
GET /lib3/user/_search
{
"size": 0,
"aggs": {
"price_of_group": {
"terms": {
"field": "price"
}
}
}
}
# 执行结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0,
"hits": []
},
"aggregations": {
"price_of_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 25,
"doc_count": 1
},
{
"key": 30,
"doc_count": 1
},
{
"key": 40,
"doc_count": 1
},
{
"key": 50,
"doc_count": 1
}
]
}
}
} -
综合题: 对哪些有唱歌兴趣的用户按年龄分组
GET /lib2/user/_search
{
"query":{
"match":{
"interests":"锻炼"
}
},
"aggs":{
"age_of_group":{
"terms":{
"field":"age"
}
}
}
} -
对哪些有唱歌兴趣的用户按年龄分组,然后求年龄平均值
{
"query":{
"match":{
"interests":"锻炼"
}
},
"aggs":{
"age_of_group":{
"terms":{
"field":"age"
},
"aggs": {
"age_of_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}
# 执行结果
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.7549128,
"hits": [
{
"_index": "lib2",
"_type": "user",
"_id": "2",
"_score": 0.7549128,
"_source": {
"name": "赵明",
"adress": "北京海淀区清河",
"age": 20,
"birthday": "1998-10-12",
"interests": "喜欢喝酒,锻炼,唱歌"
}
},
{
"_index": "lib2",
"_type": "user",
"_id": "3",
"_score": 0.2876821,
"_source": {
"name": "lisi",
"adress": "北京海淀区清河",
"age": 23,
"birthday": "1998-10-12",
"interests": "喜欢喝酒,锻炼,唱歌"
}
},
{
"_index": "lib2",
"_type": "user",
"_id": "1",
"_score": 0.2824934,
"_source": {
"name": "赵六",
"adress": "黑龙江铁岭",
"age": 50,
"birthday": "1970-10-12",
"interests": "喜欢喝酒,锻炼,说相声"
}
}
]
},
"aggregations": {
"age_of_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 20,
"doc_count": 1,
"age_of_avg": {
"value": 20
}
},
{
"key": 23,
"doc_count": 1, "age_of_avg": { "value": 23 } }, { "key": 50, "doc_count": 1, "age_of_avg": { "value": 50 } } ] } }} -
对哪些有唱歌兴趣的用户按年龄分组,再对年龄求平均值,并按平均年龄降序
GET /lib2/user/_search
{
"size": 0,
"query": {
"match": {
"interests": "锻炼"
}
}
, "aggs": {
"age_of_group": {
"terms": {
"field": "age",
"order": {
"age_of_avg": "desc"
}
},
"aggs": {
"age_of_avg": {
"avg": {
"field": "age"
}
}
}
}
}
}
# 查询结果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"age_of_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 50,
"doc_count": 1,
"age_of_avg": {
"value": 50
}
},
{
"key": 23,
"doc_count": 1,
"age_of_avg": {
"value": 23
}
},
{
"key": 20,
"doc_count": 1,
"age_of_avg": {
"value": 20
}
}
]
}
}
}