private static void BenchmarkSearch(ISpatialIndex index, int rounds) { var query = new KnnQuery { Coordinate = new GeoCoordinate(52.3667, 4.900), MaxDistance = 10000, MaxResults = 15 }; Console.Write("Searching top {0} within {1} m from [{2}] .. ", query.MaxResults, query.MaxDistance, query.Coordinate); var total = 0L; var timer = new Stopwatch(); for (int i = rounds; i > 0; i--) { timer.Restart(); index.KnnSearch(query); timer.Stop(); total += timer.ElapsedMilliseconds; } Console.WriteLine("ok ({0} ms)", total / rounds); }
public LocationSearchResults Search(LocationSearchRequest search) { // TODO: validate search parameter var query = new KnnQuery { Coordinate = search.Coordinate, MaxDistance = search.MaxDistance, MaxResults = search.MaxResults }; // use spatial index to reduce data set var knnQueryResult = _spatialIndex.KnnSearch(query); if (!knnQueryResult.Results.Any()) { return(LocationSearchResults.ZeroResults()); } // now load entities from database var ids = knnQueryResult.Results.Select(i => i.Document.Id).ToArray(); var locations = _locationRepository.FindIn(ids); // build return results by joining Location and Distance var results = new List <LocationSearchResultItem>(); var locationsIndex = locations.ToDictionary(l => l.Id); foreach (var item in knnQueryResult.Results) { if (locationsIndex.ContainsKey(item.Document.Id)) { var location = locationsIndex[item.Document.Id]; results.Add(new LocationSearchResultItem(location, item.Distance)); } } return(LocationSearchResults.Success(results)); }