您当前的位置: 首页 >  http

java持续实践

暂无认证

  • 2浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

es 7.x http 聚合统计查询

java持续实践 发布时间:2022-01-09 19:30:15 ,浏览量:2

文章目录
      • 聚合查询

聚合查询

对价格进行分组查询 请求方式get 请求url: http://127.0.0.1:9200/shopping/_search 请求体:

{
   "aggs": {
     "price_group":{
		"terms": {
			"field": "price"
		}
	 }
   }
}

aggs : 代表是进行聚合操作 price_group: 是聚合操作的名称, 自己的起的 terms: 代表分组操作 field: 代表分组的字段. 查询的结果如下

{
    "took": 50,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SJtEPn4BwYTyfKb722JN",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 6999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SZtEPn4BwYTyfKb78GIE",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 7999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SptFPn4BwYTyfKb7AGLp",
                "_score": 1.0,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 1999.00
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999.0,
                    "doc_count": 3
                },
                {
                    "key": 1.0,
                    "doc_count": 1
                },
                {
                    "key": 1999.0,
                    "doc_count": 1
                },
                {
                    "key": 6999.0,
                    "doc_count": 1
                },
                {
                    "key": 7999.0,
                    "doc_count": 1
                }
            ]
        }
    }
}

可以看到在aggregations 对象中, 有分组的结果返回. 价格为3999.0的有三个, 其余价格的只有一个.

但上面的结果中, 还有一个问题是, 除了分组的结果 ,还把所有的文档数据返回了, 可能我们是不需要文档的数据的, 可以进行如下的写法

{
   "aggs": {
     "price_group":{
		"terms": {
			"field": "price"
		}
	 }
   },
   "size": 0
}

"size": 0 不进行文档的返回, 结果如下 . 只有统计的结果 , 没有文档的数据.

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 3999.0,
                    "doc_count": 3
                },
                {
                    "key": 1.0,
                    "doc_count": 1
                },
                {
                    "key": 1999.0,
                    "doc_count": 1
                },
                {
                    "key": 6999.0,
                    "doc_count": 1
                },
                {
                    "key": 7999.0,
                    "doc_count": 1
                }
            ]
        }
    }
}

查询所有价格的平均值写法:

{
   "aggs": {
     "price_avg":{
		"avg": {
			"field": "price"
		}
	 }
   },
   "size": 0
}

terms改成avg . 分组名称改成 price_avg

结果如下 :

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 4142.142857142857
        }
    }
}

可以看到结果为4142.142857142857

黑发不知勤学早,白首方悔读书迟。

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0412s