You can use prefix query to query data that matches a specified prefix. If the type of a field is TEXT, Tablestore tokenizes the string and matches tokens by using the specified prefix.

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
FieldNameThe name of the field that you want to match.
PrefixThe prefix.

If the field used to match the prefix is a TEXT field, the field values are tokenized. A row meets the query conditions when at least one token contains the specified prefix.

GetTotalCountSpecifies 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 this parameter to true, the query performance is compromised.

QueryThe type of the query. Set the query type to PrefixQuery.
TableNameThe name of the table.
IndexNameThe name of the search index.
ColumnsToGetSpecifies 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>
/// Search the table for rows in which the value of the Keyword_type_col column matches the "Search" prefix. 
/// </summary>
/// <param name="otsClient"></param>
public static void PrefixQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Set the query type to PrefixQuery, the field that you want to match to Keyword_type_col, and the prefix to "Search". 
    searchQuery.Query = new PrefixQuery(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. 
}