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
table_nameThe name of the table.
index_nameThe name of the search index.
offsetThe position from which the current query starts.
limitThe maximum number of rows that the current query returns.

To query only the number of matched rows but not detailed data, you can set limit to 0. In this case, Tablestore returns the number of matched rows without data from the table.

get_total_countSpecifies whether to return the total number of rows that match the query conditions. By default, this parameter is set to False, which indicates that the total number of rows that match the query conditions is not returned.

Query performance is affected when this parameter is set to True.

query_typeThe query type. To use range query, set this parameter to QueryTypeConst::RANGE_QUERY.
field_nameThe name of the field used to match the query conditions.
range_fromThe value from which the query starts.
range_toThe value with which the query ends.
include_lowerSpecifies whether to include the value of range_from in the response. The value type is Boolean.
include_upperSpecifies whether to include the value of range_to in the response. The value type is Boolean.
sortThe method that you want to use to sort the rows in the response. For more information, see Sorting and paging.
columns_to_getSpecifies whether to return all columns of each matched row. You can configure return_type and column_names for this parameter.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_SPECIFIED, you can use return_names to specify the columns that you want to return.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_ALL, all columns are returned.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_ALL_FROM_INDEX, all columns in the search index are returned.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_NONE, only the primary key columns are returned.

Examples

$request = array(
    'table_name' => 'php_sdk_test',
    'index_name' => 'php_sdk_test_search_index',
    'search_query' => array(
        'offset' => 0,
        'limit' => 2,
        'get_total_count' => true,
        'query' => array(
            'query_type' => QueryTypeConst::RANGE_QUERY,
            'query' => array(
                'field_name' => 'long',
                'range_from' => 100,
                'include_lower' => true,
                'range_to' => 101,
                'include_upper' => false
            )
        ),
        'sort' => array(
            array(
                'field_sort' => array(
                    'field_name' => 'keyword',
                    'order' => SortOrderConst::SORT_ORDER_ASC
                )
            ),
        )
    ),
    'columns_to_get' => array(
        'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
        'return_names' => array('double', 'long', 'keyword')
    )
);
$response = $otsClient->search($request);