栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ES之聚合查询

ES之聚合查询

ES之聚合查询

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
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/688543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号