liveBook · Manning

Geo_distance query

As explained in Elasticsearch in Action, Second Edition

A geo_distance query retrieves documents whose geo-point or geo-shape values lie within a specified distance of a geographic point.

A geo_distance query finds documents whose locations fall inside a circle centered on a specified latitude and longitude. It suits nearby-venue searches, delivery areas, directions, and schools within a radius. section 12.0 section 12.1.2

Find restaurants within 175 km

GET restaurants/_search { "query": { "geo_distance": { "distance": "175 km", "location": { "lat": 50.00, "lon": 0.10 } } } } Here, distance sets the circle's radius, while location supplies its central point. The query returns restaurants within that radius. section 12.5

Use the query with these practical considerations:

  • Specify distances in kilometers or miles, such as "175 km", "350 mi", or the same values without spaces. section 12.5
  • Represent the central point with latitude and longitude; strings, arrays, WKT, and other supported formats are available. section 12.5
  • Use geo_bounding_box for rectangular areas and geo_shape when locations or search regions require shapes rather than circles. section 12.3 section 12.4
Figure 12.2 Addresses enclosed in a circular area constructed by a geo_distance query

Visualize the query as a dropped pin surrounded by a circular search area containing matching addresses. section 12.1.2

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines the circular search model and demonstrates distance, location, units, and coordinate formats, while deferring geoshape experimentation.
Examples
Uses restaurant searches within 175 kilometers and nearby-school searches within a 10-kilometer radius.
What it assumes
Assumes locations are indexed as supported geospatial values, while introducing latitude, longitude, radius, and coordinate representations.
What you can do after
Apply it via Elasticsearch's search API to retrieve locations within a chosen radius of a geographic point.
How it connects
Connects geo_point and geo_shape data types with geo_bounding_box and geo_shape queries for alternative geographic regions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.