You can use match all query to match all rows in a table to query the total number of rows in the table and return multiple random rows.

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 query type. Set the query type to MatchAllQuery.
TableNameThe name of the table.
IndexNameThe name of the search index.
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.

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.

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>
/// Use match all query to query the total number of rows in a table. 
/// </summary>
/// <param name="otsClient"></param>
public static void MatchAllQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    searchQuery.Query = new MatchAllQuery();
    searchQuery.GetTotalCount = true; // Set GetTotalCount to true to return the total number of rows that meet the query conditions. 
    /*
    * In the result of a match all query, the value of TotalCount indicates the total number of rows in the table. 
    * 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. 
    */
    searchQuery.Limit = 0;
    var request = new SearchRequest(TableName, IndexName, searchQuery);

    var response = otsClient.Search(request);
    // Check whether the returned total number of rows that meet the query conditions is correct. If isAllSuccess is false, Tablestore may fail to query data on all servers and return a value that is smaller than the total number of rows that meet the query conditions. 
    Console.WriteLine("IsAllSuccess:" + response.IsAllSuccess);
    Console.WriteLine("Total Count:" + response.TotalCount);
}