Skip to content

Instantly share code, notes, and snippets.

@phinett
Created September 1, 2014 14:27
Show Gist options
  • Save phinett/0827a838f0dc550fdc7d to your computer and use it in GitHub Desktop.
Save phinett/0827a838f0dc550fdc7d to your computer and use it in GitHub Desktop.
public class SpatialResultsTest : RavenTestBase
{
private class Lead
{
public string Id { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
}
private class LeadsIndex : AbstractIndexCreationTask<Lead, LeadsIndex.Result>
{
public class Result
{
public string Id { get; set; }
public double Lat { get; set; }
public double Lng { get; set; }
}
public LeadsIndex()
{
Map = leads => from lead in leads
select new
{
Id = lead.Id,
__ = SpatialGenerate("Coordinates", lead.Lat, lead.Lng)
};
StoreAllFields(FieldStorage.Yes);
}
}
[Fact]
public void CanGetSpatialDistanceFromMetadataWithProjection()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(new Lead { Id = "leads/1", Lat = 52.5223618, Lng = -1.8355805 });
session.Store(new Lead { Id = "leads/2", Lat = 52.5315364, Lng = -1.7382681 });
session.Store(new Lead { Id = "leads/3", Lat = 52.5158768, Lng = -1.7306246 });
session.SaveChanges();
}
new LeadsIndex().Execute(store);
using (var session = store.OpenSession())
{
var results = session.Query<LeadsIndex.Result, LeadsIndex>()
.Customize(x => x.WaitForNonStaleResults())
.Customize(x => x.WithinRadiusOf(
fieldName: "Coordinates",
radius: 20,
latitude: 52.5223618,
longitude: -1.8355805))
.As<Lead>()
.ToList();
var distance1 = session.Advanced.GetMetadataFor(results[0])["Temp-Index-Score"];
var distance2 = session.Advanced.GetMetadataFor(results[1])["Temp-Index-Score"];
var distance3 = session.Advanced.GetMetadataFor(results[2])["Temp-Index-Score"];
Assert.NotEqual(1, distance1);
Assert.NotEqual(1, distance2);
Assert.NotEqual(1, distance3);
}
}
}
[Fact]
public void CanGetSpatialDistanceFromMetadataWithoutProjection()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(new Lead { Id = "leads/1", Lat = 52.5223618, Lng = -1.8355805 });
session.Store(new Lead { Id = "leads/2", Lat = 52.5315364, Lng = -1.7382681 });
session.Store(new Lead { Id = "leads/3", Lat = 52.5158768, Lng = -1.7306246 });
session.SaveChanges();
}
new LeadsIndex().Execute(store);
using (var session = store.OpenSession())
{
var results = session.Query<Lead, LeadsIndex>()
.Customize(x => x.WaitForNonStaleResults())
.Customize(x => x.WithinRadiusOf(
fieldName: "Coordinates",
radius: 20,
latitude: 52.5223618,
longitude: -1.8355805))
.ToList();
var distance1 = session.Advanced.GetMetadataFor(results[0])["Temp-Index-Score"];
var distance2 = session.Advanced.GetMetadataFor(results[1])["Temp-Index-Score"];
var distance3 = session.Advanced.GetMetadataFor(results[2])["Temp-Index-Score"];
Assert.NotEqual(1, distance1);
Assert.NotEqual(1, distance2);
Assert.NotEqual(1, distance3);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment