1.根据字段num分组,汇总条数(例)
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下
{
"aggs" : { //聚合操作
"num_group" : { //聚合名称,随意起
"terms" : { //分组
"field" : "num" //分组字段
}
}
},
"size" : 0 //由于会返回原始数据,这里将hits里返回的原始数据变为0
}
返回结果为:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"num_group": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 5,
"doc_count": 4
},
{
"key": 1,
"doc_count": 1
},
{
"key": 2,
"doc_count": 1
},
{
"key": 3,
"doc_count": 1
},
{
"key": 4,
"doc_count": 1
}
]
}
}
}
2.求字段num的平均值(例)
【GET】请求:http://127.0.0.1:9200/test-index-1/_search,参数如下
{
"aggs" : {
"num_avg" : {
"avg" : {
"field" : "num"
}
}
},
"size" : 0
}
返回结果为:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 8,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"num_avg": {
"value": 3.75
}
}
}



