Wildcard query

更新时间:
复制 MD 格式

A wildcard query (WildcardQuery) performs a fuzzy query, similar to the LIKE operator in a database.

Note

To implement the NOT LIKE functionality, you must use a WildcardQuery together with the mustNotQueries parameter in a boolean query. For more information, see Boolean query.

Overview

A wildcard query uses special characters for pattern matching, which improves search precision and efficiency.

When you use the WildcardQuery feature, you can use a query string with wildcards to match data. This feature supports the asterisk (*) and question mark (?) wildcards. The asterisk (*) represents any sequence of characters, and the question mark (?) represents any single character. A query string can start with an asterisk (*) or a question mark (?). For example, a query for table*e can match tablestore.

The WildcardQuery is supported for fields of the Keyword and FuzzyKeyword data types.

  • Keyword: A basic string data type. The performance of fuzzy queries, such as a wildcard query, on Keyword fields is poor for large datasets and degrades as data volume grows.

  • FuzzyKeyword: A data type optimized for fuzzy queries like wildcard queries. It delivers superior and stable performance that does not degrade significantly as dataset size increases.

Search Index provides three types of wildcard queries to support various fuzzy matching requirements. For more information, see the following table.

Note

This topic describes how to perform a WildcardQuery on fields of the Keyword and FuzzyKeyword types. To learn about implementing fuzzy queries on fields of the Text type, see Wildcard query based on tokenization.

Type

Query method

Advantage

Disadvantage

Keyword

WildcardQuery

Compatible with Elasticsearch.

Query performance degrades as the amount of indexed data grows.

FuzzyKeyword

WildcardQuery

Delivers superior and stable performance that does not degrade significantly as the data size increases.

Causes some data expansion.

Text

MatchPhraseQuery

Supports case-insensitive matching.

Causes some data expansion.

Usage notes

The query term in a WildcardQuery cannot exceed 32 characters for both Keyword and FuzzyKeyword fields.

API

To perform a wildcard query, call the Search or parallel scan operation and set the query type to WildcardQuery.

Parameters

Parameter

Description

fieldName

The name of the column to query.

value

The string containing wildcards, which cannot exceed 32 characters.

query

The query type. Set this parameter to WildcardQuery.

getTotalCount

Specifies whether to return the total number of matched rows. The default value is false.

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

weight

The query weight, a positive floating-point number used for score-based sorting in full-text search. A higher weight assigns a higher score to the column in the query results.

This parameter affects the scores of the returned results but does not affect the number of returned results.

tableName

The name of the data table.

indexName

The name of the Search Index.

columnsToGet

Specifies which columns to return. It includes the returnAll and columns settings.

By default, returnAll is false, which indicates that not all columns are returned. In this case, you can use columns to specify the columns to return. If you do not specify any columns, only primary key columns are returned.

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

How to use

You can perform a wildcard query using the Tablestore console, a command-line tool (CLI), or an SDK. Before you start, complete the following preparations:

Important

Currently, a WildcardQuery on a FuzzyKeyword field is only supported via Tablestore SDKs.

Use the console

  1. Go to the Index Management tab.

    1. Log on to the Table Store console.

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

    3. On the Overview page, click the instance name or click Instance Management in the Actions column.

    4. On the Instance Details tab, in the Data Table List tab, click the data table name or click Index Management in the Actions column.

  2. On the Index Management tab, find the target Search Index and click Search in the Actions column.

  3. In the Search dialog box, configure the query.

    1. By default, all columns are returned. To return specific columns, turn off Retrieve All Columns and enter the column names, separated by commas.

      Note

      By default, Table Store returns the primary key columns of the data table.

    2. Select a logical operator: And, Or, or Not.

      If you select And, the query returns data that meets all specified conditions. If you select Or, the query returns data that meets at least one of the specified conditions. If you select Not, the query returns data that does not meet the specified conditions.

    3. Select an index field and click Add.

    4. Set the query type for the index field to WildcardQuery and enter a value that contains wildcards.

    5. By default, sorting is disabled. To sort the results by a specific field, turn on Enable Sorting, add the sort field, and configure the sort order.

    6. By default, aggregation is disabled. To perform statistical aggregation on a specific field, turn on Enable Aggregation, add the field for aggregation, and configure the aggregation settings.

  4. Click OK.

    The query results are displayed on the Index Management tab.

Use the command-line tool

You can run the search command in a command-line tool (CLI) to query data by using a Search Index. For more information, see Search Index.

Important

The CLI currently supports WildcardQuery only for the Keyword type, not for the FuzzyKeyword type.

  1. Run the search command to query data in the table by using the search_index Search Index, which returns all indexed columns.

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

    {
        "Offset": -1,
        "Limit": 10,
        "Collapse": null,
        "Sort": null,
        "GetTotalCount": true,
        "Token": null,
        "Query": {
            "Name": "WildcardQuery",
            "Query": {
                "FieldName": "col_keyword",
                "Value": "hang*u"
            }
        }
    }

Use an SDK

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

Note

The query statement is identical for both the Keyword and FuzzyKeyword types. The only difference is the data type of the column being queried.

The following sample code provides an example on how to query rows in which the value of the Col_Keyword column matches the "hang*u" pattern.

/**
 * Search the table for rows in which the value of the Col_Keyword column matches the "hang*u" pattern. 
 * @param client
 */
private static void wildcardQuery(SyncClient client) {
    SearchQuery searchQuery = new SearchQuery();
    WildcardQuery wildcardQuery = new WildcardQuery(); // Use WildcardQuery. 
    wildcardQuery.setFieldName("Col_Keyword");
    wildcardQuery.setValue("hang*u"); // Specify a string that contains one or more wildcard characters in wildcardQuery. 
    searchQuery.setQuery(wildcardQuery);
    //searchQuery.setGetTotalCount(true); // Specify that the total number of matched rows is returned. 

    SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
    // You can 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()); // Display the total number of matched rows instead of the number of returned rows. 
    System.out.println("Row: " + resp.getRows());
}

Billing

In VCU mode (formerly reserved mode), Search Index queries consume VCU compute resources. In CU mode (formerly pay-as-you-go mode), they consume read throughput. For more information, see Search Index metering and billing.

FAQ

Related topics