From 23835940481b4316becc52d5cc22ecbcfd7836e5 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Mon, 29 Oct 2018 15:31:29 -0700 Subject: [PATCH] Use haversine instead of distance in knn if distance is not required. --- internal/server/search.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/server/search.go b/internal/server/search.go index bfc90ae9..e2759f33 100644 --- a/internal/server/search.go +++ b/internal/server/search.go @@ -423,9 +423,18 @@ func (server *Server) nearestNeighbors( if !match { return true } - dist := o.Distance(target) - if target.Meters() > 0 && dist > target.Meters() { - return false + var dist float64 + if s.distance { + dist = o.Distance(target) + if target.Meters() > 0 && dist > target.Meters() { + return false + } + } else { + // don't need actual distances, use haversine as proxy for sorting + dist = target.HaversineTo(o.Center()) + if target.Haversine() > 0 && dist > target.Haversine() { + return false + } } items = append(items, iterItem{id: id, o: o, fields: fields, dist: dist}) if !keepGoing {