對(duì)搜索結(jié)果進(jìn)行聚合
對(duì) gucci 品牌的 color 進(jìn)行聚合
GET /shirts/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "brand": "gucci" }}
]
}
},
"aggs": {
"models": {
"terms": { "field": "color" }
}
}
}
對(duì)聚合結(jié)果進(jìn)行過濾
對(duì) gucci 品牌的 color 進(jìn)行聚合,同時(shí)對(duì) gucci 品牌的 red 顏色的 model 進(jìn)行聚合
GET /shirts/_search
{
"query": {
"bool": {
"filter": {
"term": { "brand": "gucci" }
}
}
},
"aggs": {
"colors": {
"terms": { "field": "color" }
},
"color_red": {
"filter": {
"term": { "color": "red" }
},
"aggs": {
"models": {
"terms": { "field": "model" }
}
}
}
}
}
使用post_filter,可以使聚合不受過濾影響,適用于聚合條件與過濾條件不一致的情況
對(duì) gucci 品牌的 color 進(jìn)行聚合,并且返回過濾 red 顏色的數(shù)據(jù)
GET /shirts/_search
{
"query": {
"bool": {
"filter": {
"term": { "brand": "gucci" }
}
}
},
"aggs": {
"colors": {
"terms": { "field": "color" }
}
},
"post_filter": {
"term": { "color": "red" }
}
}