1、什么是聚合?
聚合是对文档数据的统计、分析、计算
聚合的常见种类有哪些?
桶(Bucket)聚合:用来对文档做分组
TermAggregation:按照文档字段值分组
Date Histogram:按照日期阶梯分组,例如一周为一组,或者一月为一组
度量(Metric)聚合:用以计算一些值,比如:最大值、最小值、平均值等
AVg:求平均值
Max:求最大值
Min:求最小值
Stats:同时求max、min、avg、sum等
Pipeline:基于其它聚合结果再做聚合
参与聚合的字段类型必须是:
keyword
数值
日期
布尔
2、DSL实现Bucket聚合
可以新增在aggs内order字段以自定义排序规则,也可以在最外层新增query来限定聚合范围
aggs代表聚合,与query同级,此时query的作用是?
限定聚合的的文档范围
聚合必须的三要素:
聚合名称
聚合类型
聚合字段
聚合可配置属性有:
size:指定聚合结果数量
order:指定聚合结果排序方式
field:指定聚合字段
3、DSL实现Metrics聚合
在聚合类型同级多加一层聚合即可,如图
4、RestClient实现聚合
@Test
void testAggregation() throws IOException {
//1、准备Request
SearchRequest request = new SearchRequest("hotel");
//2、准备dsl
request.source().size(0);
request.source().aggregation(AggregationBuilders
.terms("brandAgg")
.field("brand")
.size(10)
);
//3、发出请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//4、解析结果
Aggregations aggregations = response.getAggregations();
Terms brandTerms = aggregations.get("brandAgg");
List<? extends Terms.Bucket> buckets = brandTerms.getBuckets();
for (Terms.Bucket bucket : buckets) {
String keyAsString = bucket.getKeyAsString();
System.out.println(keyAsString);
}
}
封装请求信息逐层封装,解析相应结果逐层解析即可
注意返回值类型与方法传参,ctrl+p可以提示方法所需形参类型
多条件聚合只需增加aggregations,并在取出结果时逐一拆分即可
@Override
public Map<String, List<String>> filters() {
try {
SearchRequest request = new SearchRequest("hotel");
Map<String, List<String>> result = new HashMap<>();
buildAggregation(request);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Aggregations aggregations = response.getAggregations();
List<String> brandlist = getAggByName(aggregations,"brandAgg");
result.put("品牌",brandlist);
result.put("星级",getAggByName(aggregations,"starNameAgg"));
result.put("城市",getAggByName(aggregations,"cityAgg"));
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<String> getAggByName(Aggregations aggregations,String aggName) {
Terms brandterms = aggregations.get(aggName);
List<String> brandlist = new ArrayList<>();
List<? extends Terms.Bucket> buckets = brandterms.getBuckets();
for (Terms.Bucket bucket : buckets) {
brandlist.add(bucket.getKeyAsString());
}
return brandlist;
}
private static void buildAggregation(SearchRequest request) {
request.source().aggregation(AggregationBuilders
.terms("brandAgg")
.size(100)
.field("brand"));
request.source().aggregation(AggregationBuilders
.terms("cityAgg")
.size(100)
.field("city"));
request.source().aggregation(AggregationBuilders
.terms("starNameAgg")
.size(100)
.field("starName"));
}