You can perform a nested query to query the data in the subfields of Nested fields. Nested fields cannot be directly queried. To query a Nested field, you must specify the path of the Nested field and a subquery in a NestedQuery object. The subquery can be any type of query.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
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 field. The Nested field uses a tree structure. For example, news.title specifies the title subfield in the Nested field named news. |
query | The query on the subfield in the Nested field. The query can be a query of any type. |
score_mode | The value that is used to calculate the score when a field contains multiple values. |
table_name | The name of the data table. |
index_name | The name of the search index. |
inner_hits | The settings of the subfields in the Nested field.
|
Example
Single-level Nested fields
The following examples show how to query the rows in which the value of the col_nested.col_long field is greater than or equal to 100 and less than or equal to 300.
Perform a nested query by using Tablestore SDK for Python V5.2.1 or later
If you use Tablestore SDK for Python V5.2.1 or later to perform a nested query, a SearchResponse object is returned by default. The following code provides a sample 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)
You can use the following sample request to return TUPLE results:
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()
Perform a nested query by using Tablestore SDK for Python of a version earlier than 5.2.1
If you use a version of Tablestore SDK for Python that is earlier than 5.2.1 to perform a nested query, TUPLE results are returned by default. The following code provides a sample 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) )
Nested fields for which the highlight feature is enabled
The following sample code provides an example on how to query the rows in which the value of the col_nested.col_text field is tablestore
and highlight the keywords in the query 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, '<b>', '</b>', 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.