Skip to content
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

Fix rewrite of near & nearSphere count queries using geoJson to geoWithin. #4006

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-4004-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>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-4004-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>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-4004-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>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-4004-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.data.mongodb.core;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -162,7 +164,8 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
boolean spheric = source.containsKey("$nearSphere");
Object $near = spheric ? source.get("$nearSphere") : source.get("$near");

Number maxDistance = source.containsKey("$maxDistance") ? (Number) source.get("$maxDistance") : Double.MAX_VALUE;
Number maxDistance = getMaxDistance(source, $near, spheric);

List<Object> $centerMax = Arrays.asList(toCenterCoordinates($near), maxDistance);
Document $geoWithinMax = new Document("$geoWithin",
new Document(spheric ? "$centerSphere" : "$center", $centerMax));
Expand Down Expand Up @@ -193,6 +196,24 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
return new Document("$and", criteria);
}

private static Number getMaxDistance(Document source, Object $near, boolean spheric) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have MetricConversion for some conversion calculations. It would make sense to reuse those bits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some, not all. Still we could move those bits there 👍


Number maxDistance = Double.MAX_VALUE;
if(source.containsKey("$maxDistance")) { // legacy coordinate pair
maxDistance = (Number) source.get("$maxDistance");
} else if ($near instanceof Document) {
Document nearDoc = (Document)$near;
if(nearDoc.containsKey("$maxDistance")) {
maxDistance = (Number) nearDoc.get("$maxDistance");
// geojson is in Meters but we need radians x/(6378.1*1000)
if(spheric && nearDoc.containsKey("$geometry")) {
maxDistance = BigDecimal.valueOf(maxDistance.doubleValue()).divide(BigDecimal.valueOf(6378100d), MathContext.DECIMAL64).doubleValue();
}
}
}
return maxDistance;
}

private static boolean containsNear(Document source) {
return source.containsKey("$near") || source.containsKey("$nearSphere");
}
Expand All @@ -216,10 +237,16 @@ private static Object toCenterCoordinates(Object value) {
return Arrays.asList(((Point) value).getX(), ((Point) value).getY());
}

if (value instanceof Document && ((Document) value).containsKey("x")) {

Document point = (Document) value;
return Arrays.asList(point.get("x"), point.get("y"));
if (value instanceof Document ) {
Document document = (Document) value;
if(document.containsKey("x")) {
Document point = document;
return Arrays.asList(point.get("x"), point.get("y"));
}
else if (document.containsKey("$geometry")) {
Document geoJsonPoint = document.get("$geometry", Document.class);
return geoJsonPoint.get("coordinates");
}
}

return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,13 @@ public Criteria intersects(GeoJson geoJson) {
}

/**
* Creates a geo-spatial criterion using a {@literal $maxDistance} operation, for use with $near
* Creates a geo-spatial criterion using a {@literal $maxDistance} operation, for use with {@literal $near} or
* {@literal $nearSphere}.
* <p>
* <strong>NOTE:</strong> The unit of measure for distance may depends on the used coordinate representation
* (legacy vs. geoJson) as well as the target operation.
*
* @param maxDistance
* @param maxDistance radians or meters
* @return this.
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/maxDistance/">MongoDB Query operator:
* $maxDistance</a>
Expand All @@ -645,8 +649,11 @@ public Criteria maxDistance(double maxDistance) {
/**
* Creates a geospatial criterion using a {@literal $minDistance} operation, for use with {@literal $near} or
* {@literal $nearSphere}.
* <p>
* <strong>NOTE:</strong> The unit of measure for distance may depends on the used coordinate representation
* (legacy vs. geoJson) as well as the target operation.
*
* @param minDistance
* @param minDistance radians or meters
* @return this.
* @since 1.7
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
*/
package org.springframework.data.mongodb.core;

import static org.mockito.Mockito.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.test.util.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.query.Criteria;
Expand Down Expand Up @@ -155,6 +152,39 @@ void nearToGeoWithinWithMaxDistanceOrCombinedWithOtherCriteria() {
"{\"$or\" : [ { \"name\": \"food\" }, {\"location\": {\"$geoWithin\": {\"$center\": [[-73.99171, 40.738868], 10.0]}}} ]}"));
}

@Test // GH-4004
void nearToGeoWithinWithMaxDistanceUsingGeoJsonSource() {

Query source = query(new Criteria().orOperator(where("name").is("food"),
where("location").near(new GeoJsonPoint(-73.99171, 40.738868)).maxDistance(10)));

org.bson.Document target = postProcessQueryForCount(source);
assertThat(target).isEqualTo(org.bson.Document.parse(
"{\"$or\" : [ { \"name\": \"food\" }, {\"location\": {\"$geoWithin\": {\"$center\": [[-73.99171, 40.738868], 10.0]}}} ]}"));
}

@Test // GH-4004
void nearSphereToGeoWithinWithoutMaxDistanceUsingGeoJsonSource() {

Query source = query(new Criteria().orOperator(where("name").is("food"),
where("location").nearSphere(new GeoJsonPoint(-73.99171, 40.738868))));

org.bson.Document target = postProcessQueryForCount(source);
assertThat(target).isEqualTo(org.bson.Document.parse(
"{\"$or\" : [ { \"name\": \"food\" }, {\"location\": {\"$geoWithin\": {\"$centerSphere\": [[-73.99171, 40.738868], 1.7976931348623157E308]}}} ]}"));
}

@Test // GH-4004
void nearSphereToGeoWithinWithMaxDistanceUsingGeoJsonSource() {

Query source = query(new Criteria().orOperator(where("name").is("food"), where("location")
.nearSphere(new GeoJsonPoint(-73.99171, 40.738868)).maxDistance/*in meters for geojson*/(10d)));

org.bson.Document target = postProcessQueryForCount(source);
assertThat(target).isEqualTo(org.bson.Document.parse(
"{\"$or\" : [ { \"name\": \"food\" }, {\"location\": {\"$geoWithin\": {\"$centerSphere\": [[-73.99171, 40.738868], 1.567865038177514E-6]}}} ]}"));
}

private org.bson.Document postProcessQueryForCount(Query source) {

org.bson.Document intermediate = mapper.getMappedObject(source.getQueryObject(), (MongoPersistentEntity<?>) null);
Expand Down