Setting Up Geolocation Search With Elasticsearch

Pramono Winata
5 min readJan 9, 2021
Photo by Timo Wielink on Unsplash

Location based feature is pretty common nowadays. Often we saw a lot of features based around our a location like store near corresponding location.

Location based feature might sounds complicated, but it can actually easily be implemented with Elasticsearch.

Elasticsearch is a NoSQL database with document based structure. Often, it’s being used as a Search Engine. It also provides its own syntax and many tools to help the search to be as flexible as possible.

In this article I will show you the simple implementation of making a geolocation search by getting list of cities by coordinate range.

Installing Elasticsearch

The installation guide is actually very clear in their website and you can also get the installation guide for it. At the time I am writing this article, I am using Elasticsearch with the version number of 7.4.2 .

Elasticsearch has been making a lot of changes in their versions, one of them being the removal of mapping type. So do not expect this to fully work if you are using another version of Elasticsearch.

After finishing your installation, do not forget to run your elasticsearch service, which is mentioned quite clearly on their installation guide (for linux, in short do this ./bin/elasticsearch ).

Make sure your elasticsearch is running by using a GET request into port 9200 in your local machine.GET http://localhost:9200

Making your index

Index is similar to table in usual database. For this example, let’s make an index named cities that will contain our data.

Let’s also define a simple modeling for our data: id : keyword for our identifiername : text for the city namecoordinate : geo_point to store our city coordinates (neat, they have this data-type already)

In Elasticsearch, we are creating index by making a curl into an API. In our case our request will be like this:

When you used that curl, you should get response like this to verify that your index has been created.

Nicely done, now your index is ready to be used. We will now proceed to play around our newly created index.

Populating Elasticsearch Data

We will now fill our Elasticsearch index with documents. If you are not familiar with that definition, just know that it is very similar to rows in a SQL database.

In Elasticsearch, it’s possible to store data that doesn’t match with our predefined schema. But we will not do that here, instead we will insert data that matched our predefined schema.

Since we will be inserting several data at once, we will be utilizing bulk API that has been provide by Elasticsearch to allow multiple insertion in one API call.

In this example below, I will be inserting 9 cities into my index, feel free to add more if you wish so.

POST 'http://localhost:9200/cities/_bulk

The payload might looks weird since it’s incorrect json format, but don’t worry it supposedly designed that way.It should then reply you back with response similar to this:

Querying Documents

Now comes the interesting part. We are going to do some querying with the documents that we had inserted before.

Elasticsearch supports many type of syntax for query searching, it also provided geolocation type searching and we will play around it today.We can simply start searching for our cities with curl like this:

POST 'http://localhost:9200/cities/_search

That query should give me San Fransisco which the coordinate is 37.7749 and -122.4194 which should be inside 10km distance radius from our coordinate (courtesy to google).

Congratulations! Now you have your own location search engine. But let’s play more around it. Let’s say you want to get more cities in that location.

Let’s try to expand the distance to 4500km by changing the payload:

And you should get this response:

It gives two results: New York and San Fransisco. The results surely are correct, but the position might be a bit weird. San Fransisco supposedly should come first since it’s nearer right?

Well not exactly, since what we are doing is just filtering, Elasticsearch doesn’t care about which one is nearest. And there might be case when we want to show the locations from the nearest one first on top.

Don’t worry, Elasticsearch has catered that too. We can utilize one type of query provided by Elasticsearch called function score query.

Function Score Query

Elasticsearch has it owns score calculation on what documents will be shown to user. By using function score query, we can modify that score so we can determine which documents should be returned.

What we will be using here is decay query function. There are three kinds of decay functions: exp, linear and gauss. Each of them have different behavior.

The one we will be using here is linear type function. We will also specify the coordinate together with offset and scale.

POST 'http://localhost:9200/cities/_search

Now, we should get our result ordered by the highest score.

And that wraps it up!

That should cover how location based is implemented with Elasticsearch. But that is not the end, what i have shown here is just the surface of it.

I hope you found this article interesting and useful. If you do interested, keep learning more on this and try to experiment around it with combination of the function scoring, it will be fun I promise.

Always be curious and you will learn something new.

--

--