This query is similar to a term query. A terms query supports multiple terms. A row of data is returned if at least one of the keywords matches the field value. Terms queries can be used in the same manner as the IN operator in SQL statements.

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
TableNameThe name of the table.
IndexNameThe name of the search index.
QueryThe type of the query. Set the value to TermsQuery.
FieldNameThe name of the field that you want to match.
TermsThe keywords that are used to match the field values when you perform a terms query.

A row of data is returned if at least one of the keywords matches the field value.

LimitThe maximum number of rows that you want the current query to return.

To query only the number of rows that meet the query conditions without returning specific data, you can set Limit to 0. This way, Tablestore returns the number of rows that meet the query conditions without specific data from the table.

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 Col_Keyword exactly matches "SearchIndex" or "Sample". 
/// </summary>
/// <param name="otsClient"></param>
public static void TermsQuery(OTSClient otsClient)
{
    TermsQuery termsQuery = new TermsQuery();
    termsQuery.FieldName = "Col_Keyword";
    termsQuery.Terms = new List<ColumnValue>
    {
        new ColumnValue("SearchIndex"),
        new ColumnValue("Sample")
    };

    SearchQuery searchQuery = new SearchQuery();
    searchQuery.Query = termsQuery;

    SearchRequest searchRequest = 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. 
    searchRequest.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         
    };

    SearchResponse searchResponse = otsClient.Search(searchRequest);

    Console.WriteLine(JsonConvert.SerializeObject(searchResponse));
}