ES聚合查询

请求方式

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
                            }
                        ]
                    }
                }
            ]
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容