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.
You have an Alibaba Cloud account or a RAM user that has permissions to perform operations on Tablestore. For information about how to grant Tablestore operation permissions to a RAM user, see Use a RAM policy to grant permissions to a RAM user.
If you want to use Tablestore SDKs or the Tablestore CLI to perform a query, an AccessKey pair is created for your Alibaba Cloud account or RAM user. For more information, see Create an AccessKey pair.
A data table is created. For more information, see Operations on tables.
A search index is created for the data table. For more information, see Create a search index.
If you want to use Tablestore SDKs to perform a query, an OTSClient instance is initialized. For more information, see Initialize a Tablestore client.
If you want to use the Tablestore CLI to perform a query, the Tablestore CLI is downloaded and started, and information about the instance that you want to access and the data table are configured. For more information, see Download the Tablestore CLI, Start the Tablestore CLI and configure access information, and Manage data tables.
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
When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, fuzzy query, Boolean query, geo query, nested query, KNN vector query, and exists query. You can select query methods based on your business requirements to query data from multiple dimensions.
You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Perform sorting and paging.
You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (remove duplicates).
If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.
If you want to 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.