[Test]
public void FindNearbyDocs()
{
// These items is in a radius of 4 miles (approx 6,5 km)
var areaOneDocOne = new DummyGeoDoc(55.6880508001, 13.
5717346673);
var areaOneDocTwo = new DummyGeoDoc(55.6821978456, 13.
6076183965);
var areaOneDocThree = new DummyGeoDoc(55.673251569, 13.5946697607);
// This item is 12 miles (approx 19 km) from the closests in areaOne
var closeButOutsideAreaOne = new DummyGeoDoc(55.
8634157297, 13.5497731987);
// This item is about 3900 miles from areaOne
var newYork = new DummyGeoDoc(40.
7137578228, -74.0126901936);
var session = Store.OpenSession();
session.Store(areaOneDocOne);
session.Store(areaOneDocTwo);
session.Store(areaOneDocThree);
session.Store(closeButOutsideAreaOne);
session.Store(newYork);
session.SaveChanges();
var indexDefinition = new IndexDefinition
{
Map = "from doc in docs select new { _ = SpatialIndex.Generate(doc.Latitude, doc.Longitude) }"
};
Store.DatabaseCommands.PutIndex("FindByLatLng", indexDefinition);
// Wait until the index is built
session.Advanced.LuceneQuery<DummyGeoDoc>("FindByLatLng")
.WaitForNonStaleResults()
.ToArray();
const double lat = 55.
6836422426, lng = 13.5871808352; // in the middle of AreaOne
const double radius = 5.0;
// Radius >= 14.4 return 4 results
// Radius >= 14.3 return 0 results
// Expcted is that 5.0 will return 3 results
var nearbyDocs = session.Advanced.LuceneQuery<DummyGeoDoc>("FindByLatLng")
.WithinRadiusOf(radius, lat, lng)
.WaitForNonStaleResults()
.ToArray();
Assert.That(nearbyDocs != null);
Assert.That(nearbyDocs.Length == 3);
session.Dispose();
}
public class DummyGeoDoc
{
public string Id { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public DummyGeoDoc(double lat, double lng)
{
this.Latitude = lat;
this.Longitude = lng;
}
}