Scenarios
Many business scenarios require range queries. For example, online-to-offline (O2O) services often search for businesses within a certain range of a user's location. Product searches might query for items within a specific price range. Big data retrieval scenarios also require searching for results within a specific time period.
Usage
To perform a range query, follow these three steps:
1. In the application schema, configure the field type for the fields that require range queries.
Only the following field types support range queries. Configure them as needed.
|
Field type |
Description |
System limits |
|
INT |
An int64 integer. Supports range queries on INT data. |
A maximum of four fields can be of this type. |
|
TIMESTAMP |
An integer timestamp greater than or equal to 0, in milliseconds. Storage supports milliseconds, but query precision is limited to seconds. The last three digits default to 000. This type supports range queries on timestamp data. |
A maximum of four fields can be of this type. |
|
GEO_POINT |
A string in the format `lon lat`. `lon` is the longitude and `lat` is the latitude. Both are double-type values separated by a space. The range for `lon` is [-180, 180], and the range for `lat` is [-90, 90]. |
A maximum of two fields can be of this type. |
2. In the index schema, create an index for the fields that you configured in Step 1 and specify the appropriate analyzer.
Field indexes can be created for TIMESTAMP, GEO_POINT, and INT fields. Compound indexes are not supported for these fields.
The following table describes the analyzers for indexes created on these three field types.
|
Field types for single-field indexes |
Analyzer for range queries |
Analyzer description |
|
INT |
Numerical analysis |
Tokenization method: No tokenization. Index type: Numeric index for range queries. |
|
TIMESTAMP |
Numerical Analysis |
Tokenization method: No tokenization. Index type: Numeric index for range queries. |
|
GEO_POINT |
Geographic location |
Tokenization method: No tokenization. Index type: Numeric index for range queries. |
3. Use the range query syntax to perform a range search.
(1) Range queries for INT and TIMESTAMP
The range query syntax for INT is the same as for TIMESTAMP. The supported syntax is as follows:
index:[number1,number2]// number1 <= index <= number2
index:[number1,number2)// number1 <= index < number2
index:(number1,number2]// number1 < index <= number2
index:(number1,number2)// number1 < index < number2
index:(,number2)// index < number2
index:(number1,)// index > number1
index:(,number2]// index <= number2
index:[number1,)// index >= number1
Note: Do not add quotation marks after the colon that follows the index name.
For example:
To search for "dresses" with a price between 100 and 200 USD:
query=index_text:'dress' AND index_price:[100,200]
To search for news about "Beijing" between June 1, 2019, and June 3, 2019:
query=index_text:'Beijing' AND index_timestamp:[1559318400000,1559577599000]
(2) Range queries for GEO_POINT
Currently, only point, circle, and rectangle queries are supported. The syntax is as follows:
-
Query for a point:
query=spatial_index:'point(LON LAT)'
`LON` is the longitude, and `LAT` is the latitude. For example, `query=spatial_index:'point(116.3906 39.92324)'` retrieves documents at these exact coordinates.
-
Query for points within a circle. This can be used to find points within a certain distance.
query=spatial_index:'circle(LON LAT,Radius)'
`LON` is the longitude, `LAT` is the latitude, and `Radius` is the radius in meters. Performance is optimal for radii within 10 km. Performance degrades significantly for radii greater than 10 km. For example, `query=spatial_index:'circle(116.5806 39.99624, 1000)'` retrieves documents within a 1000-meter (1 km) radius of the coordinate '116.5806 39.99624'.
-
Query for points within a rectangle:
query=spatial_index:'rectangle(minX minY,maxX maxY)'
For latitude, `maxY` must be greater than or equal to `minY`. If the order is reversed, it is automatically adjusted. For longitude, the range from `minX` to `maxX` is interpreted from west to east. If this order is reversed, the range will be incorrect. For example, `rectangle(116.3906 39.92324, 116.3907 39.92325)` retrieves documents within the rectangle formed by these two coordinates.
Limits
-
The query range for the TIMESTAMP field type is from 1970-01-01 00:00 to 2100-01-01 00:00, which corresponds to the millisecond timestamp range of [0, 4102416000000]. Storage supports millisecond precision, but query precision is limited to seconds, and the last three digits of a timestamp are treated as 000. Values greater than 4102416000000 are treated as 4102416000000. To distinguish timestamps greater than 4102416000000, add a filter.
-
For range queries on an index that uses a numeric analyzer, the second value in the range must be greater than or equal to the first value. Otherwise, a syntax error (6112: Query clause error) is returned.
-
The precision range for the GEO_POINT field type is -180 to 180 for longitude and -90 to 90 for latitude.
-
In an ha3 schema scenario, the maximum number of supported range indexes is 8 for Dedicated clusters and 4 for Shared clusters.