Geospatial queries in Elasticsearch

Published on March 18th 2020
Graffiti on a wall

How we use location data provide region specific search results.

The Candide App is used across multiple countries, each with their own seasons and unique plants. This means that the way we store that data needs to reflect this and be easily searchable on a regionalised basis.
For most of our search needs, we lean on Elasticsearch. Where we felt it was important we have broken the indexes into regionalised domains (plants, problems etc). But in some cases, we felt that splitting the indexes was not the right thing to do. As our user base grows the need to regionalise the results of the indexes that are not split by region become ever greater. Being able to provide search results that are appropriate for you location provides a far better user experience.

Setting out geo_point field mapping

For us to be able to make geospatial queries against out Elasticsearch index we need to correctly set up the field to be queried when creating our index. This is done by changing the type that is specified when setting the mappings for the index. The type in question is the geo_point type. According to the Elasticsearch documentation, when setting fields to a types we can:
  1. find geo-points within a bounding box, within a certain distance of a central point or a polygon
  2. aggregate documents geographically by distance
  3. integrate the distance into the relevance score
  4. sort documents by distance
Now that we know some of the benefits of using geospatial queries, let look at the request that will set the geo_point within our index' mappings. The snippet below will change the type of the location field to be geo_point.
1 PUT my_index
2 {
3 "mappings": {
4 "properties": {
5 "location": {
6 "type": "geo_point"
7 }
8 }
9 }
10 }

Adding some data to our index

Now that we have set up our index correctly, we need to add some data. Geo-point fields can accept data in 5 ways. Each of these can be seen below. The one you choose will depend on the needs of your application. Take note of the format of the lat, lon, in some instances, the order is switched.
1PUT my_index/_doc/1
3 "text": "Geo-point as an object",
4 "location": {
5 "lat": 41.12,
6 "lon": -71.34
7 }
10PUT my_index/_doc/2
12 "text": "Geo-point as a string",
13 "location": "41.12,-71.34"
16PUT my_index/_doc/3
18 "text": "Geo-point as a geohash",
19 "location": "drm3btev3e86"
22PUT my_index/_doc/4
24 "text": "Geo-point as an array",
25 "location": [ -71.34, 41.12 ]
28PUT my_index/_doc/5
30 "text": "Geo-point as a WKT POINT primitive",
31 "location" : "POINT (-71.34 41.12)"


Now that we have all this lovely geo data being indexed. How can we query it?
We are going to cover 2 of the options for querying geo-point data, geo_distance and geo_bounding_box.

Filtering by distance

Filtering by distance will only return documents that occur within a specific distance from a geo-point.
1GET /my_index/_search
3 "query": {
4 "bool" : {
5 "must" : {
6 "match_all" : {}
7 },
8 "filter" : {
9 "geo_distance" : {
10 "distance" : "200km",
11 "location" : {
12 "lat" : 40,
13 "lon" : -70
14 }
15 }
16 }
17 }
18 }
As with adding documents to our index, geo-points can be filtered in different representations of a geo-point. These can be seen here.

Filtering by a bounding box

Using a bounding box in our query allows us to filter our documents based on a bounding box. This could range from a bounding box containing a city to one containing a whole hemisphere. Either way the query looks like this:
1GET my_index/_search
3 "query": {
4 "bool" : {
5 "must" : {
6 "match_all" : {}
7 },
8 "filter" : {
9 "geo_bounding_box" : {
10 "location" : {
11 "top_left" : {
12 "lat" : 40.73,
13 "lon" : -74.1
14 },
15 "bottom_right" : {
16 "lat" : 40.01,
17 "lon" : -71.12
18 }
19 }
20 }
21 }
22 }
23 }
The vertices of the bounding box can also be set to top_right and bottom_left, as well as topLeft, topRight, bottomLeft and bottomRight. You can also set the top, bottom, left and right separately.

In conclusion

Need the ability to query shapes like squares and polygons? Check out geo-shapes and use this awesome tool to generate your polygons

Be the first to download the app

Download on the App StoreGet it on Google Play