elasticsearch过滤聚合(Keyword和Nested聚合),先过滤,再根据过滤结果聚合,并排序
elasticsearch分类聚合
先模糊检索过滤后,再对结果聚合
1. 对普通字段或数组类型聚合(默认按聚合数量排序)
例子:对Keyword类型字段sponsor聚合,先进行模糊检索,再对检索的结果聚合
{
"size": 0,
"query": {
"bool": {
"must": {
"wildcard": {
"sponsor": "*University*"
}
}
}
},
"aggs": {
"sponsor": {
"terms": {
"field": "sponsor",
"size":100
}
}
}
}
2. 对nested类型字段聚合(默认按聚合数量排序)
例子:对nested类型字段researchAreas对象的subjectName聚合,先进行模糊检索,再对检索的结果聚合
{
"size": 0,
"query": {
"bool": {
"must": [
{
"nested": {
"path": "researchAreas",
"query": {
"bool": {
"must": {
"wildcard": {
"researchAreas.subjectName": "*and*"
}
}
}
}
}
}
]
}
},
"aggs": {
"researchAreas_aggs": {
"nested": {
"path": "researchAreas"
},
"aggs": {
"filter_term": {
"filter": {
"wildcard": {
"researchAreas.subjectName": "*and*"
}
},
"aggs": {
"subjectName": {
"terms": {
"field": "researchAreas.subjectName",
"size": 100
}
}
}
}
}
}
}
}
3. 引入order对象,在例子1的基础上,添加自主排序
例子:按聚合词项字符串的字母顺序排序
{
"size": 0,
"query": {
"bool": {
"must": {
"wildcard": {
"sponsor": "*University*"
}
}
}
},
"aggs": {
"sponsor": {
"terms": {
"field": "sponsor",
"size": 100,
"order":{
"_term":"asc"
}
}
}
}
}