I want to do a geosearch where it should first search for all locations within a distance of 50 meters, if more than 5 hits are found, then return those. If less than 5 hits are found I want to expand and search all locations within a distance of 400 meters. Again, if less than 5 hits are found I want to expand to 1000 meters but if less than 5 hits are found there I want to return those and not expand further. I don't want to return the 5 closest results, I want to return all the hits from up to the distance used.
I'm aggregating like this:
aggregations.GeoDistance("nearby_locations", g => g
.Field(f => f.GeoLocations)
.DistanceType(GeoDistanceType.Arc)
.Unit(DistanceUnit.Meters)
.Origin((double)position.X, (double)position.Y)
.Ranges(
r => r.To(50),
r => r.To(400),
r => r.To(1000)));
But I don't know how to return the hits for the first bucket that has over 5 hits. At the moment I'm checking which bucket that had more than 5 hits and then do another search on that distance.
var maxDistance = 1000;
response = Search(query, skip, size, position, maxDistance);
var distanceBucket = response.Aggs.GeoDistance("nearby_locations").Buckets
.FirstOrDefault(x => x.DocCount > 5);
if(distanceBucket != null) {
distanceUsed = (int)distanceBucket.To.Value;
response = Search(query, skip, size, position, distanceUsed);
}
This works, but I was wondering if there is a better way to achieve this?