Elasticsearch Aggregation 聚合查询
6.1,使用
#创建索引
PUT /fruit
{
“mappings”: {
“properties”: {
“title”: {
“type”: “keyword”
},
“price”: {
“type”: “double”
},
“description”:{
“type”: “text”
}
}
}
}
#添加数据
PUT /fruit/_bulk
{“index”:{}}
{“title”:“面包”,“price”:19.9,“description”:“小面包非常好吃”}
{“index”:{}}
{“title”:“旺仔牛奶”,“price”:29.9,“description”:“非常好喝”}
{“index”:{}}
{“title”:“日本豆”,“price”:19.9,“description”:“日本豆非常好吃”}
{“index”:{}}
{“title”:“小馒头”,“price”:19.9,“description”:“小馒头非常好吃”}
{“index”:{}}
{“title”:“大辣片”,“price”:39.9,“description”:“大辣片非常好吃”}
{“index”:{}}
{“title”:“透心凉”,“price”:9.9,“description”:“透心凉非常好喝”}
{“index”:{}}
{“title”:“小浣熊”,“price”:19.9,“description”:“童年的味道”}
{“index”:{}}
{“title”:“海苔”,“price”:19.9,“description”:“海的味道”}
6.2,根据某个字段进行分组,统计数量
GET /fruit/_search
{
“query”: {
“term”: {
“description”: {
“value”: “味”
}
}
},
“aggs”: {
“price_group”: {
“terms”: {
“field”: “price”
}
}
}
}
6.3,只返回聚合数量,不返回结果 “size”:0
GET /fruit/_search
{
“query”: {
“term”: {
“description”: {
“value”: “味”
}
}
},
“size”: 0,
“aggs”: {
“price_group”: {
“terms”: {
“field”: “price”
}
}
}
}
6.4,求最大值
GET /fruit/_search
{
“query”: {
“match_all”: {}
},
“aggs”: {
“max_price”: {
“max”: {
“field”: “price”
}
}
}
}
6.5,求最小值
GET /fruit/_search
{
“query”: {
“match_all”: {}
},
“size”: 0,
“aggs”: {
“min_price”: {
“min”: {
“field”: “price”
}
}
}
}
6.6,求和
GET /fruit/_search
{
“query”: {
“match_all”: {}
},
“size”: 0,
“aggs”: {
“sum_price”: {
“sum”: {
“field”: “price”
}
}
}
}