Last active
November 12, 2020 04:32
-
-
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+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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