Elasticsearch - How to use geolocation

In this example, we are going to understand how to use ElasticSearch geolocation. We will add documents with geolocation using geo_point and then search for those documents using coordinates.

ElasticSearch geolocation is a very useful tool that can help you, for example, to search for businesses, clients, etc., that are close to where a user is at a specific point.

What are the steps to create an index with geolocation

  • You create an index with a POST and property: properties:location
  • Create a document with a POST by completing the location
  • You perform the search with a GET with a distance filter and location

Create the geolocation index

The first thing you need to do is create an index in ElasticSearch with geolocation. The index will be called establishment and you are going to define it with the geo_point property so that it has geolocation support.

Define PUT to create an index with geolocation and run it. Notice the type geo_point that is defined inside properties:location

curl --location --request PUT 'http://localhost:9200/establishment' \
--header 'Content-Type: application/json' \
--data-raw '{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_point"
            }
        }
    }
}

Elastic search Geo point

Hot to Add documents to elastic with geolocation

In the establishment index, we create a document with ID 1 and indicate its location. Doing a PUT to the index you add a document with the geolocation coordinates. Notice here that inside location indicate the latitude and longitude coordinates of this index.

curl --location --request PUT 'http://localhost:9200/establishment/_doc/1' \
--header 'Content-Type: application/json' \
--data-raw '{
    "text": "Sports Shop ABC",
    "location": {
        "lat": -31.397561,
        "lon": -64.235415
    }
}'

Here I only show you one insert for simplicity, but it is convenient to insert several documents in different coordinates (near and far from a point) to better visualize this example.

Elastic search Geo point

Search documents in elasticsearch with geolocation coordinates

Then you can search by proximity using kilometers from a point of origin.

Look again at the filters we use for the search by geo_distance. Using the distance to where we want to search in distance and the point from which we want to search in location.

curl --location --request GET 'http://localhost:9200/establishment/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "10km",
                    "location" : {
                        "lat" : -31.397948,
                        "lon" : -64.254007
                    }
                }
            }
        }
    }
}'

Elastic search Geo point

Conclusion

In this short example we saw how to create a document containing geolocation coordinates. We use the geo_point property to create a document and then search for it by proximity. There are other types of searches that you can also examine, such as the geo_shape that other forms of geolocation search can provide.

References:

Indexes with geo_point: Datatype geo-point

Search by geo-distance using the query geo_point: Search geo-point

Hi! If you find my posts helpful, please support me by inviting me for a coffee :)