Open In App

Geospatial Search and Location-Based Queries

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Location-based services have become virtually unavoidable in today’s environment as they are included in numerous applications and services such as navigation and ride-sharing services, restaurant finders, and social networks. These applications use geographic search and location query to perform search and query data by geographic locations.

This article introduces geospatial search as a new feature in SOLR, outlines its main concepts and syntax, provides examples and explanations, and provides output explanation.

Geospatial search

Geospatial search is similar to full-text search as it requires entering queries based on a specific data set that might contain geo-coordinates. This kind of search is important for applications, that are reconnoitering points of interest (POIs), distance calculations, or search within regions. Location-based queries are queries that work by filtering as well as retrieving data according to geographical parameters like coordinates and distance.

The search and analytics engine Elasticsearch offers rich support for geographical data and operations. It provides a range of geospatial data types and enables several geospatial queries like geo-distance, geo-bounding box, geolocation, geopolygon, and geographic coordinate system.

Syntax for Geospatial Queries

1. Geo-point Data Type

Represents a geographical point using two coordinates in a geographical space: latitude and longitude.

{
"location": {
"type": "geo_point"
}
}

2. Geo-distance Query

Finds documents within a specified distance from a point.

{
"query": {
"geo_distance": {
"distance": "200km",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}

3. Geo-bounding Box Query

Finds documents within a specified rectangle defined by top-left and bottom-right coordinates.

{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -71.12
}
}
}
}
}

4. Geo-polygon Query

Finds documents within a specified polygon.

{
"query": {
"geo_polygon": {
"location": {
"points": [
{ "lat": 40.73, "lon": -74.1 },
{ "lat": 40.01, "lon": -71.12 },
{ "lat": 41.12, "lon": -71.12 }
]
}
}
}
}

Examples with Explanation of the Output

Example 1: Geo-distance Query

Suppose we have the dataset of restaurants saved in elasticsearch and each restaurant has the coordinates of its position saved as the geographical coordinates. There are times that one needs to get a restaurant that is near a given place up to a maximum of 10 Kilometres.

Step 1: Create an Index with Geo-point Mapping

First, we need to create an index with the geo_point mapping for the location field.

PUT /restaurants
{
"mappings": {
"properties": {
"name": { "type": "text" },
"location": { "type": "geo_point" }
}
}
}

Use a tool like Kibana Dev Tools or a REST client like Postman to send this request to your Elasticsearch server.

Step 2: Index Sample Data

Next, we index some sample restaurant data into Elasticsearch.

Request:

POST /restaurants/_doc/1
{
"name": "Central Perk",
"location": { "lat": 40.7128, "lon": -74.0060 }
}

POST /restaurants/_doc/2
{
"name": "Monk's Cafe",
"location": { "lat": 40.7306, "lon": -73.9352 }
}

Send each POST request separately to add documents to your Elasticsearch index.

Step 3: Perform Geo-distance Query

Now, we perform a geo_distance query to find restaurants within a 10km radius of a specific location.

Request:

GET /restaurants/_search
{
"query": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}

Output Explanation

This query returns restaurants within a 10km radius of the given coordinates. The result includes "Central Perk" as it falls within the specified distance.

Output:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "restaurants",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "Central Perk",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
]
}
}

Example 2: Geo-bounding Box Query

We want to find restaurants within a specific geographical rectangle.

Step 1: Perform Geo-bounding Box Query

Request:

GET /restaurants/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left": {
"lat": 40.73,
"lon": -74.1
},
"bottom_right": {
"lat": 40.01,
"lon": -73.12
}
}
}
}
}

Output Explanation

This query returns restaurants within the specified rectangle. Any restaurant with coordinates falling inside the rectangle will be included in the results.

Output:

{
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "restaurants",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "Central Perk",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
},
{
"_index": "restaurants",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "Monk's Cafe",
"location": {
"lat": 40.7306,
"lon": -73.9352
}
}
}
]
}
}

Conclusion

Geospatial searching and location-orientation queries are decisive for applications that require sorting and evaluation of data according to geographic coordinates. These operations are efficiently done with the help of different types of queries supported by Elasticsearch such as geo distance, geo bounding box, geo polygon, etc. With such capabilities in place, developers are in a position to deliver meaningful and location-conscious functions to users.


Article Tags :

Explore