Skip to content

DATAMONGO-1110 - Add support for $minDistance. #277

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 5 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>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-SNAPSHOT</version>
</dependency>

<dependency>
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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-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>1.7.0.BUILD-SNAPSHOT</version>
<version>1.7.0.DATAMONGO-1110-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,34 @@ public Criteria nearSphere(Point point) {
*/
public Criteria maxDistance(double maxDistance) {

if (createNearCriteriaForCommand("$near", maxDistance) || createNearCriteriaForCommand("$nearSphere", maxDistance)) {
if (createNearCriteriaForCommand("$near", "$maxDistance", maxDistance)
|| createNearCriteriaForCommand("$nearSphere", "$maxDistance", maxDistance)) {
return this;
}

criteria.put("$maxDistance", maxDistance);
return this;
}

/**
* Creates a geospatial criterion using a {@literal $minDistance} operation, for use with {@literal $near} or
* {@literal $nearSphere}.
*
* @param minDistance
* @return
* @since 1.7
*/
public Criteria minDistance(double minDistance) {

if (createNearCriteriaForCommand("$near", "$minDistance", minDistance)
|| createNearCriteriaForCommand("$nearSphere", "$minDistance", minDistance)) {
return this;
}

criteria.put("$minDistance", minDistance);
return this;
}

/**
* Creates a criterion using the {@literal $elemMatch} operator
*
Expand Down Expand Up @@ -605,7 +625,7 @@ private void setValue(DBObject dbo, String key, Object value) {
}
}

private boolean createNearCriteriaForCommand(String command, double maxDistance) {
private boolean createNearCriteriaForCommand(String command, String operation, double maxDistance) {

if (!criteria.containsKey(command)) {
return false;
Expand All @@ -615,14 +635,13 @@ private boolean createNearCriteriaForCommand(String command, double maxDistance)

if (existingNearOperationValue instanceof DBObject) {

((DBObject) existingNearOperationValue).put("$maxDistance", maxDistance);
((DBObject) existingNearOperationValue).put(operation, maxDistance);

return true;

} else if (existingNearOperationValue instanceof GeoJson) {

BasicDBObject dbo = new BasicDBObject("$geometry", existingNearOperationValue)
.append("$maxDistance", maxDistance);
BasicDBObject dbo = new BasicDBObject("$geometry", existingNearOperationValue).append(operation, maxDistance);
criteria.put(command, dbo);

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,7 @@ public final class NearQuery {
private final Point point;
private Query query;
private Distance maxDistance;
private Distance minDistance;
private Metric metric;
private boolean spherical;
private Integer num;
Expand Down Expand Up @@ -211,6 +212,63 @@ public NearQuery maxDistance(Distance distance) {
return this;
}

/**
* Sets the minimum distance results shall have from the configured origin. If a {@link Metric} was set before the
* given value will be interpreted as being a value in that metric. E.g.
*
* <pre>
* NearQuery query = near(10.0, 20.0, Metrics.KILOMETERS).minDistance(150);
* </pre>
*
* Will set the minimum distance to 150 kilometers.
*
* @param minDistance
* @return
* @since 1.7
*/
public NearQuery minDistance(double minDistance) {
return minDistance(new Distance(minDistance, getMetric()));
}

/**
* Sets the minimum distance supplied in a given metric. Will normalize the distance but not reconfigure the query's
* result {@link Metric} if one was configured before.
*
* @param minDistance
* @param metric must not be {@literal null}.
* @return
* @since 1.7
*/
public NearQuery minDistance(double minDistance, Metric metric) {

Assert.notNull(metric);
return minDistance(new Distance(minDistance, metric));
}

/**
* Sets the minimum distance to the given {@link Distance}. Will set the returned {@link Metric} to be the one of the
* given {@link Distance} if no {@link Metric} was set before.
*
* @param distance must not be {@literal null}.
* @return
* @since 1.7
*/
public NearQuery minDistance(Distance distance) {

Assert.notNull(distance);

if (distance.getMetric() != Metrics.NEUTRAL) {
this.spherical(true);
}

if (this.metric == null) {
in(distance.getMetric());
}

this.minDistance = distance;
return this;
}

/**
* Returns the maximum {@link Distance}.
*
Expand All @@ -220,6 +278,16 @@ public Distance getMaxDistance() {
return this.maxDistance;
}

/**
* Returns the maximum {@link Distance}.
*
* @return
* @since 1.7
*/
public Distance getMinDistance() {
return this.minDistance;
}

/**
* Configures a {@link CustomMetric} with the given multiplier.
*
Expand Down Expand Up @@ -352,7 +420,11 @@ public DBObject toDBObject() {
}

if (maxDistance != null) {
dbObject.put("maxDistance", this.maxDistance.getNormalizedValue());
dbObject.put("maxDistance", maxDistance.getNormalizedValue());
}

if (minDistance != null) {
dbObject.put("minDistance", minDistance.getNormalizedValue());
}

if (metric != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.geo.Distance;
Expand Down Expand Up @@ -361,11 +362,19 @@ private GeoResults<Object> doExecuteQuery(Query query) {
nearQuery.query(query);
}

Distance maxDistance = accessor.getMaxDistance();
Range<Distance> distances = accessor.getDistanceRange();
Distance maxDistance = distances.getUpperBound();

if (maxDistance != null) {
nearQuery.maxDistance(maxDistance).in(maxDistance.getMetric());
}

Distance minDistance = distances.getLowerBound();

if (minDistance != null) {
nearQuery.minDistance(minDistance).in(minDistance.getMetric());
}

Pageable pageable = accessor.getPageable();
if (pageable != null) {
nearQuery.with(pageable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
Expand Down Expand Up @@ -96,12 +97,13 @@ public Object getBindableValue(int index) {
return getConvertedValue(delegate.getBindableValue(index), null);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoParameterAccessor#getMaxDistance()
* @see org.springframework.data.mongodb.repository.query.MongoParameterAccessor#getDistanceRange()
*/
public Distance getMaxDistance() {
return delegate.getMaxDistance();
@Override
public Range<Distance> getDistanceRange() {
return delegate.getDistanceRange();
}

/*
Expand Down Expand Up @@ -252,4 +254,5 @@ public interface PotentiallyConvertingIterator extends Iterator<Object> {
*/
Object nextConverted(MongoPersistentProperty property);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package org.springframework.data.mongodb.repository.query;

import org.springframework.data.domain.Range;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.query.TextCriteria;
Expand All @@ -34,7 +35,7 @@ public interface MongoParameterAccessor extends ParameterAccessor {
* @return the maximum distance to apply to the geo query or {@literal null} if there's no {@link Distance} parameter
* at all or the given value for it was {@literal null}.
*/
Distance getMaxDistance();
Range<Distance> getDistanceRange();

/**
* Returns the {@link Point} to use for a geo-near query.
Expand Down
Loading