This topic describes how to use nested query to query the child row data of nested columns. You cannot directly query nested fields. You must specify the NestedQuery object. In NestedQuery, you must specify the path of a nested column and a subquery. The subquery can be of any type.


  • The 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 table. For more information, see Create search indexes.


Parameter Description
TableName The name of the table.
IndexName The name of the search index.
Path The 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.
Query The query on the subcolumn in the NESTED column. The query can be of any query type.
ScoreMode The value on which a score is calculated when multiple values exist for a column.


The NESTED column contains nested_1 and nested_2. You must search the col_nested.nested_1 column for data that matches "tablestore".

func NestedQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    query := &search.NestedQuery{ // Set the query type to NestedQuery.
        Path: "col_nested", // Set the path of the NESTED field.
        Query: &search.TermQuery{ // Construct the subquery of NestedQuery.
            FieldName: "col_nested.nested_1", // Set the field that is prefixed with col_nested.
            Term:      "tablestore",          // Set the value you want to match.
        ScoreMode: search.ScoreMode_Avg,
    searchQuery := search.NewSearchQuery()
    // Return all columns.
        ReturnAll: true,
    searchResponse, err := client.Search(searchRequest)
    if err != nil {
        fmt.Printf("%#v", err)
    fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all data is returned.
    fmt.Println("RowCount: ", len(searchResponse.Rows))
    for _, row := range searchResponse.Rows {
        jsonBody, err := json.Marshal(row)
        if err != nil {
        fmt.Println("Row: ", string(jsonBody))