示例#1
0
        public void CanPerformSpatialSearch()
        {
            var indexDefinition = new IndexDefinition
            {
                Map     = "from e in docs.Events select new { Tag = \"Event\", _ = SpatialIndex.Generate(e.Latitude, e.Longitude) }",
                Indexes =
                {
                    { "Tag", FieldIndexing.NotAnalyzed }
                }
            };

            db.PutIndex("eventsByLatLng", indexDefinition);

            var events = SpatialIndexTestHelper.GetEvents();

            for (int i = 0; i < events.Length; i++)
            {
                db.Put("Events/" + (i + 1), null,
                       RavenJObject.FromObject(events[i]),
                       RavenJObject.Parse("{'Raven-Entity-Name': 'Events'}"), null);
            }

            const double lat = 38.96939, lng = -77.386398;
            const double radius = 6.0;
            QueryResult  queryResult;

            do
            {
                queryResult = db.Query("eventsByLatLng", new SpatialIndexQuery()
                {
                    Query        = "Tag:[[Event]]",
                    Latitude     = lat,
                    Longitude    = lng,
                    Radius       = radius,
                    SortedFields = new[] { new SortedField("__distance"), }
                });
                if (queryResult.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (queryResult.IsStale);

            Assert.Equal(7, queryResult.Results.Count);

            double previous = 0;

            foreach (var r in queryResult.Results)
            {
                Event e = r.JsonDeserialization <Event>();

                double distance = Raven.Database.Indexing.SpatialIndex.GetDistanceMi(lat, lng, e.Latitude, e.Longitude);

                Console.WriteLine("Venue: " + e.Venue + ", Distance " + distance);

                Assert.True(distance < radius);
                Assert.True(distance >= previous);
                previous = distance;
            }
        }
示例#2
0
        public void CanPerformSpatialSearch()
        {
            var indexDefinition = new IndexDefinition
            {
                Map     = "from e in docs.Events select new { Tag = \"Event\", _ = SpatialGenerate(e.Latitude, e.Longitude) }",
                Indexes =
                {
                    { "Tag", FieldIndexing.NotAnalyzed }
                }
            };

            store.DatabaseCommands.PutIndex("eventsByLatLng", indexDefinition);

            var events = SpatialIndexTestHelper.GetEvents();

            for (int i = 0; i < events.Length; i++)
            {
                store.DatabaseCommands.Put("Events/" + (i + 1), null,
                                           RavenJObject.FromObject(events[i]),
                                           RavenJObject.Parse("{'Raven-Entity-Name': 'Events'}"));
            }

            const double lat = 38.96939, lng = -77.386398;
            const double radiusInKm = 6.0 * 1.609344;
            QueryResult  queryResult;

            do
            {
                queryResult = store.DatabaseCommands.Query("eventsByLatLng", new SpatialIndexQuery()
                {
                    Query            = "Tag:[[Event]]",
                    QueryShape       = SpatialIndexQuery.GetQueryShapeFromLatLon(lat, lng, radiusInKm),
                    SpatialRelation  = SpatialRelation.Within,
                    SpatialFieldName = Constants.DefaultSpatialFieldName,
                    SortedFields     = new[] { new SortedField("__distance"), }
                }, null);
                if (queryResult.IsStale)
                {
                    Thread.Sleep(100);
                }
            } while (queryResult.IsStale);

            var expected = events.Count(e => GetGeographicalDistance(lat, lng, e.Latitude, e.Longitude) <= radiusInKm);

            Assert.Equal(expected, queryResult.Results.Count);
            Assert.Equal(7, queryResult.Results.Count);

            double previous = 0;

            foreach (var r in queryResult.Results)
            {
                Event e = r.JsonDeserialization <Event>();

                double distance = GetGeographicalDistance(lat, lng, e.Latitude, e.Longitude);
                Console.WriteLine("Venue: " + e.Venue + ", Distance " + distance);

                Assert.True(distance < radiusInKm);
                Assert.True(distance >= previous);
                previous = distance;
            }
        }