All Products
Search
Document Center

OpenSearch:Proximity search

Last Updated:Jun 20, 2026

OpenSearch supports proximity searches for entities such as people or places. You can pass coordinates to improve search efficiency and sort the results by distance. This topic describes how to configure this feature.

Solution

  1. Configure a field of the GEO_POINT type in the application schema and a geo-location index to retrieve results.

  2. Configure a data source processing plugin for the GEO_POINT field.

  3. Use the search syntax to test queries and add the sorting feature.

Procedure

  1. Configure the application schema

In the OpenSearch application schema, add fields for geographical coordinates: lon of the DOUBLE type and lat of the DOUBLE type. Then, create a field named company_lon_lat of the GEO_POINT type. In the index schema, add an index named company_lon_lat that includes the company_lon_lat field, and select Geographic Location Analyzer as its analyzer. In the attribute field list, find the company_lon_lat field and click Add as attribute field. This makes the field available in filter, aggregate, sort, and distinct clauses. For the full application creation process, see Build an application.

  1. Configure the data source

When you configure field mapping, use the StringCatenateExtractor plugin for the company_lon_lat application schema field. This plugin concatenates the lon (longitude) and lat (latitude) fields and writes the result to the target field company_lon_lat.

In the field delimiter text box, enter a single space.

Note: You can leave the mapping field for the target field company_lon_lat empty.

For details about the plugin, see Data source plugins.

In the console, leave the mapping for the target field company_lon_lat empty.

  1. Test the search

Sample query: query=name:'Alibaba' AND company_lon_lat:'circle(116.5806 39.99624, 1000)'.

Description: This query searches for documents where the company name is Alibaba and the location is within 1,000 meters (1 km) of the coordinates '116.5806 39.99624'.

Syntax: query=spatial_index:'circle(LON LAT,Radius)'.

  • LON represents longitude, LAT represents latitude, and Radius is the radius in meters. For optimal performance, the radius should be within 10 km. Performance degrades significantly for radii greater than 10 km.

  • For more information about the features and syntax, see Range query.

  1. Add a refined sort expression

Sample expression: (distance(company_lon_lat,long_lat_in_query,distance)+1))*10000.

Function: Sorts the retrieved documents by distance.

Syntax: distance(location1, location2, outputname, defaultvalue).

  • location1: The name of a field of the GEO_POINT type.

  • location2: A variable name from the kvpairs clause of the query string. Its value must be in the LON LAT format required for the GEO_POINT type.

  • outputname: Optional. If you need the calculated distance returned in the results, specify a name for the output field.

  • defaultvalue: Optional. Specifies the distance value to return if a document's location1 value is invalid. If this parameter is not specified, the default value is 100000.

Note: In the example, long_lat_in_query must be set in the kvpairs clause. For example: kvpairs=long_lat_in_query:120.34256 30.56982.