🏡 Home 📖 Chapter Home 👉 Next

⚡  ElasticsearchBook.com is crafted by Jozef Sorocin and powered by:

Use Case

I want to retrieve locations

Randomly generated points of interest in the NYC area. Base map courtesy of http://geojson.io/

Randomly generated points of interest in the NYC area. Base map courtesy of http://geojson.io/

Approach

Data

Bounding Box Recap

In order to describe an area of a map (that uses the Mercator projection) with minimum points, we'll need a rectangle's opposing corners — typically the south west and the north east:

<aside> 💡 Many frontend mapping libraries implement a getBounds() method which returns the viewport's NE & SW corners — most notably Mapbox, Google Maps, and Leaflet.

</aside>

As soon as we've obtained the two points, we can plug them into the [geo_bounding_box](<https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html>) query:

POST locations/_search
{
  "query": {
    "geo_bounding_box": {
      "geometry.coordinates": {
        "bottom_left": [-74.32457, 40.51119],
        "top_right": [-73.72444, 40.93012]
      }
    }
  }
}

Alternatively, the opposing points' coordinates can be deconstructed into a bounding box: