DATAES-89 - implements missing "near" in ElasticsearchQueryCreator

This commit is contained in:
Franck MARCHAND
2014-06-09 23:22:51 +02:00
parent 5faf54b67c
commit 6d61dceab9
6 changed files with 124 additions and 3 deletions

View File

@@ -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());
}

View File

@@ -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);
}
}

View File

@@ -408,6 +408,7 @@ public class Criteria {
}
/**
* Creates new CriteriaEntry for bounding box created from points
*

View File

@@ -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);
}