Elasticsearch in Action: Overview of Specialised Queries

Madhusudhan Konda
5 min readNov 14, 2022
Excerpts taken from my upcoming book: Elasticsearch in Action

I will be presenting these short and condensed articles a mini-series on each of the topics in the next few months. 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.

In this article, we look at a set of advanced and special queries. We’ll introduce a bit of theory to summarise all these types before jumping into running and experimenting with them in the following articles.

Location based searches

The common and popular use cases involving geoqueries include searching nearby restaurants for a delivery order, finding the directions to a friend’s house, popular schools within a 10 km range, and so on. Elasticsearch has first-class support for satisfying such location-related searches. It also provides a handful of geospatial queries in the name of geo_bounding_box, geo_distance, and geo_shape queries.

Elasticsearch also provides capability of searching two-dimensional (2D) shapes using shape queries. Design engineers, game developers, and others can search 2D shapes in an index consisting of 2D shapes.

There are another set of queries — low-level positional queries called span queries. Although leaf queries, such as full-text and term-level, help us search data, they aren’t helpful for searching at a level where we want to find the words in a particular order, their position, the exact (or approximate) distance between the words, and so on. This is where span queries come into play.

There are specialized queries such as distance_feature, percolator, more_like_this, and pinned.

The distance_feature query boosts the results if our search result is nearer to a particular location; for example, searching for schools within a 10 km radius, but we want those schools with nearby parks to be given a higher priority.

To append the organically found search results in a list of sponsored results, we use pinned queries.

The more_like_this queries help find similarly looking documents.

The percolator queries help in alerting and notifying the users for their unyielded results from the past.

Let’s understand the need for geospatial queries and the data types that support these queries. We will then look at the queries provided by Elasticsearch out of the box for such search criteria.

Introducing location search

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 the 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 focussed search.

The out-of-the-box data types that support geospatial data are geo_point and geo_shape.

Elasticsearch also 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.

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_box query 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 below indicates the addresses enclosed in one such georectangle.

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

As marked by the rectangle, we are searching for addresses in central London in an area highlighted in the georectangle. Addresses intersecting this rectangle are returned as positive results.

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_distance query does!

Elasticsearch provides the geo_distance query 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. Figure shown below demonstrates the geodistance concept pictorially.

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

In the above figure, 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_shape query. 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 cannot be an open ended one). Figure shown below demonstrates this concept pictorially.

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

The above hexagonal envelope is constructed on a map with a given six pairs of coordinates (each pair is a geopoint with latitude and longitude). The geo_shape search 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. In the next article, we will go through the geo_point types first, then on to the geo_shapes.

Stay tuned.

Don’t forget to follow me if you like these articles! And clap clap clap :)

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