工作中遇到需要统计分组数量的查询,记录一下
GET /fy_roomel/_search
{
"size": 0,
"aggs": {
"groupNAME": {
"terms": {
"field": "district.keyword"
}
},
"count":{
"cardinality": {
"field": "district.keyword"
}
}
}
}
ps: 按区域分组, 统计有几个区域
结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 145,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"groupNAME" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "包河区",
"doc_count" : 87
},
{
"key" : "瑶海区",
"doc_count" : 29
},
{
"key" : "津南区",
"doc_count" : 26
},
{
"key" : "蜀山区",
"doc_count" : 3
}
]
},
"count" : {
"value" : 4
}
}
}