业务需求
在搜索页面的顶部,包含很多过滤项:
分类、品牌这些过滤项应该从索引库中聚合得到。
接口说明:
controller层:
/**
* 过滤项聚合
*/
@PostMapping("/filters")
public Map<String,List<String>> filters (@RequestBody RequestParams params){
log.info("搜索栏自动补全===前后端联通==" + params);
return isearchService.filters(params);
}
service层:文章来源:https://www.toymoban.com/news/detail-505357.html
/**
* 过滤项聚合
*/
Map<String, List<String>> filters(RequestParams params);
service实现类:文章来源地址https://www.toymoban.com/news/detail-505357.html
/**
* 过滤项聚合
*
* @param params
*/
@Override
public Map<String, List<String>> filters(RequestParams params) {
try {
// 1.准备Request
SearchRequest request = new SearchRequest("item");
// 2.准备DSL
// 2.1.设置查询条件
buildBasicQuery(params, request);
// 2.2.设置size
request.source().size(0);//不在结果中包含文档详情
// 2.3.聚合
request.source().aggregation(AggregationBuilders
.terms("brandAgg")
.field("brand")
.size(50)
);
request.source().aggregation(AggregationBuilders
.terms("categoryAgg")
.field("category")
.size(50)
);
// 3.发出请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//获取品牌
List<String> brandList = getAggByName(response, "brandAgg");
List<String> cityList = getAggByName(response, "categoryAgg");
//响应结果
Map<String, List<String>> map = new HashMap<>();
map.put("brand", brandList);
map.put("category", cityList);
return map;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 获取聚合结果
*
* @param response
* @param aggName
* @return
*/
private List<String> getAggByName(SearchResponse response, String aggName) {
List<String> brandList = new ArrayList<>();
Aggregations aggregations = response.getAggregations();
// 4.1.根据聚合名称获取聚合结果
Terms brandTerms = aggregations.get(aggName);
// 4.2.获取buckets
List<? extends Terms.Bucket> buckets = brandTerms.getBuckets();
// 4.3.遍历
for (Terms.Bucket bucket : buckets) {
// 4.4.获取key
String key = bucket.getKeyAsString();
brandList.add(key);
}
return brandList;
}
/**
* 查询条件
*
* @param params
* @param request
*/
private void buildBasicQuery(RequestParams params, SearchRequest request) {
// 1.构建BooleanQuery
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 2.关键字搜索
if (!StringUtils.isBlank(params.getKey())) {
boolQuery.must(QueryBuilders.matchQuery("all", params.getKey()));
} else {
boolQuery.must(QueryBuilders.matchAllQuery());
}
//过滤
//品牌
if (!StringUtils.isBlank(params.getBrand())) {
boolQuery.filter(QueryBuilders.termQuery("brand", params.getBrand()));
}
//分类
if (!StringUtils.isBlank(params.getCategory())) {
boolQuery.filter(QueryBuilders.termQuery("category", params.getCategory()));
}
//价格
if (params.getMinPrice() != null && params.getMaxPrice() != null) {
boolQuery.filter(QueryBuilders.rangeQuery("price").gte(params.getMinPrice()).lte(params.getMaxPrice()));
}
//复合查询——算分函数查询
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(
boolQuery, //原始查询
new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
QueryBuilders.termQuery("isAD", true), //过滤条件(符合条件的文档,重新打分)
ScoreFunctionBuilders.weightFactorFunction(10) //如何打分
)
}
).boostMode(CombineFunction.SUM);//加权规则(新旧得分运算规则)
//设置整体查询
request.source().query(functionScoreQueryBuilder);
}
到了这里,关于es——java代码实现聚合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!