您可以使用BoolQuery接口进行多条件的组合查询。BoolQuery可以包含一个或者多个子查询条件,根据子查询条件是否满足来判断一行数据是否满足查询条件。

您可以设置多种组合方式,例如设置多个子查询条件为mustQueries,则要求这些子查询条件必须都满足。设置多个子查询条件为mustNotQueries时,要求这些子查询条件都不能满足。

BoolQuery有以下几个参数:




/**
 * 文档必须完全匹配所有的子query
 */
List<Query> mustQueries;
/**
 * 文档必须不能匹配任何子query
 */
List<Query> mustNotQueries;
/**
 * 文档必须完全匹配所有的子filter, filter类似于query,区别是不会进行算分
 */
List<Query> filterQueries;
/**
 * 文档应该至少匹配minimumShouldMatch个should条件,匹配多的得分会高
 */
List<Query> shouldQueries;
/**
 * 定义了至少满足几个should子句,默认是1.
 */
Integer minimumShouldMatch;

BoolQuery的子Query可以是任意Query类型,包括BoolQuery本身,因此通过BoolQuery可以实现非常复杂的组合查询类型。

示例

  • Java
    
    /**
     * 通过BoolQuery进行复合条件查询。
     * @param client
     */
    public static void boolQuery(SyncClient client) {
        /**
         * 查询条件一:RangeQuery,Col_Long这一列的值要大于3
         */
        RangeQuery rangeQuery = new RangeQuery();
        rangeQuery.setFieldName("Col_Long");
        rangeQuery.greaterThan(ColumnValue.fromLong(3));
    
        /**
         * 查询条件二:MatchQuery,Col_Keyword这一列的值要匹配"hangzhou"
         */
        MatchQuery matchQuery = new MatchQuery(); // 设置查询类型为MatchQuery
        matchQuery.setFieldName("Col_Keyword"); // 设置要匹配的字段
        matchQuery.setText("hangzhou"); // 设置要匹配的值
    
        SearchQuery searchQuery = new SearchQuery();
        {
            /**
             * 构造一个BoolQuery,设置查询条件是必须同时满足"条件一"和"条件二"
             */
            BoolQuery boolQuery = new BoolQuery();
            boolQuery.setMustQueries(Arrays.asList(rangeQuery, matchQuery));
            searchQuery.setQuery(boolQuery);
            searchQuery.setGetTotalCount(true);
            SearchRequest searchRequest = new SearchRequest(TABLE_NAME, INDEX_NAME, searchQuery);
            SearchResponse resp = client.search(searchRequest);
            System.out.println("TotalCount: " + resp.getTotalCount()); // 匹配到的总行数,非返回行数
            System.out.println("Row: " + resp.getRows());
        }
    
        {
            /**
             * 构造一个BoolQuery,设置查询条件是至少满足"条件一"和"条件二"中的一个条件
             */
            BoolQuery boolQuery = new BoolQuery();
            boolQuery.setShouldQueries(Arrays.asList(rangeQuery, matchQuery));
            boolQuery.setMinimumShouldMatch(1); // 设置最少满足一个条件
            searchQuery.setQuery(boolQuery);
            searchQuery.setGetTotalCount(true);
            SearchRequest searchRequest = new SearchRequest(TABLE_NAME, INDEX_NAME, searchQuery);
            SearchResponse resp = client.search(searchRequest);
            System.out.println("TotalCount: " + resp.getTotalCount()); // 匹配到的总行数,非返回行数
            System.out.println("Row: " + resp.getRows());
        }
    }
  • 含有子boolQuery的组合查询
    /**
     * (col2<4 or col3<5)  or (col2 = 4 and (col3 = 5 or col3 =6))
     * 使用多条件组合查询上述表达式,每个or或and就是一个BoolQuery,多个表达式的and or就是多个BoolQuery的组合
     * @param client
    */
    private static void boolQuery2(SyncClient client){
            //条件1 col2 < 4
            RangeQuery rangeQuery1 = new RangeQuery();
            rangeQuery1.setFieldName("col2");
            rangeQuery1.lessThan(ColumnValue.fromLong(4));
    
            //条件二 col3<5
            RangeQuery rangeQuery2 = new RangeQuery();
            rangeQuery2.setFieldName("col3");
            rangeQuery2.lessThan(ColumnValue.fromLong(5));
    
            //条件 col2 =4
            TermQuery termQuery = new TermQuery();
            termQuery.setFieldName("col2");
            termQuery.setTerm(ColumnValue.fromLong(4));
    
            //条件四 col3 = 5 or col3 = 6
            TermsQuery termsQuery = new TermsQuery();
            termsQuery.setFieldName("col3");
            termsQuery.addTerm(ColumnValue.fromLong(5));
            termsQuery.addTerm(ColumnValue.fromLong(6));
    
            SearchQuery searchQuery = new SearchQuery();
    
            List<Query> queryList1 = new ArrayList<>();
            queryList1.add(rangeQuery1);
            queryList1.add(rangeQuery2);
    
            //组合1:col2 < 4 OR col3 < 5
            BoolQuery boolQuery1 = new BoolQuery();
            boolQuery1.setShouldQueries(queryList1);
    
            //组合2: col2 = 4  and (col3 = 5 or col3 = 6)
            List<Query> queryList2 = new ArrayList<>();
            queryList2.add(termQuery);
            queryList2.add(termsQuery);
    
            BoolQuery boolQuery2 = new BoolQuery();
            boolQuery2.setMustQueries(queryList2);
    
            //总组合  (col2 < 4 OR col3 < 5)  or (col2 = 4  and (col3 = 5 or col3 = 6))
    
            List<Query> queryList3 = new ArrayList<>();
            queryList3.add(boolQuery1);
            queryList3.add(boolQuery2);
    
            BoolQuery boolQuery = new BoolQuery();
            boolQuery.setShouldQueries(queryList3);
    
            searchQuery.setQuery(boolQuery);
            SearchRequest request = new SearchRequest(TABLE_NAME,INDEX_NAME,searchQuery);
            SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
            columnsToGet.setReturnAll(true);
            request.setColumnsToGet(columnsToGet);
            SearchResponse response = client.search(request);
            System.out.println(response.getRows());
    
    
        }