A geo distance query is a query type in Elasticsearch that is used to find documents based on their distance from a specified geographic point. This query is used to search for documents within a certain radius or distance from a specific location.
When a geo distance query is executed, Elasticsearch searches for documents that fall within a specified distance from a given geographic point. The query calculates the distance between the specified point and each document’s location field, which is typically stored as a geo_point data type.
Here’s an example of a geo distance query in Elasticsearch:
GET /my_index/_search { "query": { "bool": { "must": { "match_all": {} }, "filter": { "geo_distance": { "distance": "10km", "location": { "lat": 37.773972, "lon": -122.431297 } } } } } }
In this example, we are searching the `my_index` index for documents that are within 10 kilometers of the geographic point specified by the `location` field, which is a geo_point data type. The `geo_distance` query specifies the distance parameter as `10km` and the location parameter as a latitude and longitude pair.
The `geo_distance` query also supports a variety of other features, such as specifying different distance units, using different distance calculation algorithms, and filtering documents by a minimum and maximum distance.
The `geo_distance` query provides a powerful and flexible way to search for documents based on their distance from a specific geographic point in Elasticsearch. However, it’s important to note that geo distance queries can be computationally expensive and may not be suitable for large datasets or high-traffic applications. Additionally, the accuracy of geo distance queries can be impacted by the quality and completeness of the location data. Therefore, it’s important to carefully consider the use case and performance implications before using geo distance queries in Elasticsearch.