public void TestAntiM() { _searcher = new IndexSearcher(_directory, true); const double miles = 5.0; Console.WriteLine("testAntiM"); // create a distance query var dq = new DistanceQueryBuilder(_lat, _lng, miles, LatField, LngField, CartesianTierPlotter.DefaltFieldPrefix, true); Console.WriteLine(dq); //create a term query to search against all documents Query tq = new TermQuery(new Term("metafile", "doc")); var dsort = new DistanceFieldComparatorSource(dq.DistanceFilter); Sort sort = new Sort(new SortField("foo", dsort, false)); // Perform the search, using the term query, the distance filter, and the // distance sort TopDocs hits = _searcher.Search(tq, dq.Filter, 1000, sort); int results = hits.TotalHits; ScoreDoc[] scoreDocs = hits.ScoreDocs; // Get a list of distances Dictionary<int, Double> distances = dq.DistanceFilter.Distances; Console.WriteLine("Distance Filter filtered: " + distances.Count); Console.WriteLine("Results: " + results); Console.WriteLine("============================="); Console.WriteLine("Distances should be 3 " + distances.Count); Console.WriteLine("Results should be 3 " + results); Assert.AreEqual(3, distances.Count); // fixed a store of only needed distances Assert.AreEqual(3, results); double lastDistance = 0; for (int i = 0; i < results; i++) { Document d = _searcher.Doc(scoreDocs[i].Doc); String name = d.Get("name"); double rsLat = NumericUtils.PrefixCodedToDouble(d.Get(LatField)); double rsLng = NumericUtils.PrefixCodedToDouble(d.Get(LngField)); Double geo_distance = distances[scoreDocs[i].Doc]; double distance = DistanceUtils.GetInstance().GetDistanceMi(_lat, _lng, rsLat, rsLng); double llm = DistanceUtils.GetInstance().GetLLMDistance(_lat, _lng, rsLat, rsLng); Console.WriteLine("Name: " + name + ", Distance " + distance); Assert.IsTrue(Math.Abs((distance - llm)) < 1); Assert.IsTrue((distance < miles)); Assert.IsTrue(geo_distance >= lastDistance); lastDistance = geo_distance; } }
public ActionResult Index(string q, string city, string lat, string @long, string miles, string count) { IList<SelectListItem> listOfMiles = new List<SelectListItem> { new SelectListItem {Text = "50", Value = "50"}, new SelectListItem {Text = "100", Value = "100"} }; IList<SelectListItem> listOfCounts = new List<SelectListItem> { new SelectListItem {Text = "10", Value = "10"}, new SelectListItem {Text = "50", Value = "50"}, new SelectListItem {Text = "100", Value = "100"} }; ViewData["Miles"] = listOfMiles; ViewData["Counts"] = listOfCounts; var indexViewModel = new IndexViewModel { q = q, lat = lat, @long = @long }; var analyzer = new StandardAnalyzer(_luceneVersion); Query query; if (string.IsNullOrEmpty(q)) { query = new MatchAllDocsQuery(); } else { var multiFieldQueryParser = new MultiFieldQueryParser(_luceneVersion, _searchFields, analyzer); query = multiFieldQueryParser.Parse(q); } // create a distance query // https://svn.apache.org/repos/asf/incubator/lucene.net/trunk/test/contrib/Spatial/TestCartesian.cs double milesDouble = 100.0; int countInt = 50; double latitudeDouble; double longitudeDouble; if (!String.IsNullOrEmpty(miles)) { Double.TryParse(miles, out milesDouble); } if (!String.IsNullOrEmpty(count)) { int.TryParse(count, out countInt); } Double.TryParse(lat, out latitudeDouble); Double.TryParse(@long, out longitudeDouble); var distanceQuery = new DistanceQueryBuilder(latitudeDouble, longitudeDouble, milesDouble, LuceneIndexFieldMap.LatitudeField, LuceneIndexFieldMap.LongitudeField, CartesianTierPlotter.DefaltFieldPrefix, true); var dsort = new DistanceFieldComparatorSource(distanceQuery.DistanceFilter); var sort = new Sort(new SortField("foo", dsort, false)); var startTime = DateTime.Now; var distanceHelper = new DistanceHelper(); // search for documents { TopDocs topDocs = _indexSearcher.Search(query, distanceQuery.Filter, countInt, sort); indexViewModel.TotalHits = topDocs.TotalHits; foreach (ScoreDoc match in topDocs.ScoreDocs) { Document doc = _indexSearcher.Doc(match.doc); double prefixCodedToDouble = NumericUtils.PrefixCodedToDouble(doc.Get(LuceneIndexFieldMap.LatitudeField)); double codedToDouble = NumericUtils.PrefixCodedToDouble(doc.Get(LuceneIndexFieldMap.LongitudeField)); double distanceInKilometres = distanceHelper.DistanceInKilometres(latitudeDouble, longitudeDouble, prefixCodedToDouble, codedToDouble); var geo = new GeoName { Name = doc.Get(LuceneIndexFieldMap.FirstNameField), Asciiname = doc.Get(LuceneIndexFieldMap.CityField), FeatureClass = doc.Get(LuceneIndexFieldMap.FeatureClassField), FeatureCode = doc.Get(LuceneIndexFieldMap.FeatureCodeField), Latitude = prefixCodedToDouble, Longitude = codedToDouble, Timezone = doc.Get(LuceneIndexFieldMap.TimeZoneField), DistanceInKm = distanceInKilometres }; indexViewModel.SearchResults.Add(geo); } } indexViewModel.SearchTimeMs = DateTime.Now.Subtract(startTime).TotalMilliseconds; return View(indexViewModel); }