All Products
Search
Document Center

Tablestore:Terms query

Last Updated:Apr 30, 2026

A terms query matches rows where a field value exactly equals any one of multiple specified keywords. It is the equivalent of the SQL IN operator and extends the single-keyword term query to support multiple keywords.

Prerequisites

Parameters

Parameter

Description

TableName

The name of the data table.

IndexName

The name of the search index.

Query

The query type. Set to TermsQuery.

FieldName

The name of the field to match against.

Terms

The keywords to match against the field value. A row is returned if its field value exactly matches at least one keyword.

Limit

The maximum number of rows to return.

Set to 0 to return only the count of matching rows without retrieving row data.

ColumnsToGet

The columns to return for each matching row. Configure using ReturnAll, Columns, or ReturnAllFromIndex.

By default, ReturnAll is false, which means only primary key columns are returned. To return additional columns, use one of the following options:

  • Set Columns to specify which columns to return.

  • Set ReturnAllFromIndex to true to return all columns in the search index.

Set ReturnAll to true to return all columns in the data table.

Examples

The following example queries rows where Col_Keyword exactly matches "SearchIndex" or "Sample":

/// <summary>
/// Search the table for rows in which the value of the Col_Keyword column 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);
    // Specify columns to return: by search index, by name, or all columns.
    // If not specified, only primary key columns are returned.
    searchRequest.ColumnsToGet = new ColumnsToGet
    {
        // Return all columns in the search index.
        ReturnAllFromIndex = true
        // Return specific columns by name.
        //Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
        // Return all columns in the data table.
        //ReturnAll = true         
    };

    SearchResponse searchResponse = otsClient.Search(searchRequest);

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

FAQ

References

  • When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, and exists query. You can use different query methods to query data from multiple dimensions based on your business requirements.

    You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.

    You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).

  • If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.

  • If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.