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.

Prerequisites

  • The OTSClient is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.
  • A search index is created for the data table. For more information, see Create search indexes.

Parameters

Parameter Description
query The type of the query. Set the query type to WildcardQuery.
field_name The name of the column.
value The string that contains wildcards. The string cannot exceed 32 characters in length.
table_name The name of the data table.
index_name The name of the search index.
limit The maximum number of rows that you want the current query to return.
get_total_count Specifies whether to return the total number of matched rows. Default value: False.

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

ColumnsToGet Specifies whether to return all columns of each matched row.
  • If you set the return_type parameter to ColumnReturnType.SPECIFIED, you must specify the columns that you want to return.
  • If you set the return_type parameter to ColumnReturnType.ALL, all columns are returned.
  • If you set the return_type parameter to ColumnReturnType.NONE, only the primary key columns are returned.

Examples

Search the table for rows in which the value of the k column matches 'key00*'.

  • Perform a wildcard query by using Tablestore SDK for Python V5.2.1 or later
    By default, if you use Tablestore SDK for Python V5.2.1 or later to perform a wildcard query, a SearchResponse object is returned. The following code provides a sample request:
    query = WildcardQuery('k', 'key00*')
    search_response = client.search(
        table_name, index_name, 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )
    You can use the following sample request to return results of the Tuple type:
    query = WildcardQuery('k', 'key00*')
    rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search(
        table_name, index_name, 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    ).v1_response()
  • Perform a wildcard query by using a version of Tablestore SDK for Python that is earlier than V5.2.1

    By default, if you use a version of Tablestore SDK for Python that is earlier than V5.2.1 to perform a wildcard query, results of the Tuple type are returned. The following code provides a sample request:

    query = WildcardQuery('k', 'key00*')
    rows, next_token, total_count, is_all_succeed = client.search(
        table_name, index_name, 
        SearchQuery(query, limit=100, get_total_count=True), 
        ColumnsToGet(return_type=ColumnReturnType.ALL)
    )