Match query

更新时间:
复制 MD 格式

A match query performs an approximate match on data in a table. Tablestore uses the configured tokenizer to split the values in Text columns and the search query into tokens, and then searches for these tokens. To perform high-performance fuzzy queries on Text columns that use fuzzy tokenization, use a match phrase query.

Scenarios

A match query finds data that contains a specific phrase. When used with tokenization, a match query enables full-text indexing. This is useful in scenarios such as data analytics, content search, knowledge management, social media analysis, log analysis, AI chat systems, and compliance reviews. For example, you can quickly filter products on an E-commerce platform whose titles, descriptions, or labels contain keywords entered by a user. You can also quickly locate error messages or abnormal operations in logs.

Function overview

A match query performs an approximate match on data in a table. For example, consider a row with a `title` column of the Text type that has the value "Hángzhōu Xīhú Fēngjǐngqū". If this column uses single-word tokenization and the search query is "hú fēng", the row is matched.

When you use a match query, you must specify the column to match and the search query. A row meets the query condition if any of the tokens from the search query exist in the specified column.

You can also configure parameters such as the minimum number of matches, query weight, columns to return, whether to return the total number of matched rows, and the sorting method for the result set.

API operations

The API operations for a match query are Search or ParallelScan. The specific query type is MatchQuery.

Parameters

Parameter

Description

fieldName

The column to match.

A match query can be applied to Text type columns.

text

The search query, which is the value to match.

For Text columns, the search query is split into multiple tokens based on the tokenizer type set when you created the search index. If no tokenizer was set, single-word tokenization is used by default.

For example, for a Text column that uses single-word tokenization, the search query "this is" can match "..., this is tablestore", "is this tablestore", "tablestore is cool", "this", and "is".

query

Set the query type to matchQuery.

offset

The starting position for the query.

limit

The maximum number of results to return for the query.

To get only the row count without any data, set limit to 0. This does not return any rows.

minimumShouldMatch

The minimum number of matches.

A row is returned only if the value in the `fieldName` column contains at least the minimum number of matching tokens.

Note

`minimumShouldMatch` must be used with the OR logical operator.

operator

The logical operator. The default is OR, which means a row meets the query condition if some of the tokens match.

If you set `operator` to AND, a row meets the query condition only if all tokens are present in the column value.

getTotalCount

Specifies whether to return the total number of matched rows. The default is false, which means the total count is not returned.

Returning the total number of matched rows can affect query performance.

weight

The query weight. This is used for score-based sorting in full-text index scenarios. This parameter specifies the weight for score calculation on a column. A larger value results in a higher score. The value must be a positive floating-point number.

This parameter does not affect the number of results returned, only their scores.

tableName

The name of the data table.

indexName

The name of the search index.

columnsToGet

Specifies whether to return all columns. This includes the `returnAll` and `columns` settings.

`returnAll` is false by default, which means not all columns are returned. In this case, you can use `columns` to specify which columns to return. If you do not specify columns, only the primary key columns are returned.

If you set `returnAll` to true, all columns are returned.

Usage notes

You can use search indexes to sort query results based on the BM25-based keyword relevance score. You cannot specify custom relevance scores to sort query results.

Usage

You can perform a match query using the console, the command line interface, or a software development kit (SDK). Before you begin, complete the following preparations.

Use the console

  1. Go to the Indexes tab.

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.

    4. On the Tables tab of the Instance Details tab, click the name of the data table or click Indexes in the Actions column of the data table.

  2. On the Indexes tab, find the search index that you want to use to query data and click Manage Data in the Actions column.

  3. In the Query Data dialog box, query the data.

    1. By default, the system returns all attribute columns. To return specific attribute columns, turn off All Columns and specify the attribute columns that you want to return. Separate multiple attribute columns with commas (,).

      Note

      By default, the system returns all primary key columns of the data table.

    2. Select the And, Or, or Not logical operator based on your business requirements.

      If you select the And logical operator, data that meets the query conditions is returned. If you select the Or operator and specify a single query condition, data that meets the query condition is returned. If you select the Or logical operator and specify multiple query conditions, data that meets one of the query conditions is returned. If you select the Not logical operator, data that does not meet the query conditions is returned.

    3. Select an index field of the Text type and click Add.

    4. Set the query type for the index field to Match Query (MatchQuery) and enter the value to query.

    5. By default, the sorting feature is disabled. If you want to sort the query results based on specific fields, turn on Sort and specify the fields based on which you want to sort the query results and the sorting order.

    6. By default, the aggregation feature is disabled. If you want to collect statistics on a specific field, turn on Collect Statistics, specify the field based on which you want to collect statistics, and then configure the information that is required to collect statistics.

  4. Click OK.

    Data that meets the query conditions is displayed in the specified order on the Indexes tab.

Use the command line interface

Run the search command in the command line interface to query data using a search index. For more information, see Search index.

  1. Run the search command to query data in the table using the `search_index` search index and return all indexed columns.

    search -n search_index --return_all_indexed
  2. Enter the query conditions as prompted. The following is an example:

    {
        "Offset": -1,
        "Limit": 10,
        "Collapse": null,
        "Sort": null,
        "GetTotalCount": true,
        "Token": null,
        "Query": {
            "Name": "MatchQuery",
            "Query": {
                "FieldName": "col_text",
                "Text": "this is",
                "MinimumShouldMatch": 1
            }
        }
    }

Use an SDK

You can perform a match query using the Java SDK, Go SDK, Python SDK, Node.js SDK, .NET SDK, or PHP SDK. This section uses the Java SDK as an example.

The following sample code provides an example on how to query the rows in which the value of the Col_Keyword column matches "hangzhou" in a table:

/**
 * Query the rows in which the value of the Col_Keyword column matches "hangzhou" in a table. Tablestore returns the total number of rows that meet the query conditions and the specific data of some of these rows. 
 * @param client
 */
private static void matchQuery(SyncClient client) {
    SearchQuery searchQuery = new SearchQuery();
    MatchQuery matchQuery = new MatchQuery(); // Set the query type to MatchQuery. 
    matchQuery.setFieldName("Col_Keyword"); // Specify the name of the column that you want to query. 
    matchQuery.setText("hangzhou"); // Specify the keyword that you want to match. 
    searchQuery.setQuery(matchQuery);
    searchQuery.setOffset(0); // Set offset to 0. 
    searchQuery.setLimit(20); // Set limit to 20 to return up to 20 rows. 
    //searchQuery.setGetTotalCount(true); // Specify that the total number of matched rows is returned. 

    SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
    // Configure the columnsToGet parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned. 
    //SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
    //columnsToGet.setReturnAll(true); // Specify that all columns are returned. 
    //columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Specify the columns that you want to return. 
    //searchRequest.setColumnsToGet(columnsToGet);

    SearchResponse resp = client.search(searchRequest);
    //System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of matched rows instead of the number of returned rows is displayed. 
    System.out.println("Row: " + resp.getRows());
}
            

Billing rules

useVCUmode (formerly reserved mode),usesearch CNYindex queries consumeVCUcompute resources. When you useCUmode (formerly pay-as-you-go mode),usesearch CNYindex queries consumeread throughput. For more search information, seesearch CNYindex billing.

FAQ

References