QueryBuilders数组弹性搜索2.4

时间:2017-01-12 06:18:29

标签: elasticsearch

 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;

}

1 个答案:

答案 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;
}