OpenSearch allows you to implement features of searching for people or locations nearby. This topic describes how to search for people or locations nearby by passing in the coordinates of a specific person or location. This can help improve the search efficiency. In addition, you can implement sort features for search results.
Configure the fields of the GEO_POINT type for the application schema and configure geo-location indexes for data retrieval.
Set the data source processing plug-in for the fields of the GEO_POINT type.
Use search test syntax and add sort features.
Configure the application schema
When you manually create the application schema in the OpenSearch console, add the lon and lat fields of the DOUBLE type, which indicate the longitude and latitude. Then, add the company_lon_lat field of the GEO_POINT type. You can set the names of the fields as needed. In the Index Schema step, select Geographic Location Analyzer for the company_lon_lat field and set it as an attribute field. For more information about how to create an application in the OpenSearch console, see Build an application.
2. Configure the data source
When you configure field mappings, use the StringCatenateExtractor plug-in for the company_lon_lat field to concatenate the lon and lat fields. Use spaces as the field delimiter to concatenate the values of the lon and lat fields to the company_lon_lat field. Note: The mapping field of the company_lon_lat field can be ignored. For more information about plug-ins, see Use data processing plug-ins.
3. Run a search test
query=name:'Alibaba' AND company_lon_lat:'circle(116.5806 39.99624, 1000)'. Description: Search for the documents of Alibaba within 1,000 meters from the source location with the coordinates of 116.5806 and 39.99624. Syntax:
LON indicates the longitude. LAT indicates the latitude. Radius indicates the radius. Unit: meters. To obtain the optimal search effect, we recommend that you set a radius within 10 kilometers from the source location.
For more information about specific features and syntax, see Range search.
4. Add fine sort expressions
(distance(company_lon_lat,long_lat_in_query,distance)+1))*10000. Feature: Sorts the documents that are retrieved from near to far. Syntax:
distance(location1, location2, outputname, defaultvalue).
location1: the name of a field of the GEO_POINT type.
location2: the name of a field that is set in the kvpairs clause in the query string. The value of the field is in the same format as a field of the GEO_POINT type: Longitude Latitude.
outputname: the return value. This parameter is optional. You can specify this parameter to obtain the calculated distance in the search results.
defaultvalue: the distance value to be returned when the value of the location1 parameter is invalid. This parameter is optional. If this parameter is not specified, 100000 is returned by default.
Description: Set the long_lat_in_query parameter in the kvpairs clause. Example: