一、请求方式:Get
二、请求URL:
http://127.0.0.1:9200/megacorp/employee/_search
三、
1.按年龄分组
(1)请求信息:
{
"aggs" : { // 聚合操作
"age_group" : { // 统计结果名称
"terms" : { // 分组
"field" : "age" // 分组字段
}
}
}
}
(2)响应信息
{
"took": 86,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 1.0,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "2",
"_score": 1.0,
"_source": {
"first_name": "Jane",
"last_name": "Smith",
"age": 32,
"about": "I like to collect rock albums",
"interests": [
"music"
]
}
},
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_score": 1.0,
"_source": {
"first_name": "Douglas",
"last_name": "Fir",
"age": 35,
"about": "I like to build cabinets",
"interests": [
"forestry"
]
}
}
]
},
"aggregations": {
"price_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 25,
"doc_count": 1
},
{
"key": 32,
"doc_count": 1
},
{
"key": 35,
"doc_count": 1
}
]
}
}
}
2.
(1)请求信息:
{
"aggs" : { // 聚合操作
"age_avg" : { // 统计结果名称
"avg" : { // 求平均值
"field" : "age" // 求平均值字段
}
}
},
"size": 0
}
(2)响应信息:
{
"took": 50,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"age_avg": {
"value": 30.666666666666668
}
}
}