Esempio n. 1
0
        public void SearchGeoNearWithCustomDistanceField()
        {
            var connection = TestConfiguration.GetConnection();
            var dbSet      = new MongoDbSet <SearchGeoModel>();

            dbSet.SetConnection(connection);

            dbSet.AddRange(new SearchGeoModel[]
            {
                new SearchGeoModel {
                    Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(-74.005974, 40.712776)
                        )
                },
                new SearchGeoModel {
                    Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(138.600739, -34.928497)
                        )
                }
            });
            dbSet.SaveChanges();

            var results = dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                                                  new GeoJson2DGeographicCoordinates(138, -30)
                                                  ), distanceResultField: e => e.CustomDistanceField).ToArray();

            Assert.AreNotEqual(0, results[0].CustomDistanceField);
            Assert.AreNotEqual(0, results[1].CustomDistanceField);
            Assert.IsTrue(results[0].CustomDistanceField < results[1].CustomDistanceField);

            Assert.IsNull(results[0].ExtraElements);
        }
Esempio n. 2
0
        public void SearchGeoNearWithMinMaxDistances()
        {
            var connection = TestConfiguration.GetConnection();
            var dbSet      = new MongoDbSet <SearchGeoModel>();

            dbSet.SetConnection(connection);

            dbSet.AddRange(new SearchGeoModel[]
            {
                new SearchGeoModel {
                    Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(-74.005974, 40.712776)
                        )
                },
                new SearchGeoModel {
                    Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(138.600739, -34.928497)
                        )
                },
                new SearchGeoModel {
                    Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(115.860458, -31.950527)
                        )
                },
                new SearchGeoModel {
                    Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(147.327194, -42.882137)
                        )
                }
            });
            dbSet.SaveChanges();

            SearchGeoModel[] GetResults(double?maxDistance = null, double?minDistance = null)
            {
                return(dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                                               new GeoJson2DGeographicCoordinates(138, -30)
                                               ), distanceResultField: e => e.CustomDistanceField, maxDistance: maxDistance, minDistance: minDistance).ToArray());
            }

            var results = GetResults(maxDistance: 3000000);

            Assert.AreEqual(3, results.Count());
            Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 3000000);

            results = GetResults(maxDistance: 600000);
            Assert.AreEqual(1, results.Count());
            Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 600000);

            results = GetResults(maxDistance: 17000000);
            Assert.AreEqual(4, results.Count());

            results = GetResults(minDistance: 600000);
            Assert.AreEqual(3, results.Count());
            Assert.IsTrue(results.Min(e => e.CustomDistanceField) > 600000);

            results = GetResults(maxDistance: 3000000, minDistance: 600000);
            Assert.AreEqual(2, results.Count());
            Assert.IsTrue(results.Max(e => e.CustomDistanceField) < 3000000);
            Assert.IsTrue(results.Min(e => e.CustomDistanceField) > 600000);
        }
Esempio n. 3
0
        public void SearchGeoNearRecordLimits()
        {
            var connection = TestConfiguration.GetConnection();
            var dbSet      = new MongoDbSet <SearchGeoModel>();

            dbSet.SetConnection(connection);

            for (var i = 0; i < 100; i++)
            {
                dbSet.Add(new SearchGeoModel
                {
                    Description        = $"Adelaide ({i})",
                    PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(138.600739, -34.928497)
                        )
                });
            }

            dbSet.AddRange(new SearchGeoModel[]
            {
                new SearchGeoModel {
                    Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(-74.005974, 40.712776)
                        )
                },
                new SearchGeoModel {
                    Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(115.860458, -31.950527)
                        )
                },
                new SearchGeoModel {
                    Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(147.327194, -42.882137)
                        )
                }
            });
            dbSet.SaveChanges();

            IQueryable <SearchGeoModel> WithGeoQuery()
            {
                return(dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                                               new GeoJson2DGeographicCoordinates(138, -30)
                                               )));
            }

            Assert.AreEqual(103, WithGeoQuery().Count());
            Assert.AreEqual(3, WithGeoQuery().Skip(100).Count());

            var afterSkipResult = WithGeoQuery().Skip(100).FirstOrDefault();

            Assert.AreEqual("Hobart", afterSkipResult.Description);

            var afterTakeResult = WithGeoQuery().Take(3).ToArray();

            Assert.AreEqual(3, afterTakeResult.Length);
            Assert.AreEqual("Adelaide (0)", afterTakeResult[0].Description);
        }
Esempio n. 4
0
        public void SearchGeoNear()
        {
            var connection = TestConfiguration.GetConnection();
            var dbSet      = new MongoDbSet <SearchGeoModel>();

            dbSet.SetConnection(connection);

            dbSet.AddRange(new SearchGeoModel[]
            {
                new SearchGeoModel {
                    Description = "New York", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(-74.005974, 40.712776)
                        )
                },
                new SearchGeoModel {
                    Description = "Adelaide", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(138.600739, -34.928497)
                        )
                },
                new SearchGeoModel {
                    Description = "Perth", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(115.860458, -31.950527)
                        )
                },
                new SearchGeoModel {
                    Description = "Hobart", PrimaryCoordinates = new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                        new GeoJson2DGeographicCoordinates(147.327194, -42.882137)
                        )
                }
            });
            dbSet.SaveChanges();

            var results = dbSet.SearchGeoNear(e => e.PrimaryCoordinates, new GeoJsonPoint <GeoJson2DGeographicCoordinates>(
                                                  new GeoJson2DGeographicCoordinates(138, -30)
                                                  )).ToArray();

            Assert.AreEqual(4, results.Count());
            Assert.AreEqual("Adelaide", results[0].Description);
            Assert.AreEqual("New York", results[3].Description);

            Assert.IsTrue(results[0].ExtraElements.ContainsKey("Distance"));
        }