Use `NestedQuery` to query data in the nested documents of a nested type field. You cannot query nested types directly. Instead, wrap your query in a `NestedQuery` object. The `NestedQuery` must specify the path of the nested field and a subquery. The subquery can be any query type.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize a Tablestore client.
A data table is created and data is written to the data table. For more information, see Create a data table and Write data.
A search index is created for the data table. For more information, see Create a search index.
Parameters
Parameter | Description |
path | The path of the nested type column. For example, `news.title` indicates the `title` sub-column within the nested `news` column. |
query | The query on the sub-columns of the nested type column. The subquery can be any query type. |
score_mode | The value used to calculate the score when a column contains multiple values. |
table_name | The name of the data table. |
index_name | The name of the search index. |
inner_hits | The configuration parameters for the sub-columns of a nested type field. It includes the following items:
|
Examples
Example of a single-level nested type query
The following example queries for data where the value of the `col_nested.col_long` column is greater than or equal to 100 and less than or equal to 300.
SDK versions 5.2.1 and later
If you use SDK version 5.2.1 or later, the search returns a `SearchResponse` object by default. The following code shows an example request:
nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True) query = NestedQuery('col_nested', nested_query) 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)If you want the result to be a tuple, use the following example request.
nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True) query = NestedQuery('col_nested', nested_query) rows, next_token, total_count, is_all_succeed, agg_result, 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()SDK versions earlier than 5.2.1
If you use an SDK version earlier than 5.2.1, the search returns a tuple by default. The following code shows an example request:
nested_query = RangeQuery('col_nested.col_long', range_from=100, range_to=300, include_lower=True, include_upper=True) query = NestedQuery('col_nested', nested_query) 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) )
Example of using summary and highlight in a nested type query
The following example shows how to use `NestedQuery` to find data where the value of the `col_text` sub-field in the `col_nested` nested field matches `tablestore`. The search term is highlighted in the results.
def _print_rows(request_id, rows, total_count):
print('Request ID:%s' % request_id)
for row in rows:
print(row)
print('Rows return: %d' % len(rows))
print('Total count: %d' % total_count)
def _print_search_hit(hits):
for search_hit in hits:
print('\t score is %.6f' % search_hit.score)
for highlight_field in search_hit.highlight_result.highlight_fields:
print('\t\t highlight:%s:%s' % (highlight_field.field_name, highlight_field.field_fragments))
for inner_result in search_hit.search_inner_hits:
print('\t\t path:%s' % (inner_result.path))
_print_search_hit(inner_result.search_hits)
def highlight_query_for_nested(client):
print('********** Begin HighlightQueryForNested **********')
sort = Sort(
sorters=[FieldSort('col_nested.col_long', sort_order=SortOrder.ASC)]
)
highlight_parameter = HighlightParameter("col_nested.col_text", 1, 18, '', '', HighlightFragmentOrder.TEXT_SEQUENCE)
highlight_clause = Highlight([highlight_parameter], HighlightEncoder.PLAIN_MODE)
inner_hits_parameter = InnerHits(None, 0, 10, highlight_clause)
query = NestedQuery('n', MatchQuery('col_nested.col_text', 'tablestore'), ScoreMode.AVG, inner_hits_parameter)
search_response = client.search('<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
SearchQuery(query, limit=2, get_total_count=True),
ColumnsToGet(return_type=ColumnReturnType.ALL_FROM_INDEX)
)
print('----- Print Rows:')
print('search rows count:%d' % len(search_response.rows))
_print_rows(search_response.request_id,search_response.rows,search_response.total_count)
print('----- Print Highlight Result:')
search_hits = search_response.search_hits
print('search hit count:%d' % len(search_hits))
_print_search_hit(search_hits)
print('********** End HighlightQuery **********')FAQ
References
When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, Boolean query, KNN vector query, nested query, and exists query. You can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements.
You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.
You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).
If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.
If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.