Elasticsearch in Action: Introducing Location Search

Madhusudhan Konda
4 min readJan 22, 2023
Excerpts taken from my upcoming book: Elasticsearch in Action

The excerpts are taken from my book Elasticsearch in Action, Second Edition. The code is available in my GitHub repository. You can find executable Kibana scripts in the repository so you can run the commands in Kibana straight away. All code is tested against Elasticsearch 8.4 version.

Me @ Medium || LinkedIn || Twitter || GitHub

In this day and age of the internet and information, it is a common requirement to enable location-based search in apps and applications. Location-based search fetches venues or places based on proximity such as nearby restaurants, houses for sale not farther than 1 km radius, and so on. We also use location-based searches for finding directions to a place or point of interest.

The good news is that geospatial support is a first-class citizen in Elasticsearch. Dedicated data types allow us to define a schema for indexing geospatial data, thus enabling a focused search.

Elasticsearch provides a set of geospatial search queries such as bounding_box, geo_distance, and geo_shape, depending on the given use case. These queries suffice for most of the use cases. Each of these queries satisfies a set of requirements briefly discussed in this section.

The bounding box query

We, at times, may want to find out a list of locations such as restaurants, schools, or universities in a surrounding area; let’s say in a square or a rectangular. We can construct a rectangle, often called a georectangle, by taking the set of coordinates of the top-left and bottom-right corners. These coordinates consist of a pair of longitude and latitude measurements, representing these corners.

Elasticsearch provides a bounding_boxquery that lets us search required addresses fitting in a georectangle. This query fetches the points of interest (as query criteria) inside the georectangle constructed by our set of coordinates. For example, figure shown below indicates the addresses enclosed in one such georectangle.

Figure : The georectangle constructed with an appropriate set of longitude and latitude coordinates.

As marked by the rectangle in the previous figure, we are searching for addresses in central London in an area highlighted in the georectangle. Addresses intersecting this rectangle are returned as positive results. We will learn and run through some bounding_boxqueries in detail shortly.

The geo_distance query

You may have watched Hollywood movies where an FBI agent is trying to pin down the fugitive in an area drawn as a circle around a central focal point. That’s exactly what the geo_distancequery does!

Elasticsearch provides the geo_distancequery to fetch the addresses in an area enclosed by a circle. The given center is defined by longitude and latitude and a radius as the distance. The figure given below demonstrates the geodistance concept pictorially.

Figure : Addresses enclosed in a circular area constructed by a geo_distance query.

As the figure demonstrates, we have a central location (shown as the dropped pin on the map) and a circular area covering the addresses that we are looking for. The focus (or central location) is a point on the map that’s dictated by latitude and longitude coordinates.

The geo_shape query

There’s also another type of query, a geo_shapequery. This query fetches a list of geographical points (addresses) in a given geometrically constructed geo-envelope. The envelope could be a three-sided triangle or a multi-sided polygon (except that the envelope must not be an open ended one). The figure given below shows this concept pictorially.

Figure : Finding the addresses in a polygonal shape with a geo_shape query

The figure shows an hexagonal envelope constructed on a map with a given six pairs of coordinates (each pair is a geopoint with latitude and longitude). The geo_shapesearch finds the locations fitting inside this polygon.

Before we jump into experimenting and learning the geospatial queries in full swing, we will need to understand the mapping schema of geospatial data: the data types that support the geodata and the mechanics for indexing that data. We look at that in geo data types in the next article.

Me @ Medium || LinkedIn || Twitter || GitHub

These short articles are condensed excerpts taken from my book Elasticsearch in Action, Second Edition. The code is available in my GitHub repository.

Elasticsearch in Action

--

--

Madhusudhan Konda
Madhusudhan Konda

Written by Madhusudhan Konda

Madhusudhan Konda is a full-stack lead engineer, mentor, and conference speaker. He delivers live online training on Elasticsearch, Elastic Stack &Spring Cloud

No responses yet