BoolQuery查询条件包含一个或者多个子查询条件,根据子查询条件来判断一行数据是否满足查询条件。每个子查询条件可以是任意一种Query类型,包括BoolQuery。

前提条件

  • 已初始化OTSClient,详情请参见初始化
  • 已创建数据表并写入数据。
  • 已在数据表上创建多元索引,详情请参见创建多元索引

参数

参数 说明
table_name 数据表名称。
index_name 多元索引名称。
must_queries 多个Query列表,行数据必须满足所有的子查询条件才算匹配,等价于And操作符。
must_not_queries 多个Query列表,行数据必须不能满足任何的子查询条件才算匹配,等价于Not操作符。
filter_queries 多个Query列表,行数据必须满足所有的子filter才算匹配,filter类似于query,区别是filter不会根据满足的filter个数进行相关性算分。
should_queries 多个Query列表,可以满足,也可以不满足,等价于Or操作符。

行数据应该至少满足should_queries子查询条件的最小匹配个数才算匹配。

如果满足的should_queries子查询条件个数越多,则整体的相关性分数更高。

minimum_should_match should_queries子查询条件的最小匹配个数。默认值为1。

示例

如下示例中条件A为keyword列值精确匹配'keyword',条件B为long列值大于等于100且小于101。

  • 示例1

    通过BoolQuery进行And条件查询,查询条件为MustQueries(A AND B),即查询同时满足条件A和条件B的行。

    $request = array(
        'table_name' => 'php_sdk_test',
        'index_name' => 'php_sdk_test_search_index',
        'search_query' => array(
            'offset' => 0,
            'limit' => 2,
            'get_total_count' => true,
            'query' => array(
                'query_type' => QueryTypeConst::BOOL_QUERY,
                'query' => array(
                    'must_queries' => array(
                        array(
                            'query_type' => QueryTypeConst::TERM_QUERY,
                            'query' => array(
                                'field_name' => 'keyword',
                                'term' => 'keyword'
                            )
                        ),
                        array(
                            'query_type' => QueryTypeConst::RANGE_QUERY,
                            'query' => array(
                                'field_name' => 'long',
                                'range_from' => 100,
                                'include_lower' => true,
                                'range_to' => 101,
                                'include_upper' => false
                            )
                        )
                    ),
                )
            ),
            'sort' => array(
                array(
                    'field_sort' => array(
                        'field_name' => 'keyword',
                        'order' => SortOrderConst::SORT_ORDER_ASC
                    )
                ),
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('keyword', 'long')
        )
    );
    $response = $otsClient->search($request);
  • 示例2

    通过BoolQuery进行Not条件查询,查询条件为MustNotQueries(!A AND !B),即查询同时不满足条件A和条件B的行。

    $request = array(
        'table_name' => 'php_sdk_test',
        'index_name' => 'php_sdk_test_search_index',
        'search_query' => array(
            'offset' => 0,
            'limit' => 2,
            'get_total_count' => true,
            'query' => array(
                'query_type' => QueryTypeConst::BOOL_QUERY,
                'query' => array(
                    'must_not_queries' => array(
                        array(
                            'query_type' => QueryTypeConst::TERM_QUERY,
                            'query' => array(
                                'field_name' => 'keyword',
                                'term' => 'keyword'
                            )
                        ),
                        array(
                            'query_type' => QueryTypeConst::RANGE_QUERY,
                            'query' => array(
                                'field_name' => 'long',
                                'range_from' => 100,
                                'include_lower' => true,
                                'range_to' => 101,
                                'include_upper' => false
                            )
                        )
                    ),
                )
            ),
            'sort' => array(
                array(
                    'field_sort' => array(
                        'field_name' => 'keyword',
                        'order' => SortOrderConst::SORT_ORDER_ASC
                    )
                ),
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('keyword', 'long')
        )
    );
    $response = $otsClient->search($request);
  • 示例3

    通过BoolQuery进行Filter条件查询,查询条件为FilterQueries(A AND B),即查询同时满足条件A和条件B的行,行数据不会根据满足条件个数进行相关性算分。

    $request = array(
        'table_name' => 'php_sdk_test',
        'index_name' => 'php_sdk_test_search_index',
        'search_query' => array(
            'offset' => 0,
            'limit' => 2,
            'get_total_count' => true,
            'query' => array(
                'query_type' => QueryTypeConst::BOOL_QUERY,
                'query' => array(
                    'filter_queries' => array(
                        array(
                            'query_type' => QueryTypeConst::TERM_QUERY,
                            'query' => array(
                                'field_name' => 'keyword',
                                'term' => 'keyword'
                            )
                        ),
                        array(
                            'query_type' => QueryTypeConst::RANGE_QUERY,
                            'query' => array(
                                'field_name' => 'long',
                                'range_from' => 100,
                                'include_lower' => true,
                                'range_to' => 101,
                                'include_upper' => false
                            )
                        )
                    ),
                )
            ),
            'sort' => array(
                array(
                    'field_sort' => array(
                        'field_name' => 'keyword',
                        'order' => SortOrderConst::SORT_ORDER_ASC
                    )
                ),
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('keyword', 'long')
        )
    );
    $response = $otsClient->search($request);
  • ShouldQueries(A OR B)

    查询满足条件A和条件B中至少一个条件的行。

    $request = array(
        'table_name' => 'php_sdk_test',
        'index_name' => 'php_sdk_test_search_index',
        'search_query' => array(
            'offset' => 0,
            'limit' => 2,
            'get_total_count' => true,
            'query' => array(
                'query_type' => QueryTypeConst::BOOL_QUERY,
                'query' => array(
                    'should_queries' => array(
                        array(
                            'query_type' => QueryTypeConst::TERM_QUERY,
                            'query' => array(
                                'field_name' => 'keyword',
                                'term' => 'keyword'
                            )
                        ),
                        array(
                            'query_type' => QueryTypeConst::RANGE_QUERY,
                            'query' => array(
                                'field_name' => 'long',
                                'range_from' => 100,
                                'include_lower' => true,
                                'range_to' => 101,
                                'include_upper' => false
                            )
                        )
                    ),
                    'minimum_should_match' => 1
                )
            ),
            'sort' => array(
                array(
                    'field_sort' => array(
                        'field_name' => 'keyword',
                        'order' => SortOrderConst::SORT_ORDER_ASC
                    )
                ),
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('keyword', 'long')
        )
    );
    $response = $otsClient->search($request);
  • 示例4
    含有子BoolQuery的组合查询。使用多条件组合查询实现(col2<4 or col3<5) or (col2 = 4 and (col3 = 5 or col3 =6)),每个and或or相当于一个BoolQuery,多个表达式的and或or就是多个BoolQuery的组合。
    $request = array(
        'table_name' => 'php_sdk_test',
        'index_name' => 'php_sdk_test_index',
        'search_query' => [
            'offset' => 0,
            'limit' => 10,
            'get_total_count' => false,
            'query' => [
                'query_type' => QueryTypeConst::BOOL_QUERY,
                'query' => [
                    //总组合查询条件为(col2<4 or col3<5) or (col2 = 4 and (col3 = 5 or col3 =6))。
                    'should_queries' => [
                        [
                            'query_type' => QueryTypeConst::BOOL_QUERY,
                            'query' => [
                                //组合条件1为(col2<4 or col3<5)。
                                'should_queries' => [
                                    [
                                        'query_type' => QueryTypeConst::RANGE_QUERY,
                                        //查询条件1为col2<4。
                                        'query' => [
                                            'field_name' => 'col2',
                                            'range_to' => 4
                                        ]
                                    ],
                                    [
                                        'query_type' => QueryTypeConst::RANGE_QUERY,
                                        //查询条件2为col3<5。
                                        'query' => [
                                            'field_name' => 'col3',
                                            'range_to' => 5
                                        ]
                                    ]
                                ]
                            ]
                        ],
                        [
                            'query_type' => QueryTypeConst::BOOL_QUERY,
                            //组合条件2为(col2 = 4 and (col3 = 5 or col3 =6))。
                            'query' => [
                                'must_queries' => [
                                    [
                                        'query_type' => QueryTypeConst::TERM_QUERY,
                                        //查询条件3为col2=4。
                                        'query' => [
                                            'field_name' => 'col2',
                                            'term' => 4
                                        ]
                                    ],
                                    [
                                        'query_type' => QueryTypeConst::BOOL_QUERY,
                                        //组合条件3为(col3 = 5 or col3 =6))。
                                        'query' => [
                                            'should_queries' => [
                                                [
                                                    'query_type' => QueryTypeConst::TERM_QUERY,
                                                    //查询条件4为col3=5。
                                                    'query' => [
                                                        'field_name' => 'col3',
                                                        'term' => 5
                                                    ]
                                                ],
                                                [
                                                    'query_type' => QueryTypeConst::TERM_QUERY,
                                                    //查询条件5为col3=6。
                                                    'query' => [
                                                        'field_name' => 'col3',
                                                        'term' => 6
                                                    ]
                                                ]
                                            ],
                                            'minimum_should_match' => 1
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'minimum_should_match' => 1
                ]
            ]
        ],
        //通过设置columns_to_get参数可以指定返回的列或返回所有列,如果不设置此参数,则默认只返回主键列。
        'columns_to_get' => [ 
            //'return_type' => ColumnReturnTypeConst::RETURN_ALL //设置为返回所有列。
            //设置为返回指定列。
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED, 
            'return_names' => array('col1', 'col2')
        ]
    );