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

  • A Tablestore client 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

Parameter Description
tableName The name of the data 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 Specifies which value is used to calculate the score when a column contains multiple values.

Examples

Search the table for rows in which the Col_Nested.Sub_Col_Keyword value is "Hangzhou".

/**
 * Col_Nested: '[{Sub_Col_Keyword: "Hangzhou"},{Sub_Col_Keyword: "Shanghai"}]'
 */
client.search({
    tableName: TABLE_NAME,
    indexName: INDEX_NAME,
    searchQuery: {
        offset: 0,
        limit: 10, // To query only the number of rows that meet the query conditions without returning specific data, you can set limit to 0. This way, Tablestore returns the number of rows that meet the query conditions without specific data from the table. 
        query: { // Set the query type to TableStore.QueryType.NESTED_QUERY. 
            queryType: TableStore.QueryType.NESTED_QUERY,
            query: {
                path: "Col_Nested",
                query: {
                    queryType: TableStore.QueryType.TERM_QUERY,
                    query: {
                        fieldName: "Col_Nested.Sub_Col_Keyword",
                        term: "Hangzhou"
                    }
                },
            }
        },
        getTotalCount: true // Specify whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which indicates that the total number of rows that meet the query conditions is not returned. 
    },
    columnToGet: { // Specify the columns that you want to return. You can configure the RETURN_SPECIFIED parameter to return specified columns, the RETURN_ALL parameter to return all columns, the RETURN_ALL_FROM_INDEX parameter to return all columns in the search index, or the RETURN_NONE parameter to return only the primary key columns. 
        returnType: TableStore.ColumnReturnType.RETURN_ALL
    }
}, function (err, data) {
    if (err) {
        console.log('error:', err);
        return;
    }
    console.log('success:', JSON.stringify(data, null, 2));
});