ES聚合查询基本语法
程序员文章站
2022-07-06 15:47:29
...
1、创造数据
POST /test_aggs/infos/1?pretty
{
"title":"test class 01",
"price": 20,
"desc": ["good","very","handsome"]
}
POST /test_aggs/infos/2?pretty
{
"title":"test class 02",
"price": 30,
"desc": ["good2","very","handsome"]
}
POST /test_aggs/infos/3?pretty
{
"title":"test class 03",
"price": 50,
"desc": ["good3","very2","handsome"]
}
2、构建聚合函数语句
ES聚合的基本语法
## 示例1:
POST /test_aggs/infos/_search?pretty
{
"size":0,
"query":{"match_all":{}},
"aggs":{
"aggs_terms":{
"min":{
"field":"price"
}
}
}
}
## 示例2:
PUT /test_aggs/_mapping/infos
{
"properties": {
"title": {
"type": "text",
"fielddata": true
}
}
}
POST /test_aggs/infos/_search?pretty
{
"size":0,
"query":{"match_all":{}},
"aggs":{
"aggs_terms":{
"terms":{
"field":"title"
},
"aggs":{
"aggs_avg":{
"avg":{
"field":"price"
}
}
}
}
}
}
结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"aggs_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "class",
"doc_count": 3,
"aggs_avg": {
"value": 33.333333333333336
}
},
{
"key": "test",
"doc_count": 3,
"aggs_avg": {
"value": 33.333333333333336
}
},
{
"key": "01",
"doc_count": 1,
"aggs_avg": {
"value": 20
}
},
{
"key": "02",
"doc_count": 1,
"aggs_avg": {
"value": 30
}
},
{
"key": "03",
"doc_count": 1,
"aggs_avg": {
"value": 50
}
}
]
}
}
}
## 示例3:
POST /test_aggs/infos/_search?pretty
{
"size":0,
"query":{"match_all":{}},
"aggs":{
"aggs_terms":{
"stats":{
"field":"price"
}
}
}
}
上一篇: ES基础查询语法
下一篇: 单机到集群安装cdh5.16