ES之聚合查询

ES之聚合查询,第1张

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
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5689055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存