Skip to content

Commit 5ed7e8e

Browse files
committed
DATAMONGO-1139 - MongoQueryCreator now only uses $nearSpherical if non-neutral Metric is used.
Fixed the evaluation of the Distance for a near clause handed into a query method. Previously we evaluated against null, which will never result in true as Distance returns Metrics.NEUTRAL by default.
1 parent fa85adf commit 5ed7e8e

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.slf4j.LoggerFactory;
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.geo.Distance;
28+
import org.springframework.data.geo.Metrics;
2829
import org.springframework.data.geo.Point;
2930
import org.springframework.data.geo.Shape;
3031
import org.springframework.data.mapping.context.MappingContext;
@@ -211,7 +212,7 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
211212
if (distance == null) {
212213
return criteria.near(point);
213214
} else {
214-
if (distance.getMetric() != null) {
215+
if (!Metrics.NEUTRAL.equals(distance.getMetric())) {
215216
criteria.nearSphere(point);
216217
} else {
217218
criteria.near(point);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.data.geo.Point;
4242
import org.springframework.data.mapping.context.MappingContext;
4343
import org.springframework.data.mongodb.core.Person;
44+
import org.springframework.data.mongodb.core.Venue;
4445
import org.springframework.data.mongodb.core.convert.MongoConverter;
4546
import org.springframework.data.mongodb.core.mapping.DBRef;
4647
import org.springframework.data.mongodb.core.mapping.Field;
@@ -466,6 +467,22 @@ public void shouldCreateRegexWhenUsingNotContainsOnStringProperty() {
466467
assertThat(query, is(query(where("username").regex(".*thew.*").not())));
467468
}
468469

470+
/**
471+
* @see DATAMONGO-1139
472+
*/
473+
@Test
474+
public void createsNonShericalNearForDistanceWithDefaultMetric() {
475+
476+
Point point = new Point(1.0, 1.0);
477+
Distance distance = new Distance(1.0);
478+
479+
PartTree tree = new PartTree("findByLocationNear", Venue.class);
480+
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, point, distance), context);
481+
Query query = creator.createQuery();
482+
483+
assertThat(query, is(query(where("location").near(point).maxDistance(1.0))));
484+
}
485+
469486
interface PersonRepository extends Repository<Person, Long> {
470487

471488
List<Person> findByLocationNearAndFirstname(Point location, Distance maxDistance, String firstname);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StubParameterAccessor.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
class StubParameterAccessor implements MongoParameterAccessor {
3535

3636
private final Object[] values;
37+
private Distance distance;
3738

3839
/**
3940
* Creates a new {@link ConvertingParameterAccessor} backed by a {@link StubParameterAccessor} simply returning the
@@ -48,7 +49,14 @@ public static ConvertingParameterAccessor getAccessor(MongoWriter<Object> conver
4849
}
4950

5051
public StubParameterAccessor(Object... values) {
52+
5153
this.values = values;
54+
55+
for (Object value : values) {
56+
if (value instanceof Distance) {
57+
this.distance = (Distance) value;
58+
}
59+
}
5260
}
5361

5462
/*
@@ -88,7 +96,7 @@ public Sort getSort() {
8896
* @see org.springframework.data.mongodb.repository.MongoParameterAccessor#getMaxDistance()
8997
*/
9098
public Distance getMaxDistance() {
91-
return null;
99+
return distance;
92100
}
93101

94102
/*

0 commit comments

Comments
 (0)