文档

多条件组合查询

更新时间:

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

前提条件

参数

参数

说明

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子查询条件的最小匹配个数。当同级没有其他Query,只有should_queries时,默认值为1;当同级已有其他Query,例如must_queries、must_not_queries和filter_queries时,默认值为0。

table_name

数据表名称。

index_name

多元索引名称。

示例

以下示例用于通过BoolQuery进行多条件组合查询。

  • 5.2.1及之后版本

    使用5.2.1及之后的SDK版本时,默认的返回结果为SearchResponse对象,请求示例如下:

    # Col_Keyword > 'key100' and (Col_Long > 110 and Col_Long < 200) and not (Col_Keyword = 'key121') and
    # should_queries(Col_Keyword > 'key120' or Col_Long < 300, minimum_should_match = 2)
    bool_query = BoolQuery(
        must_queries=[
            RangeQuery('Col_Keyword', range_from='key100', include_lower=False),
            # 多条件组合查询。
            BoolQuery(
                # 设置需要满足的子查询条件。
                must_queries=[
                    RangeQuery('Col_Long', range_from=110, include_lower=False),
                    RangeQuery('Col_Long', range_to=200, include_upper=False)
                ],
            )
        ],
        # 设置需要排除的子查询条件。
        must_not_queries=[
            TermQuery('Col_Keyword', 'key121')
        ],
        should_queries=[
            RangeQuery('Col_Keyword', range_from='key120', include_lower=False),
            RangeQuery('Col_Long', range_to=300, include_upper=130)
        ],
        minimum_should_match=2
    )
    # 构造完整查询语句,包括排序的列,返回前100行以及返回查询结果总的行数。
    search_response = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
        SearchQuery(
            bool_query,
            sort=Sort(sorters=[FieldSort('Col_Long', SortOrder.ASC)]),
            limit=100,
            get_total_count=True),
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )
    print('request_id : %s' % search_response.request_id)
    print('is_all_succeed : %s' % search_response.is_all_succeed)
    print('total_count : %s' % search_response.total_count)
    print('rows : %s' % search_response.rows)

    如果需要返回Tuple类型结果,您可以使用如下请求示例实现。

    # Col_Keyword > 'key100' and (Col_Long > 110 and Col_Long < 200) and not (Col_Keyword = 'key121') and 
    # should_queries(Col_Keyword > 'key120' or Col_Long < 300, minimum_should_match = 2) 
    bool_query = BoolQuery(
        must_queries=[
            RangeQuery('Col_Keyword', range_from='key100', include_lower=False),
            # 多条件组合查询。
            BoolQuery(
                # 设置需要满足的子查询条件。
                must_queries=[
                    RangeQuery('Col_Long', range_from=110, include_lower=False),
                    RangeQuery('Col_Long', range_to=200, include_upper=False)
                ],
            )
        ],
        # 设置需要排除的子查询条件。
        must_not_queries=[
            TermQuery('Col_Keyword', 'key121')
        ],
        should_queries=[
            RangeQuery('Col_Keyword', range_from='key120', include_lower=False),
            RangeQuery('Col_Long', range_to=300, include_upper=130)
        ],
        minimum_should_match=2
    )
    # 构造完整查询语句,包括排序的列,返回前100行以及返回查询结果总的行数。
    rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
        SearchQuery(
            bool_query,
            sort=Sort(sorters=[FieldSort('Col_Long', SortOrder.ASC)]),
            limit=100,
            get_total_count=True),
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    ).v1_response()
  • 5.2.1之前版本

    使用5.2.1之前的SDK版本时,默认的返回结果为Tuple类型,请求示例如下:

    # Col_Keyword > 'key100' and (Col_Long > 110 and Col_Long < 200) and not (Col_Keyword = 'key121') and 
    # should_queries(Col_Keyword > 'key120' or Col_Long < 300, minimum_should_match = 2) 
    bool_query = BoolQuery(
        must_queries=[
            RangeQuery('Col_Keyword', range_from='key100', include_lower=False),
            # 多条件组合查询。
            BoolQuery(
                # 设置需要满足的子查询条件。
                must_queries=[
                    RangeQuery('Col_Long', range_from=110, include_lower=False),
                    RangeQuery('Col_Long', range_to=200, include_upper=False)
                ],
            )
        ],
        # 设置需要排除的子查询条件。
        must_not_queries=[
            TermQuery('Col_Keyword', 'key121')
        ],
        should_queries=[
            RangeQuery('Col_Keyword', range_from='key120', include_lower=False),
            RangeQuery('Col_Long', range_to=300, include_upper=130)
        ],
        minimum_should_match=2
    )
    # 构造完整查询语句,包括排序的列,返回前100行以及返回查询结果总的行数。
    rows, next_token, total_count, is_all_succeed = client.search(
        '<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
        SearchQuery(
            bool_query,
            sort=Sort(sorters=[FieldSort('Col_Long', SortOrder.ASC)]),
            limit=100,
            get_total_count=True),
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )

常见问题

相关文档