Skip to content

Instantly share code, notes, and snippets.

@gordysc
Last active November 12, 2020 04:32
Show Gist options
  • Save gordysc/220c790c1c77c4283e4c36c6a9469c2c to your computer and use it in GitHub Desktop.
Save gordysc/220c790c1c77c4283e4c36c6a9469c2c to your computer and use it in GitHub Desktop.
Find first 10 points within a given radius of a lon/lat ordered by closest to furthest (Elasticsearch v7+)
def within_circle(lon:, lat:, distance:, unit:, direction: "asc")
{
query: {
bool: {
must: {
match_all: {}
},
filter: [
{
geo_distance: {
distance: [distance, unit].join(""),
coordinates: {
lon: lon,
lat: lat
},
}
}
]
}
},
sort: [
{
_geo_distance: {
coordinates: {
lon: lon,
lat: lat
},
order: direction,
unit: unit
}
}
]
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment