QueryBuilder missingFiletr = QueryHelper.andQuery(incCatsEmptyFilter, excCatsEmptyFilter, incEmptyFilter, excEmptyFilter);
我正在调用andQuery方法和 andFilterBuilders ()的所有参数 现在我将 filterBuilders 数组nad解压缩到 filterBuilder ,因为我想 和 所有的queryBuilders。格式是否正确
public static QueryBuilder andQuery(QueryBuilder... filterBuilders) {
QueryBuilder filterBuilder = null;
if (filterBuilders != null && filterBuilders.length > 0) {
for(int i = 0 ; i < filterBuilders.length ; i++){
filterBuilder = QueryBuilders.boolQuery().filter(filterBuilders[i]);
}
}
return filterBuilder;
}
答案 0 :(得分:1)
不,您正在为每次迭代创建一个新的bool/filter
查询并将其重新分配给同一个变量,这将无效。你需要这样做:
public static QueryBuilder andQuery(QueryBuilder... filterBuilders) {
BoolQueryBuilder filterBuilder = null;
if (filterBuilders != null && filterBuilders.length > 0) {
// create the bool query here
filterBuilder = QueryBuilders.boolQuery();
for(QueryBuilder filter : filterBuilders){
// add each filter to the bool query here
filterBuilder.filter(filter);
}
}
return filterBuilder;
}