目录
一. 分桶聚合 Bucket
GET /product/_search
{
"size": 0,
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword",
"size": 5
}
}
}
}
二. 指标聚合 metric
GET /product/_search
{
"size": 0,
"aggs": {
"max_agg": {
"max": {
"field": "price"
}
},
"min_agg": {
"min": {
"field": "price"
}
},
"stats_agg": {
"stats": {
"field": "price"
}
}
}
}
三. 去重聚合
GET /product/_search
{
"size": 0,
"aggs": {
"distinct_name": {
"cardinality": {
"field": "name.keyword"
}
}
}
}
四. 管道聚合 pepiline
GET /product/_search
{
"size": 0,
"aggs": {
"lv_aggs": {
"terms": {
"field": "lv.keyword"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"min_avg_price": {
"min_bucket": {
"buckets_path": "lv_aggs>avg_price"
}
}
}
}
五. 嵌套聚合
GET /product/_search
{
"size": 0,
"aggs": {
"tyep_agg": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"type_lv_agg": {
"terms": {
"field": "lv.keyword"
}
}
}
}
}
}
GET /product/_search
{
"size": 0,
"aggs": {
"type_agg": {
"terms": {
"field": "type.keyword"
},
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"min_avg_price": {
"min_bucket": {
"buckets_path": "lv_agg>avg_price"
}
}
}
}
}
}
六. 基于查询结果的聚合
GET /product/_search
{
"query": {
"match": {
"name": "xiaomi"
}
},
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword"
}
}
}
}
七. 不影响聚合结果的查询
GET /product/_search
{
"size": 0,
"query": {
"constant_score": {
"filter": {
"range": {
"price": {
"gte": 3000
}
}
},
"boost": 1.2
}
},
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
},
"all_price_stats": {
"global": {},
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
}
}
}
}
八. 基于集合结果的查询
GET /product/_search
{
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword",
"size": 10
}
}
},
"post_filter": {
"term": {
"type.keyword": "erji"
}
}
}
九. 多重聚合中指定查询条件
GET /product/_search
{
"size": 0,
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
},
"all_price_stats": {
"filter": {
"range": {
"price": {
"gte": 4500
}
}
},
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
}
}
}
}
十. 多重聚合中组合查询条件
GET /product/_search
{
"query": {
"match": {
"name": "xiaomi"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"bool_avg_price": {
"filter": {
"range": {
"price": {
"lt": 4000
}
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
十一. 多重聚合中阻断查询条件
GET /product/_search
{
"size": 0,
"query": {
"match": {
"name": "xiaomi"
}
},
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
},
"all_price_stats": {
"global": {},
"aggs": {
"stats_price": {
"stats": {
"field": "price"
}
}
}
}
}
}
十二. 聚合排序
GET /product/_search
{
"size": 0,
"aggs": {
"lv_agg": {
"terms": {
"field": "lv.keyword",
"order": {
"_count": "desc"
}
}
}
}
十三. 嵌套聚合排序
GET /product/_search
{
"size": 0,
"aggs": {
"lv_bucket": {
"terms": {
"field": "lv.keyword",
"order": {
"_count": "asc"
}
},
"aggs": {
"type_bucket": {
"terms": {
"field": "type.keyword",
"order": {
"_count": "desc"
}
}
}
}
}
}
}
十三. 多级聚合排序
GET /product/_search
{
"size": 0,
"aggs": {
"lv_bucket": {
"terms": {
"field": "lv.keyword",
"order": {
"lv_stats_agg>price_stats.avg": "asc"
}
},
"aggs": {
"lv_stats_agg": {
"filter": {
"range": {
"price": {
"gte": 1000
}
}
},
"aggs": {
"price_stats": {
"stats": {
"field": "price"
}
}
}
}
}
}
}
}