造成这种错误的原因:使用了withFilter
// 构建过滤查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (!StringUtils.isEmpty(searchMap.get("keywords"))) {
boolQueryBuilder.filter(QueryBuilders.queryStringQuery(searchMap.get("keywords")).field("name"));
}
if (!StringUtils.isEmpty(searchMap.get("brand"))) {
boolQueryBuilder.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand")));
}
if (!StringUtils.isEmpty(searchMap.get("category"))) {
boolQueryBuilder.filter(QueryBuilders.termQuery("categoryName", searchMap.get("category")));
}
nativeSearchQueryBuilder.withFilter(boolQueryBuilder);
解决办法:使用withQuery
// 构建过滤查询
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (!StringUtils.isEmpty(searchMap.get("keywords"))) {
boolQueryBuilder.filter(QueryBuilders.queryStringQuery(searchMap.get("keywords")).field("name"));
}
if (!StringUtils.isEmpty(searchMap.get("brand"))) {
boolQueryBuilder.filter(QueryBuilders.termQuery("brandName", searchMap.get("brand")));
}
if (!StringUtils.isEmpty(searchMap.get("category"))) {
boolQueryBuilder.filter(QueryBuilders.termQuery("categoryName", searchMap.get("category")));
}
nativeSearchQueryBuilder.withQuery(boolQueryBuilder);