Scenarios
Many business scenarios involve range searches. For example, in online to offline (O2O) business scenarios, you may want to search for merchants within a specific range of your geographical location. You may want to search for commodities within a specific price range. You may want to retrieve a large amount of data in the background to search for results in a specific time period.
Procedure
You can perform the following steps to implement a range search:
1. When you define the application schema, configure the field type for the field that you want to use to perform a range search.
The following table describes the field types that can be used for range searches. Configure the field type as required.
Type | Description | Remark |
INT | The INT64 type. Fields of the INT type can be used to perform range searches. | A maximum of four fields of this type are supported. |
TIMESTAMP | The integer timestamp that is greater than or equal to 0, in milliseconds. The unit of the stored timestamp is millisecond. The unit of the timestamp that you can search is second. By default, the last three digits are set to 000. Fields of the TIMESTAMP type can be used to perform range searches. | A maximum of four fields of this type are supported. |
GEO_POINT | The STRING type, in the format of lon lat. lon specifies the longitude, and lat specifies the latitude. Both the longitude and latitude values must be of the DOUBLE type and are separated by a space character. Valid values of the lon parameter: [-180,180]. Valid values of the lat parameter: [-90,90]. | A maximum of two fields of this type are supported. |
2. When you define the index schema, create an index for the field of the field type that is defined in Step 1 and specify a corresponding analyzer.
You can create single-field indexes, but not composite indexes, for the fields of the TIMESTAMP, GEO_POINT, and INT types.
The following table describes the analyzers for the indexes that are created based on the three field types.
Field type of a single-field index | Analyzer for a range search | Analyzer description |
INT | Numerical value analysis | No analysis is required. You can use this analyzer if you want to perform range searches based on the indexes of the numeric data type. |
TIMESTAMP | Numerical value analysis | No analysis is required. You can use this analyzer if you want to perform range searches based on the indexes of the numeric data type. |
GEO_POINT | Geographical location | No analysis is required. You can use this analyzer if you want to perform range searches based on the indexes of the numeric data type. |
3. Use the range search syntax in queries to perform range searches.
(1) Range searches of the INT and TIMESTAMP types
The range search syntax of the INT type is the same as that of the TIMESTAMP type. The following search syntax is supported:
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 use single quotation marks (") or double quotation marks ("") after the colon (:).
Examples:
Search for "Dresses" in a price range from CNY 100 to CNY 200 in commodities:
query=index_text: "Dresses" AND index_price:[100,200]
Search for news about "Beijing" from June 1, 2019 to June 3, 2019:
query=index_text:'Beijing' AND index_timestamp:[1559318400000,1559577599000]
(2) Range searches of the GEO_POINT type
You can perform range searches of the GEO_POINT type to search for geological points, circles, and rectangles. You can use the following syntax:
Search for geological points:
query=spatial_index:'point(LON LAT)'
LON specifies the longitude, and LAT specifies the latitude. For example, you can use query=spatial_index:'point(116.3906 39.92324)' to search for geological points with the same coordinates.
Search for geographical points within a circle whose radius can be several kilometers:
query=spatial_index:'circle(LON LAT,Radius)'
LON specifies the longitude. LAT specifies the latitude. Radius specifies the radius. Unit: meters. If range searches are performed within a circle whose radius is less than or equal to 10 kilometers, optimal performance can be achieved. If the radius exceeds 10 kilometers, the performance greatly deteriorates. For example, you can use query=spatial_index:'circle(116.5806 39.99624, 1000)'
to search for geographical points within a circle with a radius of 1,000 meters near the geological point whose longitude and latitude values are 116.5806 and 39.99624.
Search for geographical points within a rectangle:
query=spatial_index:'rectangle(minX minY,maxX maxY)'
In latitude, the value of the maxY parameter must be greater than or equal to that of the minY parameter. Otherwise, the values of the maxY and minY parameters are automatically adjusted. The minX and maxX parameters specify longitudes from west to east. If the values of the minX and maxX parameters are inverted, the longitudinal range changes. For example, you can use rectangle(116.3906 39.92324, 116.3907 39.92325) to search for geological points within the rectangle formed by these coordinates.
Usage notes
The values of the TIMESTAMP type range from 1970-01-01 00:00 to 2100-01-01 00:00, precise to milliseconds. The unit of the stored timestamp is millisecond. The unit of the timestamp that you can search is second. By default, the last three digits are set to 000. Valid values: [0,4102416000000]. If a value is greater than 4102416000000, 4102416000000 is used. If you want to distinguish the values that are greater than 4102416000000 from other values, add a filter clause for filtering.
If query searches are performed based on the indexes for numerical value analysis, the second value in the value interval must be greater than the first value. Otherwise, the following syntax error whose error code is 6112 is returned: Query clause error.
The field of the GEO_POINT type has a longitudinal value range from -180 to 180 and a latitudinal value range from -90 to 90.