All Products
Search
Document Center

Tablestore:Nested query

Last Updated:Oct 17, 2024

You can perform a 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

Parameters

Parameter

Description

tableName

The name of the data table.

indexName

The name of the search index.

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.

scoreMode

The value that is used to calculate the score if a field contains multiple values.

InnerHits

The settings of the subfields of the nested field.

  • sort: the sorting rule for the child rows of the nested field.

  • offset: the start position of the child rows to return if the nested field consists of multiple child rows.

  • limit: the maximum number of child rows to return if the nested field consists of multiple child rows. Default value: 3.

  • highlight: the highlight settings for the subfields of the nested field. For more information, see Highlight.

Examples

Query single-level nested fields

The following sample code provides an example on how to query the rows in which the value of the Sub_Col_Keyword subfield in the Col_Nested nested field is Hangzhou:

/**
 * Col_Nested: '[{Sub_Col_Keyword: "Hangzhou"},{Sub_Col_Keyword: "Shanghai"}]'
 */
client.search({
    tableName: "<TABLE_NAME>",
    indexName: "<SEARCH_INDEX_NAME>",
    searchQuery: {
        offset: 0,
        limit: 10, // To query only the number of rows that meet the query conditions without returning specific data, set the limit parameter to 0. 
        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. Default value: false. 
    },
    columnToGet: { // Specify the columns that you want to return. You can set the parameter to RETURN_SPECIFIED to return specific columns, RETURN_ALL to return all columns, RETURN_ALL_FROM_INDEX to return all columns in the search index, or RETURN_NONE 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));
});

Highlight the query strings in the query results of a nested query

The following sample code provides an example on how to query the rows in which the value of the Sub_Col_Text subfield of the nested field named Col_Nested matches tablestore and highlight the query strings in the query results.

client.search({
    tableName: "<TABLE_NAME>",
    indexName: "<SEARCH_INDEX_NAME>",
    searchQuery: {
        offset: 0,
        limit: 10, // To query only the number of rows that meet the query conditions without returning specific data, set the limit parameter to 0. 
        query: { // Set the query type to TableStore.QueryType.NESTED_QUERY. 
            queryType: TableStore.QueryType.NESTED_QUERY,
            query: {
                path: "Col_Nested",
                query: {
                    queryType: TableStore.QueryType.MATCH_QUERY,
                    query: {
                        fieldName: "Col_Nested.Sub_Col_Text",
                        text: "tablestore"
                    }
                },
                innerHits: {
                    sort: {
                        sorters: [
                            {
                                scoreSort: {
                                    order: TableStore.SortOrder.SORT_ORDER_DESC
                                }
                            },
                            {
                                docSort: {
                                    order: TableStore.SortOrder.SORT_ORDER_ASC
                                }
                            },
                        ],
                    },
                    highlight: {
                        highlightParameters: [
                            {
                                fieldName:"Col_Nested.Sub_Col_Text",
                                preTag: "<b>",
                                postTag: "</b>",
                                fragmentsOrder: TableStore.HighlightFragmentOrder.TEXT_SEQUENCE,
                                fragmentSize: 20,
                                numberOfFragments: 3,
                            }
                        ]
                    }
                },
            }
        },
        getTotalCount: true // Specify whether to return the total number of rows that meet the query conditions. Default value: false. 
    },
    columnToGet: { // Specify the columns that you want to return. You can set the parameter to RETURN_SPECIFIED to return specific columns, RETURN_ALL to return all columns, RETURN_ALL_FROM_INDEX to return all columns in the search index, or RETURN_NONE 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.rows, null, 2));

    printSearchHit(data.searchHits, "");
});

/**
 * Display the content that meets the query conditions. 
 * @param searchHits searchHits
 * If the output uses the @param prefix Nested structure, add the prefix to display the hierarchy information. 
 */
function printSearchHit(searchHits, prefix) {
    TableStore.util.arrayEach(searchHits, function (searchHit) {
        if (searchHit.highlightResultItem != null) {
            console.log(prefix + "Highlight: \n");
            var strBuilder = ""
            for  (const [key,val]  of searchHit.highlightResultItem.highlightFields.entries()) {
                strBuilder += key + ":[";
                strBuilder += val.fragments.join(",") + "]\n";
                console.log(strBuilder);
            }
        }
        for  (const [key,val]  of searchHit.searchInnerHits.entries()) {
            console.log(prefix + "Path: " + key + "\n");
            console.log(prefix + "InnerHit: \n");
            printSearchHit(val.subSearchHits, prefix + "    ");
        }
    });
}

FAQ

References

  • The following query types are supported by search indexes: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, vector query, and exists query. You can select a query type to query data based on your business requirements.

    If you want to sort or paginate the rows that meet the query conditions, you can use the sorting and paging feature. For more information, see Sorting and paging.

    If you want to collapse the result set based on a specific column, you can use the collapse (distinct) feature. 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, such as obtaining the extreme values, sum, and total number of rows, you can perform aggregation operations or execute SQL statements. For more information, see Aggregation and SQL query.

  • If you want to quickly 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.