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 MustNotQueries of BoolQuery.

Prerequisites

  • An OTSClient instance 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.
FieldNameThe name of the column.
ValueThe string that contains wildcard characters. The string cannot exceed 32 characters in length.
TableNameThe name of the table.
IndexNameThe name of the search index.
GetTotalCount Specifies whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which indicates that the total number of rows that meet the query conditions is not returned.

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

ColumnsToGet Specifies whether to return all columns of each row that meets the query conditions. You can configure ReturnAll, Columns, and ReturnAllFromIndex for this parameter.
The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
  • Configure Columns to specify the columns that you want to return.
  • Set ReturnAllFromIndex to true to return all columns from the search index.

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

Examples

/// <summary>
/// Use wildcard query to search the table for rows in which the value of the Keyword_type_col column matches "Search*". 
/// </summary>
/// <param name="otsClient"></param>
public static void WildcardQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Set the query type to WildcardQuery. The string based on which you want to match column values can contain wildcard characters. 
    searchQuery.Query = new WildcardQuery(Keyword_type_col, "*Search*");
    // Specify that the total number of rows that meet the query conditions is returned. 
    searchQuery.GetTotalCount = true;
    var request = new SearchRequest(TableName, IndexName, searchQuery);
    // You can use the ColumnsToGet parameter to specify the columns that you want to return, specify that all columns are returned, or specify that all columns in the search index are returned. If you do not configure this parameter, only the primary key columns are returned. 
    request.ColumnsToGet = new ColumnsToGet()
    {
        // Specify that all columns in the search index are returned. 
        ReturnAllFromIndex = true
        // Specify the columns that you want to return. 
        //Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
        // Specify that all columns are returned. 
        //ReturnAll = true
    };

    var response = otsClient.Search(request);

    Console.WriteLine("Total Count:" + response.TotalCount); // Specify that the total number of rows that meet the query conditions instead of the number of rows that are returned is displayed. 
}