I'm building an application that requires elasticsearch to find the nearest location based on latitude and longitude. At the moment I'm able to do this based on a GeoPoint and a maximum distance in kilometers.
@Override
public List<CityDefinition> findCitiesNearby(GeoPoint geo, double distance, String source) {
LOGGER.info("<findCitiesNearby>");
FilterBuilder filterBuilderGeo = FilterBuilders.geoDistanceFilter("geo").point(geo.getLat(), geo.getLon()).distance(distance, DistanceUnit.KILOMETERS);
SearchQuery q = new NativeSearchQueryBuilder()
.withFilter(filterBuilderGeo);
List<CityDefinition> fields = esOps.queryForList(q, CityDefinition.class);
return fields;
}
This works almost perfect but instead of searching within a given distance I want elastic to return the nearest location no matter the distance. Is this possible? If it is, how do I do this?