Search in sources :

Example 6 with NearQuery

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))));
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) GeoNearExecution(org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.GeoNearExecution) Method(java.lang.reflect.Method) Point(org.springframework.data.geo.Point) Person(org.springframework.data.mongodb.repository.Person) Distance(org.springframework.data.geo.Distance) Test(org.junit.Test)

Example 7 with NearQuery

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()));
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) GeoNearExecution(org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.GeoNearExecution) Method(java.lang.reflect.Method) Point(org.springframework.data.geo.Point) Person(org.springframework.data.mongodb.repository.Person) Test(org.junit.Test)

Example 8 with NearQuery

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));
}
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 NearQuery

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));
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Document(org.bson.Document) Test(org.junit.Test)

Example 10 with NearQuery

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));
}
Also used : NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Metric(org.springframework.data.geo.Metric) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)14 Document (org.bson.Document)6 Point (org.springframework.data.geo.Point)6 Metric (org.springframework.data.geo.Metric)3 Venue (org.springframework.data.mongodb.core.Venue)3 Method (java.lang.reflect.Method)2 GeospatialIndex (org.springframework.data.mongodb.core.index.GeospatialIndex)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 Query (org.springframework.data.mongodb.core.query.Query)2 Person (org.springframework.data.mongodb.repository.Person)2 GeoNearExecution (org.springframework.data.mongodb.repository.query.ReactiveMongoQueryExecution.GeoNearExecution)2 ReadPreference (com.mongodb.ReadPreference)1 Distance (org.springframework.data.geo.Distance)1 AutogenerateableId (org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId)1 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)1