You can use range query to query data that falls within a specified range. If the type of a field is TEXT, Tablestore tokenizes the string and matches any of the tokens that fall within the specified range.

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
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 query type. To use range query, set this parameter to RangeQuery.
FieldNameThe name of the field that you want to match.
FromThe value from which the query starts.
ToThe value with which the query ends.
IncludeLowerSpecifies whether to include the rows in which the value of column that you want to match is equal to the value of the From parameter in the response. The value type of this parameter is Boolean.
IncludeUpperSpecifies whether to include the rows in which the value of column that you want to match is equal to the value of the To parameter in the response. The value type of this parameter is Boolean.
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 Long_type_col column is greater than or equal to 0 and smaller than 6. 
/// </summary>
/// <param name="otsClient"></param>
public static void RangeQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Specify that the total number of rows that meet the query conditions is returned. 
    searchQuery.GetTotalCount = true;
    var rangeQuery = new RangeQuery(Long_type_col, new ColumnValue(0), new ColumnValue(6));
    // Specify that the rows in which the value of the column that you want to match is equal to the value of the From parameter are included in the response. 
    rangeQuery.IncludeLower = true;
    searchQuery.Query = rangeQuery;
    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. 
}