Skip to content

Instantly share code, notes, and snippets.

@JohannesRudolph
Created June 1, 2015 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesRudolph/64320e23b70e5a625b9e to your computer and use it in GitHub Desktop.
Save JohannesRudolph/64320e23b70e5a625b9e to your computer and use it in GitHub Desktop.
Weird RavenDb Spatial query behavior
[Fact]
public void WithinRadiusOf_NamedSpatialField()
{
Session.Advanced.DocumentStore.ExecuteIndex( new NamedSpatialFieldIndex() );
var obj = new ClassWithLocation()
{
Id = "obj/1",
Location = new Location( 10.0, 11.0 )
};
Session.Store( obj );
Session.SaveChanges();
var namedLuceneQuery = Session.Advanced
.LuceneQuery<ClassWithLocation, NamedSpatialFieldIndex>()
.WithinRadiusOf( "Location", 1.0, 10.0, 11.0 )
.WaitForNonStaleResults()
.ToList();
Assert.Single( namedLuceneQuery );
var linqQueryWithSpatial = Session.Query<ClassWithLocation, NamedSpatialFieldIndex>()
.Spatial( x => x.Location, x => x.WithinRadiusOf( 1.0, 10.0, 11.0 ) )
.Customize( x => x.WaitForNonStaleResults() )
.ToList();
Assert.Empty( linqQueryWithSpatial ); // for some reason this doesn't work
var linqQueryWithCustomize = Session.Query<ClassWithLocation, NamedSpatialFieldIndex>()
.Customize( x => x.WithinRadiusOf( "Location", 1.0, 10.0, 11.0 ) )
.Customize( x => x.WaitForNonStaleResults() )
.ToList();
Assert.Single( linqQueryWithCustomize ); // this works
}
class ClassWithLocation : IAggregateRoot
{
public string Id { get; set; }
public Location Location { get; set; }
}
class ClassWithLocationReduceResult
{
public string Id { get; set; }
public Location Location { get; set; }
}
class NamedSpatialFieldIndex : AbstractIndexCreationTask<ClassWithLocation, ClassWithLocationReduceResult>
{
public NamedSpatialFieldIndex()
{
Map = docs => from d in docs
select new ClassWithLocationReduceResult()
{
Id = d.Id,
Location = (Location)SpatialGenerate( "Location", d.Location.Latitude, d.Location.Longitutde )
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment