This topic describes how to use nested query to query the data in the child rows 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 a query of any type.

Prerequisites

  • The OTSClient is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.
  • A search index is created for the data table. For more information, see Create search indexes.

Parameters

ParameterDescription
pathThe path of the nested column. The path is similar to the tree structure. For example, news.title indicates the title subcolumn in the news column of the nested type.
queryThe query on the subcolumn in the nested column. The query can be of any query type.
score_modeSpecifies which value is used to calculate the score when a column contains multiple values.
table_nameThe name of the table.
index_nameThe name of the search index.

Examples

Use nested query to query the value of the n.n1 column that is greater than or equal to 100 and smaller than or equal to 300.

  • Perform nested query by using Tablestore SDK for Python V5.2.1 or later
    By default, if you use Tablestore SDK for Python V5.2.1 or later to perform a nested query, SearchResponse objects are returned. The following code provides a request sample:
    nested_query = RangeQuery('n.nl', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('n', nested_query)
    search_response = client.search(
        table_name, index_name, 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )
    You can use the following sample request to return results of the Tuple type:
    nested_query = RangeQuery('n.nl', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('n', nested_query)
    rows, next_token, total_count, is_all_succeed, agg_result, group_by_results = client.search(
        table_name, index_name, 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    ).v1_response()
  • Perform nested query by using Tablestore SDK for Python of a version earlier than 5.2.1

    By default, if you use Tablestore SDK for Python of a version earlier than 5.2.1 to perform a nested query, results of the Tuple type are returned. The following code provides a request sample:

    nested_query = RangeQuery('n.nl', range_from=100, range_to=300, include_lower=True, include_upper=True)
    query = NestedQuery('n', nested_query)
    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)
    )