You can use term query to query data that exactly matches the specified value of a field. Term query is similar to queries based on string match conditions. If the type of a field is TEXT, Tablestore tokenizes the string and exactly matches tokens.

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

Parameter Description
FieldName The name of the field that you want to match.
Term The keyword that is used to match the field values when you perform a term query.

This word is not tokenized. Instead, the entire word is used to match the field values.

If the type of a field is TEXT, Tablestore tokenizes the string and exactly matches tokens. For example, TEXT string "tablestore is cool" is tokenized into "tablestore", "is", and "cool". When you specify one of these tokens as a search string, you can retrieve query results that contain "tablestore is cool".

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

Query The query type. To use term query, set this parameter to TermQuery.
TableName The name of the 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, 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 exactly matches "SearchIndex". 
/// </summary>
/// <param name="otsClient"></param>
public static void TermQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Specify that the total number of rows that meet the query conditions is returned. 
    searchQuery.GetTotalCount = true;
    // Set the query type to TermQuery, the field that you want to match to Keyword_type_col, and the keyword to "SearchIndex". 
    searchQuery.Query = new TermQuery(Keyword_type_col, new ColumnValue("SearchIndex"));

    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);
    // Check the value of NextToken. 
}