Return hits from one bucket when performing a geopoint search in elasticsearch.net

I want to do a geo-search, where he must first search all locations at a distance of 50 meters if more than 5 hits are found, and then return them. If less than 5 hits are found, I want to expand and find all locations at a distance of 400 meters. Again, if less than 5 strokes are found, I want to expand to 1000 meters, but if less than 5 strokes are found, I want to return them and not expand further. I do not want to return the 5 closest results, I want to return all hits from a distance to the distance used.

I aggregate 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 do not know how to return hits for the first bucket having more than 5 hits. I am currently checking which bucket has more than 5 hits, and then performs another search at 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?

+5
source share

Source: https://habr.com/ru/post/1259408/


All Articles