No results from WithinRadius spatial query

28 views
Skip to first unread message

Mircea Chirea

unread,
May 18, 2016, 11:23:19 AM5/18/16
to RavenDB - 2nd generation document database
I'm not getting any results when I try to do a spatial query to find documents within a certain distance of an arbitrary position. Below is a failing test, the points are 270 meters apart:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Raven.Client.Indexes;
using Raven.Tests.Helpers;

namespace ConsoleApplication
{
    public class Car
    {
        public string Id
        {
            get;
            set;
        }

        public double? Latitude
        {
            get;
            set;
        }

        public double? Longitude
        {
            get;
            set;
        }
    }

    public class Cars_Spatial : AbstractIndexCreationTask<Car, Cars_Spatial.Query>
    {
        public class Query
        {
            public object Position
            {
                get;
                set;
            }
        }

        public Cars_Spatial()
        {
            Map = cars => from car in cars
                          select new
                          {
                              Position = SpatialGenerate(nameof(Query.Position), car.Latitude, car.Longitude),
                          };

            Spatial(q => q.Position, options => options.Geography.Default());
        }
    }

    public class SpatialTest : RavenTestBase
    {
        public void Run()
        {
            using (var store   = NewDocumentStore(requestedStorage: "Esent"))
            using (var session = store.OpenSession())
            {
                session.Store(new Car
                {
                    Latitude  = 44.3190634,
                    Longitude = 23.8071472,
                });

                session.SaveChanges();

                store.ExecuteIndex(new Cars_Spatial());
                WaitForIndexing(store);

                var inRange = session.Query<Cars_Spatial.Query, Cars_Spatial>()
                                     .Spatial(q => q.Position, criteria => criteria.WithinRadius(1, latitude: 44.3185266, longitude: 23.81044))
                                     .OfType<Car>()
                                     .ToList();

                Debugger.Break();
            }
        }
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            var test = new SpatialTest();

            test.Run();
        }
    }
}


Michael Yarichuk

unread,
May 18, 2016, 12:34:51 PM5/18/16
to RavenDB - 2nd generation document database
The query in your code does not return results at my machine as well. I will take a look at this

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,

 

Hibernating Rhinos Ltd  cid:image001.png@01CF95E2.8ED1B7D0

Michael Yarichuk l RavenDB Core Team 

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 

RavenDB paving the way to "Data Made Simple"   http://ravendb.net/  

Michael Yarichuk

unread,
May 19, 2016, 9:51:11 AM5/19/16
to RavenDB - 2nd generation document database
I find that there is a little bit of syntax issue here;

If you change index definition for this
public class Cars_Spatial : AbstractIndexCreationTask<Car>
{
public Cars_Spatial()
{
Map = cars => from car in cars
 select new
 {
 _ = SpatialGenerate("Position", car.Latitude, car.Longitude),
 };

Spatial("Position", options => options.Geography.Default());
}
}

and query to this

var inRange = session.Query<Car, Cars_Spatial>()
.Customize(x => x.WithinRadiusOf("Position",1, 44.3185266, 23.81044))
.ToList();

the test should work.

Michael Yarichuk

unread,
May 19, 2016, 9:57:40 AM5/19/16
to RavenDB - 2nd generation document database
In particular, if you specify the index with the following definition, it will be always empty
Map = cars => from car in cars
select new
{
  Position = SpatialGenerate("Position", car.Latitude, car.Longitude),
};

Mircea Chirea

unread,
May 19, 2016, 12:57:00 PM5/19/16
to RavenDB - 2nd generation document database
I think I've got it. I can't specify the spatial field name to be the same as the dummy field in the map result. I can do this and it works for example:

Map = cars => from car in cars
                select new
                {
                    SomeRandomFieldName = SpatialGenerate(nameof(Query.Position), car.Latitude, car.Longitude),
                };

Thanks for looking into this for me!
This should be documented though.
Reply all
Reply to author
Forward
0 new messages