请求方式
POST
API
http://{{es_ip}}:9200/user/_search
参数
{
"from": 0,
"size" : 1, // 设置size=0的意思就是,仅返回聚合查询结果,不返回普通query查询结果。
"aggs" : { // 聚合查询语句的简写
"group_by_bu" : { // 给聚合查询取个名字,叫popular_colors
"terms" : { // 聚合类型为,terms,terms是桶聚合的一种,类似SQL的group by的作用,根据字段分组,相同字段值的文档分为一组。
"field" : "bu" // terms聚合类型的参数,这里需要设置分组的字段为color,根据color分组
},
"aggs":{
"group_by_entityType" : { // 给聚合查询取个名字,叫popular_colors
"terms" : { // 聚合类型为,terms,terms是桶聚合的一种,类似SQL的group by的作用,根据字段分组,相同字段值的文档分为一组。
"field" : "entityType" // terms聚合类型的参数,这里需要设置分组的字段为color,根据color分组
}
}
}
}
}
}
响应
{
"took": 139,
"timed_out": false,
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 1.0,
},
"aggregations": {
"group_by_bu": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "bu1",
"doc_count": 1549255,
"group_by_entityType": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 272,
"buckets": [
{
"key": "上传测试19",
"doc_count": 10
},
{
"key": "上传测试8",
"doc_count": 10
}
]
}
},
{
"key": "bu2",
"doc_count": 131065,
"group_by_entityType": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "销售自拓",
"doc_count": 131054
},
{
"key": "上传测试241",
"doc_count": 2
}
]
}
}
]
}
}
}