When you perform a wildcard query, you can use the asterisk (*) and question mark (?) wildcard characters in the query to search for data. The asterisk (*) matches a string of any length at, before, or after a search term. The question mark (?) matches a single character in a specific position. The string can start with an asterisk (*) or a question mark (?). For example, if you search for the "table*e" string, "tablestore" can be matched.

The *word* string is equivalent to the WHERE field_a LIKE '%word%' clause in SQL. If you want to search for the *word* string, you can perform a fuzzy query that provides higher performance than a wildcard query. For more information about how to perform a fuzzy query, see Fuzzy query. If you perform a fuzzy query, the query performance is not compromised when the data volume increases.

Limit

A string that contains wildcards can be up to 32 characters in length.

API operations

You can call the Search or ParallelScan operation and set the query type to WildcardQuery to perform a wildcard query.

Tablestore SDKs

You can use the following Tablestore SDKs to perform a wildcard query:

Parameters

Parameter Description
fieldName The name of the column.
value The string that contains wildcards. The string cannot exceed 32 characters in length.
query The type of the query. Set the query type to WildcardQuery.
getTotalCount Specifies whether to return the total number of rows that match the query conditions. The default value of this parameter is false, which indicates that the total number of rows that match the query conditions is not returned.

If this parameter is set to true, the query performance is compromised.

tableName The name of the data table.
indexName The name of the search index.
columnsToGet Specifies whether to return all columns of each row that meets the query conditions. You can configure returnAll and columns for this parameter.

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

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

Examples

/**
 * Search the table for rows in which the value of the Col_Keyword column matches "hang*u". 
 * @param client
 */
private static void wildcardQuery(SyncClient client) {
    SearchQuery searchQuery = new SearchQuery();
    WildcardQuery wildcardQuery = new WildcardQuery(); // Set the query type to 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 rows that meet the query conditions is returned. 

    SearchRequest searchRequest = new SearchRequest("sampleTable", "sampleSearchIndex", searchQuery);
    // You can use the columnsToGet parameter to specify the columns that you want to return or specify that all columns are returned. If you do not specify this parameter, only the primary key columns are returned. 
    //SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
    //columnsToGet.setReturnAll(true); // Set ReturnAll to true to return all columns. 
    //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 rows that meet the query conditions instead of the number of returned rows is displayed. 
    System.out.println("Row: " + resp.getRows());
}