Skip to content

DATAMONGO-2394 - Fix distance conversion for derived finder using near with GeoJson type and Distance. #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATAMONGO-2394-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATAMONGO-2394-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATAMONGO-2394-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<version>2.3.0.DATAMONGO-2394-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @author Mark Paluch
* @since 2.2
*/
class MetricConversion {
public class MetricConversion {

private static final BigDecimal METERS_MULTIPLIER = new BigDecimal(Metrics.KILOMETERS.getMultiplier())
.multiply(new BigDecimal(1000));
Expand All @@ -43,7 +43,7 @@ class MetricConversion {
* @param metric
* @return
*/
protected static double getMetersToMetricMultiplier(Metric metric) {
public static double getMetersToMetricMultiplier(Metric metric) {

ConversionMultiplier conversionMultiplier = ConversionMultiplier.builder().from(METERS_MULTIPLIER).to(metric)
.build();
Expand All @@ -56,7 +56,7 @@ protected static double getMetersToMetricMultiplier(Metric metric) {
* @param distance
* @return
*/
protected static double getDistanceInMeters(Distance distance) {
public static double getDistanceInMeters(Distance distance) {
return new BigDecimal(distance.getValue()).multiply(getMetricToMetersMultiplier(distance.getMetric()))
.doubleValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.geo.GeoJson;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.MetricConversion;
import org.springframework.data.mongodb.core.query.MongoRegexCreator;
import org.springframework.data.mongodb.core.query.MongoRegexCreator.MatchMode;
import org.springframework.data.mongodb.core.query.Query;
Expand Down Expand Up @@ -235,8 +237,14 @@ private Criteria from(Part part, MongoPersistentProperty property, Criteria crit
criteria.near(pointToUse);
}

criteria.maxDistance(it.getNormalizedValue());
minDistance.ifPresent(min -> criteria.minDistance(min.getNormalizedValue()));
if(pointToUse instanceof GeoJson) { // using GeoJson distance is in meters.

criteria.maxDistance(MetricConversion.getDistanceInMeters(it));
minDistance.map(MetricConversion::getDistanceInMeters).ifPresent(min -> criteria.minDistance(min));
} else {
criteria.maxDistance(it.getNormalizedValue());
minDistance.ifPresent(min -> criteria.minDistance(min.getNormalizedValue()));
}

return criteria;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public MongoClient mongoClient() {
protected String getDatabaseName() {
return "collation-tests";
}

@Override
protected boolean autoIndexCreation() {
return false;
}
}

@Autowired MongoTemplate template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ protected String getDatabaseName() {
return DB_NAME;
}

@Override
protected boolean autoIndexCreation() {
return false;
}

@Bean
MongoTransactionManager txManager(MongoDbFactory dbFactory) {
return new MongoTransactionManager(dbFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public MongoClient mongoClient() {
protected String getDatabaseName() {
return "validation-tests";
}

@Override
protected boolean autoIndexCreation() {
return false;
}
}

@Autowired MongoTemplate template;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ protected String getDatabaseName() {
public MongoClient mongoClient() {
return MongoTestUtils.client();
}

@Override
protected boolean autoIndexCreation() {
return false;
}
}

@Autowired MongoOperations mongoOps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

import org.bson.types.Code;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
Expand Down Expand Up @@ -69,7 +69,7 @@
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class QueryMapperUnitTests {

QueryMapper mapper;
Expand All @@ -78,8 +78,8 @@ public class QueryMapperUnitTests {

@Mock MongoDbFactory factory;

@Before
public void setUp() {
@BeforeEach
public void beforeEach() {

this.context = new MongoMappingContext();

Expand Down Expand Up @@ -890,6 +890,17 @@ public void findByIdUsesMappedIdFieldNameWithUnderscoreCorrectly() {
assertThat(target).isEqualTo(new org.bson.Document("_id", "id-1"));
}

@Test // DATAMONGO-2394
public void leavesDistanceUntouchedWhenUsingGeoJson() {

Query query = query(where("geoJsonPoint").near(new GeoJsonPoint(27.987901, 86.9165379)).maxDistance(1000));

org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(ClassWithGeoTypes.class));
assertThat(document).containsEntry("geoJsonPoint.$near.$geometry.type", "Point");
assertThat(document).containsEntry("geoJsonPoint.$near.$maxDistance", 1000.0D);
}

@Document
public class Foo {
@Id private ObjectId id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@

import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Range.Bound;
import org.springframework.data.geo.Distance;
Expand Down Expand Up @@ -73,8 +72,8 @@ public class MongoQueryCreatorUnitTests {
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> context;
MongoConverter converter;

@Before
public void setUp() {
@BeforeEach
public void beforeEach() {

context = new MongoMappingContext();

Expand Down Expand Up @@ -652,6 +651,17 @@ public void betweenShouldAllowSingleRageParameter() {
assertThat(creator.createQuery()).isEqualTo(query(where("age").gt(10).lt(11)));
}

@Test // DATAMONGO-2394
public void nearShouldUseMetricDistanceForGeoJsonTypes() {

GeoJsonPoint point = new GeoJsonPoint(27.987901, 86.9165379);
PartTree tree = new PartTree("findByLocationNear", User.class);
MongoQueryCreator creator = new MongoQueryCreator(tree,
getAccessor(converter, point, new Distance(1, Metrics.KILOMETERS)), context);

assertThat(creator.createQuery()).isEqualTo(query(where("location").nearSphere(point).maxDistance(1000.0D)));
}

interface PersonRepository extends Repository<Person, Long> {

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