use of org.springframework.data.mongodb.core.query.NearQuery in project spring-data-mongodb by spring-projects.
the class ReactiveMongoQueryExecutionUnitTests method geoNearExecutionShouldApplyQuerySettings.
// DATAMONGO-1444
@Test
public void geoNearExecutionShouldApplyQuerySettings() throws Exception {
Method geoNear = ClassUtils.getMethod(GeoRepo.class, "geoNear");
Query query = new Query();
when(parameterAccessor.getGeoNearLocation()).thenReturn(new Point(1, 2));
when(parameterAccessor.getDistanceRange()).thenReturn(new Range<>(new Distance(10), new Distance(15)));
when(parameterAccessor.getPageable()).thenReturn(PageRequest.of(1, 10));
new GeoNearExecution(operations, parameterAccessor, ClassTypeInformation.fromReturnTypeOf(geoNear)).execute(query, Person.class, "person");
ArgumentCaptor<NearQuery> queryArgumentCaptor = ArgumentCaptor.forClass(NearQuery.class);
verify(operations).geoNear(queryArgumentCaptor.capture(), eq(Person.class), eq("person"));
NearQuery nearQuery = queryArgumentCaptor.getValue();
assertThat(nearQuery.toDocument().get("near"), is(equalTo(Arrays.asList(1d, 2d))));
assertThat(nearQuery.getSkip(), is(10L));
assertThat(nearQuery.getMinDistance(), is(equalTo(new Distance(10))));
assertThat(nearQuery.getMaxDistance(), is(equalTo(new Distance(15))));
}
use of org.springframework.data.mongodb.core.query.NearQuery in project spring-data-mongodb by spring-projects.
the class ReactiveMongoQueryExecutionUnitTests method geoNearExecutionShouldApplyMinimalSettings.
// DATAMONGO-1444
@Test
public void geoNearExecutionShouldApplyMinimalSettings() throws Exception {
Method geoNear = ClassUtils.getMethod(GeoRepo.class, "geoNear");
Query query = new Query();
when(parameterAccessor.getPageable()).thenReturn(Pageable.unpaged());
when(parameterAccessor.getGeoNearLocation()).thenReturn(new Point(1, 2));
when(parameterAccessor.getDistanceRange()).thenReturn(new Range<>(null, null));
new GeoNearExecution(operations, parameterAccessor, ClassTypeInformation.fromReturnTypeOf(geoNear)).execute(query, Person.class, "person");
ArgumentCaptor<NearQuery> queryArgumentCaptor = ArgumentCaptor.forClass(NearQuery.class);
verify(operations).geoNear(queryArgumentCaptor.capture(), eq(Person.class), eq("person"));
NearQuery nearQuery = queryArgumentCaptor.getValue();
assertThat(nearQuery.toDocument().get("near"), is(equalTo(Arrays.asList(1d, 2d))));
assertThat(nearQuery.getSkip(), is(0L));
assertThat(nearQuery.getMinDistance(), is(nullValue()));
assertThat(nearQuery.getMaxDistance(), is(nullValue()));
}
use of org.springframework.data.mongodb.core.query.NearQuery 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.query.NearQuery in project spring-data-mongodb by spring-projects.
the class GeoNearOperationUnitTests method rendersNearQueryAsAggregationOperation.
// DATAMONGO-1127
@Test
public void rendersNearQueryAsAggregationOperation() {
NearQuery query = NearQuery.near(10.0, 10.0);
GeoNearOperation operation = new GeoNearOperation(query, "distance");
Document document = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document nearClause = DocumentTestUtils.getAsDocument(document, "$geoNear");
Document expected = new Document(query.toDocument()).append("distanceField", "distance");
assertThat(nearClause, is(expected));
}
use of org.springframework.data.mongodb.core.query.NearQuery in project spring-data-mongodb by spring-projects.
the class GeoJsonTests method geoNear.
// DATAMONGO-1135
@Test
public void geoNear() {
NearQuery geoNear = NearQuery.near(new GeoJsonPoint(-73, 40), Metrics.KILOMETERS).num(10).maxDistance(150);
GeoResults<Venue2DSphere> result = template.geoNear(geoNear, Venue2DSphere.class);
assertThat(result.getContent().size(), is(not(0)));
assertThat(result.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
}
Aggregations