Geolocation with Elasticsearch

A little example of implementing Geolocation in our company, helped by Elasticsearch.

Geolocation with Elasticsearch

At Jolimoi, we create a service called “Prestations Maquillage sur-mesure” It’s a service of make-up at home, made by the beauty stylist of Jolimoi. This service requires you to live near to a Beauty Stylist to be able to find an availability slot. To manage this, we use the Elasticsearch Geolocation feature. We have the address for our Beauty Stylists, but Elasticsearch works with latitude and longitude values, we don’t have their values.

The French government released an API call BAN for (Base Adresse Nationale) you can find info on the documentation page. With this API, we can convert addresses into coordinate points, and we stock this in our database.

How to configure a mapping to use Geoloc in ES?

Configure Geolocation in Elasticsearch is pretty easy. The only thing you have to do is to set a property with a type “geo_point” in a mapping.

Example of mapping

PUT geolocation

PUT geolocation/_mapping/_doc
{
  "properties": {
    "location": {
      "type": "geo_point"
    }
  }
}

With this property, we can do a bulk operation or others actions like that

The index (geolocation) and the mapping with the property location are set in Elasticsearch, we can index a document.

PUT geolocation/_doc/1
{
  "location": {
    "lon": 43.6483502,
    "lat": 3.37954
  }
}

Searching from a distance

Now we have our index ready, and we can query inside with the filter “geo_distance”. This filter takes parameters distance with distance units of ES and the localization of the element, in our case a customer localization.

GET geolocation/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "70km",
          "location": {
            "lon": 43.6100166,
      "lat": 3.8391422
          }
        }
      }
    }
  }
}

So when a customer tries to book a service, he has to put the postcode of his area. We ask to BAN service the geolocation and send to Elasticsearch the list of stylists around this position (as the crow flies). We display in the front the availability of all stylists.

Conclusion

To conclude, the usage of geolocation at Jolimoi. We use it to improve the travel for our Beauty Stylists and avoiding long travel time.

We also used to get in relation to customer and Beauty Stylist in the same area. Furthermore, we want to create a close relation between Beauty Stylist and their customers.

Example of what we can do with the Geolocation

You have an e-shop, and you can display on a map all the customer place and make an analysis of where are the most of your customer.