前提条件
已初始化OTSClient。具体操作,请参见初始化Tablestore Client。
已在数据表上创建多元索引。具体操作,请参见创建多元索引。
参数
|
参数 |
说明 |
|
query_type |
查询类型,设置为 TermQuery。 |
|
field_name |
要查询的字段名称。 |
|
term |
精确匹配的关键字。该关键字不会被分词,以完整词条进行匹配。 对于 TEXT 类型字段,Tablestore 会对字段值进行分词,并将每个词条与关键字逐一比对,任意词条匹配则返回该行。 例如,某 TEXT 字段的值为"tablestore is cool",分词后得到"tablestore"、"is"、"cool"三个词条,查询"tablestore"、"is"或"cool"时均可命中该行。 |
|
table_name |
数据表名称。 |
|
index_name |
多元索引名称。 |
|
limit |
本次查询返回的最大行数。 如果只需获取匹配行数而不需要实际数据,将 limit 设置为 0。 |
|
get_total_count |
是否返回匹配的总行数,默认为 False。 开启后会降低查询性能。 |
|
columns_to_get |
指定每行返回的列,通过 return_type 和 column_names 配置。
|
示例
以下示例查询 Col_Keyword 列精确匹配"tablestore"的数据行。
-
5.2.1 及之后版本
SDK for Python V5.2.1 及之后版本默认返回 SearchResponse 对象,示例如下:
query = TermQuery('Col_Keyword', 'tablestore') search_response = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, 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) # # 当需要进行深度翻页时,推荐使用next_token进行翻页(翻页深度无限制) # all_rows = [] # next_token = None # # first round # search_response = client.search( # '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', # SearchQuery(query, next_token=next_token, limit=100, get_total_count=True), # columns_to_get=ColumnsToGet(return_type=ColumnReturnType.ALL)) # all_rows.extend(search_response.rows) # # # loop # while search_response.next_token: # search_response = client.search( # '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', # SearchQuery(query, next_token=search_response.next_token, limit=100, get_total_count=True), # columns_to_get=ColumnsToGet(return_type=ColumnReturnType.ALL)) # all_rows.extend(search_response.rows) # print('Total rows:%s' % len(all_rows))如需返回 Tuple 类型结果,调用
.v1_response()方法:query = TermQuery('Col_Keyword', 'tablestore') rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) ).v1_response() -
5.2.1 之前版本
SDK for Python V5.2.1 之前版本默认返回 Tuple 类型结果,示例如下:
query = TermQuery('Col_Keyword', 'tablestore') rows, next_token, total_count, is_all_succeed = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) )