GET mytest/doc/_search
{
"query": { ... },
"size": 0,
"aggs": {
"custom_name1": { //aggs後面接著的是一個自定義的name
"桶": { ... } //再來才是接桶
},
"custom_name2": { //一個aggs裡可以有很多聚合
"桶": { ... }
},
"custom_name3": {
"桶": {
.....
},
"aggs": { //aggs可以嵌套在別的aggs裡面
"in_name": { //記得使用aggs需要先自定義一個name
"桶": { ... } //in_name的桶作用的文檔是custom_name3的桶的結果
}
}
}
}
}
{
"hits": {
"total": 8,
"max_score": 0,
"hits": [] //因為size設為0,所以沒有查詢結果返回
},
"aggregations": {
"custom_name1": {
...
},
"custom_name2": {
...
},
"custom_name3": {
... ,
"in_name": {
....
}
}
}
}
{ "color": "red" }
{ "color": "green" }
{ "color": ["red", "blue"] }
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"terms": {
"field": "color", //使用color來進行分組
}
}
}
}
{"color": ["red", "blue"]}
就同時符合了 red 和 blue bucket
"aggregations": {
"my_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ //terms桶產生的buckets數組,裡面每個json對象都是一個bucket
{
"key": "blue",
"doc_count": 1
},
{
"key": "red",
//表示color為red的文檔有2個,此例中就是 {"color": "red"} 和 {"color": ["red", "blue"]}這兩個文檔
"doc_count": 2
},
{
"key": "green",
"doc_count": 1
}
]
}
}
min_doc_count
限制當 doc 數大於等於 n 筆時才會被搜出來,也可以帶上 size
參數,指名要顯示 bucket 中多少筆數據,默認是 10
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"terms": {
"field": "color",
"size": 100, //可以設置size,指定返回的桶數量,預設是10,如果總共桶數不超過100,那就會全部返回
"min_doc_count": 2 //bucket中只有當doc_count >= 2的人,才會被搜出來
}
}
}
}
"aggregations": {
"my_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ //因為設置了min_doc_count:2,所有只有red桶被搜出來
{
"key": "red",
"doc_count": 2
}
]
}
}
{ "color": "red", "price": 100 }
{ "color": "green", "price": 500 }
{ "color": ["red", "blue"], "price": 1000 }
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"terms": {
"field": "color"
},
"aggs": { //嵌套兩個指標avg、min在terms桶中
"my_avg_price": { //my_avg_price計算每個bucket的平均price
"avg": {
"field": "price"
}
},
"my_min_price": { //my_min_price計算每個bucket中的最小price
"min": {
"field": "price"
}
}
}
}
}
}
"aggregations": {
"my_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [ //terms桶中的每個bucket都會計算avg和min兩個指標
{
"key": "blue",
"doc_count": 1,
"my_avg_price": { //avg指標
"value": 1000
},
"my_min_price": { //min指標
"value": 100
}
},
{
"key": "red",
"doc_count": 2,
//avg指標計算的值,因為符合color為red的文檔有兩筆,所以平均price為100+1000/2 = 550
"my_avg_price": {
"value": 550
},
"my_min_price": {
"value": 100
}
},
{
"key": "green",
"doc_count": 1,
"my_avg_price": {
"value": 500
},
"my_min_price": {
"value": 500
}
}
]
}
}
include
和 exclude
參數來設定哪些 bucket 的值要聚合,哪些 bucket 的值不聚合
include
和 exclude
都支持設置特定值、數組、以及正則{ "color": ["red", "green"] }
{ "color": ["red", "blue"] }
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"terms": {
"field": "color",
"include": "red" //include也支持數組和正則
}
}
}
}
"aggregations": {
"my_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
//本來是有三個bucket的,但是因為設置了include,所以只聚合計算了一個bucket red
"buckets": [
{
"key": "red",
"doc_count": 2
}
]
}
}
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"filter": { //此處是一個filter桶,因為他用法跟一般的過濾filter一樣,所以也能使用bool嵌套
"bool": {
"must": {
"terms": { //注意此terms是查找terms,不是terms桶
"color": [ "red", "blue" ]
}
}
}
}
}
}
}
"aggregations": {
"my_name": {
"doc_count": 2 //filter桶計算出來的文檔數量
}
}
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": { //my_name聚合
"filter": { //filter桶
"bool": {
"must": {
"terms": {
"color": [ "red", "blue" ]
}
}
}
},
"aggs": {
"my_name2": { //my_name2聚合,嵌套在my_name聚合裡
"terms": { //terms桶
"field": "color"
}
}
}
}
}
}
{"color": "red"}
以及{"color": ["red", "blue"]}
,所以 terms 桶只會對這兩筆文檔做分組"aggregations": {
"my_name": {
"doc_count": 2, //filter桶計算的數量,通過此處的文檔只有2筆
"my_name2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "red",
"doc_count": 2 //terms桶計算的數量
},
{
"key": "blue",
"doc_count": 1 //terms桶計算的數量
}
]
}
}
}
doc_value
進行降序排序,也就是說哪個生成桶的 doc_value
文檔數較多,哪個生成桶就排在前面
order
進行排序order
支持的參數
_count
: 按照文檔數排序_term
: 按照每個桶的字符串值的字母順序排序{ "color": "red", "price": 100 }
{ "color": ["red", "blue"], "price": 1000 }
GET mytest/doc/_search
{
"query": {
"match_all": {}
},
"size": 0,
"aggs": {
"my_name": {
"terms": {
"field": "color",
"order": {
"_term": "asc"
}
}
}
}
}
"aggregations": {
"my_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "blue",
"doc_count": 1
},
{
"key": "red",
"doc_count": 2
}
]
}
}