AI Hints
Home›Elasticsearch Tutorial›Deep Paging, collapse and Geo
Elasticsearch · Lesson 87 of 120

Geo Search

Stores near me.

⏱ 2 min read▶ Video at 1:21:46🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Trailhead has eight stores. A shopper in Fort Collins wants the nearest ones. With a geo point field, Elasticsearch can filter and sort by distance.

  • Eight stores
  • Near me?
  • geo_point

Stores within 150 km

Location is a geo point. The geo distance filter keeps stores within one hundred and fifty kilometres of Fort Collins. We sort by distance, in kilometres. Run it. Boulder and Denver, with their distances.

Dev Tools Consoletext
GET stores/_search?filter_path=hits.hits._source.city,hits.hits.sort
{
  "query": { "bool": { "filter": { "geo_distance": {
    "distance": "150km", "location": { "lat": 40.585, "lon": -105.084 } } } } },
  "sort": [ { "_geo_distance": {
    "location": { "lat": 40.585, "lon": -105.084 }, "unit": "km" } } ]
}
Output
{
  "hits": {
    "hits": [
      {"_source": {"city": "Boulder"}, "sort": [65.3247]},
      {"_source": {"city": "Denver"}, "sort": [94.3852]}
    ]
  }
}
Also uses _load
stores

Geo features

Geo point stores a latitude and longitude. Geo distance filters by radius. Geo bounding box filters by a map rectangle. Geo shape stores areas, like delivery zones. And geo aggregations power heat maps.

keyvalue
geo_pointlat / lon
geo_distancewithin a radius
geo_bounding_boxmap rectangle
geo_shapeareas, zones

Summary

Geo point fields let you filter and sort by distance, like finding the nearest stores. Next, Elasticsearch from Python.

Key takeaways
  • geo_point
  • geo_distance
  • Sort by km
Next up: The Python Client — search from code