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.

Note If you want to use the NOT LIKE operator, you must use WildcardQuery together with must_not_queries of BoolQuery.

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

ParameterDescription
queryThe type of the query. Set the query type to WildcardQuery.
field_nameThe name of the column.
valueThe string that contains wildcard characters. The string cannot exceed 32 characters in length.
table_nameThe name of the data table.
index_nameThe name of the search index.
limitThe maximum number of rows that you want the current query to return.
get_total_countSpecifies 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.

ColumnsToGetSpecifies 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)
    )