5

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?

  • You can do multiple Geodistance aggregations in one search request – Russ Cam Nov 07 '16 at 21:54
  • @RussCam I think I'm doing that, just don't know how to return the results for only one of them. I've updated the question, hopefully that made it clearer. – Tom Ibollen Nov 09 '16 at 17:29
  • Have you tried nesting top_hits aggregation within the geo distance rings aggregation? https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html – Archit Saxena Nov 17 '16 at 13:48

0 Answers0