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

  • An OTSClient instance 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.
ScoreModeSpecifies which value is used to calculate the score when a column contains multiple values.
TableNameThe name of the table.
IndexNameThe name of the search index.
ColumnsToGetSpecifies whether to return all columns of each row that meets the query conditions. You can configure ReturnAll, Columns, and ReturnAllFromIndex for this parameter.
The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
  • Configure Columns to specify the columns that you want to return.
  • Set ReturnAllFromIndex to true to return all columns from the search index.

If you set ReturnAll to true, all columns are returned.

Examples

/// <summary>
/// Search the table for rows in which the value of the col1_nested.nested_1 column is tablestore. In this example, col1_nested is a nested field and includes the nested_1 and nested_2 subcolumns. 
/// </summary>
/// <param name="otsClient"></param>
public static void NestedQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Specify that the total number of rows that meet the query conditions is returned. 
    searchQuery.GetTotalCount = true;
    var nestedQuery = new NestedQuery();
    nestedQuery.Path = "col1_nested"; // Specify the path of the nested field. 
    TermQuery termQuery = new TermQuery("col1_nested.nested_1",new ColumnValue("tablestore"));// Construct a subquery of NestedQuery. 
    nestedQuery.Query = termQuery;
    nestedQuery.ScoreMode = ScoreMode.None;

    var request = new SearchRequest(TableName, IndexName, searchQuery);
    // You can use the ColumnsToGet parameter to specify the columns that you want to return, specify that all columns are returned, or specify that all columns in the search index are returned. If you do not configure this parameter, only the primary key columns are returned. 
    request.ColumnsToGet = new ColumnsToGet()
    {
        // Specify that all columns in the search index are returned. 
        ReturnAllFromIndex = true
        // Specify the columns that you want to return. 
        //Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
        // Specify that all columns are returned. 
        //ReturnAll = true
    };

    var response = otsClient.Search(request);

    Console.WriteLine("Total Count:" + response.TotalCount); // Specify that the total number of rows that meet the query conditions instead of the number of rows that are returned is displayed. 
}