DATAES-89 - implements missing "near" in ElasticsearchQueryCreator
This commit is contained in:
@@ -29,6 +29,7 @@ import org.elasticsearch.index.query.GeoDistanceFilterBuilder;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoBox;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
@@ -189,9 +190,17 @@ class CriteriaFilterProcessor {
|
||||
}
|
||||
|
||||
private void oneParameterBBox(GeoBoundingBoxFilterBuilder filter, Object value) {
|
||||
Assert.isTrue(value instanceof GeoBox, "single-element of boundedBy filter must be type of GeoBox");
|
||||
GeoBox geoBBox = (GeoBox) value;
|
||||
filter.topLeft(geoBBox.getTopLeft().getLat(), geoBBox.getTopLeft().getLon());
|
||||
Assert.isTrue(value instanceof GeoBox || value instanceof Box, "single-element of boundedBy filter must be type of GeoBox or Box");
|
||||
|
||||
GeoBox geoBBox;
|
||||
if(value instanceof Box) {
|
||||
Box sdbox = (Box) value;
|
||||
geoBBox = GeoBox.fromBox(sdbox);
|
||||
} else {
|
||||
geoBBox = (GeoBox) value;
|
||||
}
|
||||
|
||||
filter.topLeft(geoBBox.getTopLeft().getLat(), geoBBox.getTopLeft().getLon());
|
||||
filter.bottomRight(geoBBox.getBottomRight().getLat(), geoBBox.getBottomRight().getLon());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.data.elasticsearch.core.geo;
|
||||
|
||||
import org.springframework.data.geo.Box;
|
||||
|
||||
/**
|
||||
* Geo bbox used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
|
||||
*
|
||||
@@ -37,4 +39,18 @@ public class GeoBox {
|
||||
public GeoPoint getBottomRight() {
|
||||
return bottomRight;
|
||||
}
|
||||
|
||||
/**
|
||||
* return a {@link org.springframework.data.elasticsearch.core.geo.GeoBox}
|
||||
* from a {@link org.springframework.data.geo.Box}.
|
||||
*
|
||||
* @param box {@link org.springframework.data.geo.Box} to use
|
||||
* @return a {@link org.springframework.data.elasticsearch.core.geo.GeoBox}
|
||||
*/
|
||||
public static GeoBox fromBox(Box box) {
|
||||
GeoPoint topLeft = GeoPoint.fromPoint(box.getFirst());
|
||||
GeoPoint bottomRight = GeoPoint.fromPoint(box.getSecond());
|
||||
|
||||
return new GeoBox(topLeft, bottomRight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,6 +408,7 @@ public class Criteria {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates new CriteriaEntry for bounding box created from points
|
||||
*
|
||||
|
||||
@@ -20,10 +20,12 @@ import java.util.Iterator;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoBox;
|
||||
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.elasticsearch.core.query.Criteria;
|
||||
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||
import org.springframework.data.geo.Box;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
@@ -38,6 +40,7 @@ import org.springframework.data.repository.query.parser.PartTree;
|
||||
*
|
||||
* @author Rizwan Idrees
|
||||
* @author Mohsin Husen
|
||||
* @author Franck Marchand
|
||||
*/
|
||||
public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuery, CriteriaQuery> {
|
||||
|
||||
@@ -134,6 +137,29 @@ public class ElasticsearchQueryCreator extends AbstractQueryCreator<CriteriaQuer
|
||||
return criteria.within((Point)firstParameter, (Distance)secondParameter);
|
||||
|
||||
|
||||
if(firstParameter instanceof String && secondParameter instanceof String)
|
||||
return criteria.within((String)firstParameter, (String)secondParameter);
|
||||
}
|
||||
case NEAR : {
|
||||
Object firstParameter = parameters.next();
|
||||
|
||||
if(firstParameter instanceof GeoBox) {
|
||||
return criteria.boundedBy((GeoBox)firstParameter);
|
||||
}
|
||||
|
||||
if(firstParameter instanceof Box) {
|
||||
return criteria.boundedBy(GeoBox.fromBox((Box) firstParameter));
|
||||
}
|
||||
|
||||
Object secondParameter = parameters.next();
|
||||
|
||||
// "near" query can be the same query as the "within" query
|
||||
if(firstParameter instanceof GeoPoint && secondParameter instanceof String)
|
||||
return criteria.within((GeoPoint)firstParameter, (String)secondParameter);
|
||||
|
||||
if(firstParameter instanceof Point && secondParameter instanceof Distance)
|
||||
return criteria.within((Point)firstParameter, (Distance)secondParameter);
|
||||
|
||||
if(firstParameter instanceof String && secondParameter instanceof String)
|
||||
return criteria.within((String)firstParameter, (String)secondParameter);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user