Search in sources :

Example 6 with GeospatialIndex

use of org.springframework.data.mongodb.core.index.GeospatialIndex in project spring-data-mongodb by spring-projects.

the class ExecutableFindOperationSupportTests method setUp.

@Before
public void setUp() {
    template = new MongoTemplate(new SimpleMongoDbFactory(new MongoClient(), "ExecutableFindOperationSupportTests"));
    template.dropCollection(STAR_WARS);
    template.dropCollection(STAR_WARS_PLANETS);
    template.indexOps(Planet.class).ensureIndex(new GeospatialIndex("coordinates").typed(GeoSpatialIndexType.GEO_2DSPHERE).named("planet-coordinate-idx"));
    initPersons();
    initPlanets();
}
Also used : MongoClient(com.mongodb.MongoClient) GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Before(org.junit.Before)

Example 7 with GeospatialIndex

use of org.springframework.data.mongodb.core.index.GeospatialIndex in project spring-data-mongodb by spring-projects.

the class IndexUnitTests method testGeospatialIndexGeoHaystack.

// DATAMONGO-778
@Test
public void testGeospatialIndexGeoHaystack() {
    GeospatialIndex i = new GeospatialIndex("location").typed(GeoSpatialIndexType.GEO_HAYSTACK).withAdditionalField("name").withBucketSize(40);
    assertEquals(Document.parse("{ \"location\" : \"geoHaystack\" , \"name\" : 1}"), i.getIndexKeys());
    assertEquals(Document.parse("{ \"bucketSize\" : 40.0}"), i.getIndexOptions());
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Test(org.junit.Test)

Example 8 with GeospatialIndex

use of org.springframework.data.mongodb.core.index.GeospatialIndex in project spring-data-mongodb by spring-projects.

the class AggregationTests method shouldSupportGeoNearQueriesForAggregationWithDistanceField.

// DATAMONGO-1127
@Test
public void shouldSupportGeoNearQueriesForAggregationWithDistanceField() {
    mongoTemplate.insert(new Venue("Penn Station", -73.99408, 40.75057));
    mongoTemplate.insert(new Venue("10gen Office", -73.99171, 40.738868));
    mongoTemplate.insert(new Venue("Flatiron Building", -73.988135, 40.741404));
    mongoTemplate.indexOps(Venue.class).ensureIndex(new GeospatialIndex("location"));
    NearQuery geoNear = NearQuery.near(-73, 40, Metrics.KILOMETERS).num(10).maxDistance(150);
    Aggregation agg = newAggregation(Aggregation.geoNear(geoNear, "distance"));
    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Venue.class, Document.class);
    assertThat(result.getMappedResults(), hasSize(3));
    Document firstResult = result.getMappedResults().get(0);
    assertThat(firstResult.containsKey("distance"), is(true));
    assertThat((Double) firstResult.get("distance"), closeTo(117.620092203928, 0.00001));
}
Also used : Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Venue(org.springframework.data.mongodb.core.Venue) GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Document(org.bson.Document) Test(org.junit.Test)

Example 9 with GeospatialIndex

use of org.springframework.data.mongodb.core.index.GeospatialIndex in project spring-data-mongodb by spring-projects.

the class ReactiveFindOperationSupportTests method findAllNearByWithCollectionAndProjection.

// DATAMONGO-1719
@Test
public void findAllNearByWithCollectionAndProjection() {
    blocking.indexOps(Planet.class).ensureIndex(new GeospatialIndex("coordinates").typed(GeoSpatialIndexType.GEO_2DSPHERE).named("planet-coordinate-idx"));
    Planet alderan = new Planet("alderan", new Point(-73.9836, 40.7538));
    Planet dantooine = new Planet("dantooine", new Point(-73.9928, 40.7193));
    blocking.save(alderan);
    blocking.save(dantooine);
    StepVerifier.create(template.query(Object.class).inCollection(STAR_WARS).as(Human.class).near(NearQuery.near(-73.9667, 40.78).spherical(true)).all()).consumeNextWith(actual -> {
        assertThat(actual.getDistance()).isNotNull();
        assertThat(actual.getContent()).isInstanceOf(Human.class);
        assertThat(actual.getContent().getId()).isEqualTo("alderan");
    }).expectNextCount(// 
    1).verifyComplete();
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Point(org.springframework.data.geo.Point) Test(org.junit.Test)

Example 10 with GeospatialIndex

use of org.springframework.data.mongodb.core.index.GeospatialIndex in project spring-data-mongodb by spring-projects.

the class ReactiveFindOperationSupportTests method findAllNearByReturningGeoResultContentAsOpenInterfaceProjection.

// DATAMONGO-1719
@Test
public void findAllNearByReturningGeoResultContentAsOpenInterfaceProjection() {
    blocking.indexOps(Planet.class).ensureIndex(new GeospatialIndex("coordinates").typed(GeoSpatialIndexType.GEO_2DSPHERE).named("planet-coordinate-idx"));
    Planet alderan = new Planet("alderan", new Point(-73.9836, 40.7538));
    Planet dantooine = new Planet("dantooine", new Point(-73.9928, 40.7193));
    blocking.save(alderan);
    blocking.save(dantooine);
    StepVerifier.create(template.query(Planet.class).as(PlanetSpELProjection.class).near(NearQuery.near(-73.9667, 40.78).spherical(true)).all()).consumeNextWith(it -> {
        assertThat(it.getDistance()).isNotNull();
        assertThat(it.getContent()).isInstanceOf(PlanetSpELProjection.class);
        assertThat(it.getContent().getId()).isEqualTo("alderan");
    }).expectNextCount(// 
    1).verifyComplete();
}
Also used : GeospatialIndex(org.springframework.data.mongodb.core.index.GeospatialIndex) Point(org.springframework.data.geo.Point) Test(org.junit.Test)

Aggregations

GeospatialIndex (org.springframework.data.mongodb.core.index.GeospatialIndex)10 Test (org.junit.Test)9 Point (org.springframework.data.geo.Point)4 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2 MongoClient (com.mongodb.MongoClient)1 Document (org.bson.Document)1 Before (org.junit.Before)1 Venue (org.springframework.data.mongodb.core.Venue)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1