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();
}
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());
}
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));
}
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();
}
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();
}
Aggregations