类似于MatchQuery,但是分词后多个词的位置关系会被考虑,只有分词后的多个词在行数据中以同样的顺序和位置存在时,才表示行数据满足查询条件。如果查询列的分词类型为模糊分词,则使用MatchPhraseQuery可以实现比WildcardQuery更快的模糊查询。
前提条件
参数
参数 | 说明 |
---|---|
field_name | 要匹配的列。 匹配查询可应用于Text类型。 |
text | 查询关键词,即要匹配的值。 当要匹配的列为Text类型时,查询关键词会被分词成多个词,分词类型为创建多元索引时设置的分词器类型。如果创建多元索引时未设置分词器类型,则默认分词类型为单字分词。 例如查询的值为“this is”,可以匹配到“...,this is tablestore”、“this is a table”,但是无法匹配到“this table is ...”以及“is this a table”。 |
query | 设置查询类型为MatchPhraseQuery。 |
table_name | 数据表名称。 |
index_name | 多元索引名称。 |
limit | 本次查询需要返回的最大数量。 如果只为了获取行数,无需获取具体数据,可以设置limit=0,即不返回任意一行数据。 |
get_total_count | 是否返回匹配的总行数,默认为False,表示不返回。 返回匹配的总行数会影响查询性能。 |
columns_to_get | 是否返回所有列。
|
示例
查询表中t列的值按照顺序完整匹配'this is'短语的数据。
- 5.2.1及之后版本使用5.2.1及之后的SDK版本时,默认的返回结果为SearchResponse对象,请求示例如下:
query = MatchPhraseQuery('t', 'this is') search_response = client.search( table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) )
如果需要返回Tuple类型结果,您可以使用如下请求示例实现。query = MatchPhraseQuery('t', 'this is') rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search( table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) ).v1_response()
- 5.2.1之前版本
使用5.2.1之前的SDK版本时,默认的返回结果为Tuple类型,请求示例如下:
query = MatchPhraseQuery('t', 'this is') rows, next_token, total_count, is_all_succeed = client.search( table_name, index_name, SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) )