DATAES-12 added support for GeoPoint

This commit is contained in:
Artur Konczak
2013-06-24 12:55:52 +01:00
parent 74a4035408
commit 5a499315e8
19 changed files with 1038 additions and 622 deletions

View File

@@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -13,19 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core.geo;
package org.springframework.data.elasticsearch.annotations;
import java.util.List;
import java.lang.annotation.*;
/**
* Geo polygon used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
*
* @author Franck Marchand
* @author Artur Konczak
*/
public class GeoPolygon {
private List<GeoLocation> points;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface GeoPointField {
public GeoPolygon(List<GeoLocation> points) {
this.points = points;
}
}

View File

@@ -16,8 +16,8 @@
package org.springframework.data.elasticsearch.core;
import org.elasticsearch.index.query.*;
import org.springframework.data.elasticsearch.core.geo.GeoBBox;
import org.springframework.data.elasticsearch.core.geo.GeoLocation;
import org.springframework.data.elasticsearch.core.geo.GeoEnvelope;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.util.Assert;
@@ -46,25 +46,25 @@ class CriteriaFilterProcessor {
while (chainIterator.hasNext()) {
FilterBuilder fb = null;
Criteria chainedCriteria = chainIterator.next();
if(chainedCriteria.isOr()){
fb = orFilter(createFilterFragmentForCriteria(chainedCriteria).toArray(new FilterBuilder[]{ }));
if (chainedCriteria.isOr()) {
fb = orFilter(createFilterFragmentForCriteria(chainedCriteria).toArray(new FilterBuilder[]{}));
fbList.add(fb);
}else if(chainedCriteria.isNegating()){
} else if (chainedCriteria.isNegating()) {
List<FilterBuilder> negationFilters = buildNegationFilter(criteria.getField().getName(), criteria.getFilterCriteriaEntries().iterator());
if(!negationFilters.isEmpty()) {
if (!negationFilters.isEmpty()) {
fbList.addAll(negationFilters);
}
}else {
} else {
fbList.addAll(createFilterFragmentForCriteria(chainedCriteria));
}
}
if(!fbList.isEmpty()) {
if(fbList.size() == 1) {
filter =fbList.get(0);
if (!fbList.isEmpty()) {
if (fbList.size() == 1) {
filter = fbList.get(0);
} else {
filter = andFilter(fbList.toArray(new FilterBuilder[]{ }));
filter = andFilter(fbList.toArray(new FilterBuilder[]{}));
}
}
@@ -77,10 +77,10 @@ class CriteriaFilterProcessor {
List<FilterBuilder> filterList = new LinkedList<FilterBuilder>();
String fieldName = chainedCriteria.getField().getName();
Assert.notNull(fieldName,"Unknown field");
Assert.notNull(fieldName, "Unknown field");
FilterBuilder filter = null;
while (it.hasNext()){
while (it.hasNext()) {
Criteria.CriteriaEntry entry = it.next();
filter = processCriteriaEntry(entry.getKey(), entry.getValue(), fieldName);
filterList.add(filter);
@@ -96,36 +96,53 @@ class CriteriaFilterProcessor {
}
FilterBuilder filter = null;
switch (key){
case WITHIN: {
switch (key) {
case WITHIN: {
filter = geoDistanceFilter(fieldName);
Assert.isTrue(value instanceof Object[], "Value of a geo distance filter should be an array of two values.");
Object[] valArray = (Object[]) value;
Assert.noNullElements(valArray, "Geo distance filter takes 2 not null elements array as parameter.");
Assert.isTrue(valArray.length == 2, "Geo distance filter takes a 2-elements array as parameter.");
Assert.isTrue(valArray[0] instanceof GeoLocation, "First element of a geo distance filter must be a GeoLocation");
Assert.isTrue(valArray[0] instanceof GeoPoint || valArray[0] instanceof String, "First element of a geo distance filter must be a GeoLocation or String");
Assert.isTrue(valArray[1] instanceof String, "Second element of a geo distance filter must be a String");
GeoLocation loc = (GeoLocation)valArray[0];
String dist = (String)valArray[1];
String dist = (String) valArray[1];
if (valArray[0] instanceof GeoPoint) {
GeoPoint loc = (GeoPoint) valArray[0];
((GeoDistanceFilterBuilder) filter).lat(loc.getLat()).lon(loc.getLon()).distance(dist);
} else {
String loc = (String) valArray[0];
if (loc.contains(",")) {
String c[] = loc.split(",");
((GeoDistanceFilterBuilder) filter).lat(Double.parseDouble(c[0])).lon(Double.parseDouble(c[1])).distance(dist);
} else {
((GeoDistanceFilterBuilder) filter).geohash(loc).distance(dist);
}
}
((GeoDistanceFilterBuilder)filter).lat(loc.getLat()).lon(loc.getLon()).distance(dist);
break;
}
case BBOX: {
filter = geoBoundingBoxFilter(fieldName);
Assert.isTrue(value instanceof Object[], "Value of a geo distance filter should be an array of two values.");
Assert.isTrue(value instanceof Object[], "Value of a bbox filter should be an array of one or two values.");
Object[] valArray = (Object[]) value;
Assert.noNullElements(valArray, "Geo bbox filter takes a not null element array as parameter.");
Assert.isTrue(valArray.length == 1, "Geo distance filter takes a 1-elements array as parameter.");
Assert.isTrue(valArray[0] instanceof GeoBBox, "single-element of a geo bbox filter must be a GeoBBox");
GeoBBox geoBBox = (GeoBBox)valArray[0];
((GeoBoundingBoxFilterBuilder)filter).topLeft(geoBBox.getTopLeft().getLat(), geoBBox.getTopLeft().getLon());
((GeoBoundingBoxFilterBuilder)filter).bottomRight(geoBBox.getBottomRight().getLat(), geoBBox.getBottomRight().getLon());
if (valArray.length == 1) {
//GeoEnvelop
oneParameterBBox((GeoBoundingBoxFilterBuilder) filter, valArray[0]);
} else if (valArray.length == 2) {
//2x GeoPoint
//2x String
twoParameterBBox((GeoBoundingBoxFilterBuilder) filter, valArray);
} else {
//error
Assert.isTrue(false, "Geo distance filter takes a 1-elements array(GeoEnvelop) or 2-elements array(GeoPoints or Strings(geohash)).");
}
break;
}
@@ -134,12 +151,43 @@ class CriteriaFilterProcessor {
return filter;
}
private List<FilterBuilder> buildNegationFilter(String fieldName, Iterator<Criteria.CriteriaEntry> it){
private void oneParameterBBox(GeoBoundingBoxFilterBuilder filter, Object value) {
Assert.isTrue(value instanceof GeoEnvelope, "single-element of a geo bbox filter must be type of GeoEnvelop");
GeoEnvelope geoBBox = (GeoEnvelope) value;
filter.topLeft(geoBBox.getTopLeft().getLat(), geoBBox.getTopLeft().getLon());
filter.bottomRight(geoBBox.getBottomRight().getLat(), geoBBox.getBottomRight().getLon());
}
private static boolean isType(Object[] array, Class clazz) {
for (Object o : array) {
if (!clazz.isInstance(o)) {
return false;
}
}
return true;
}
private void twoParameterBBox(GeoBoundingBoxFilterBuilder filter, Object[] values) {
Assert.isTrue(isType(values, GeoPoint.class) || isType(values, String.class), " both elements of geo bbox filter must be type of GeoPoint or String(geohash)");
if (values[0] instanceof GeoPoint) {
GeoPoint topLeft = (GeoPoint) values[0];
GeoPoint bottomRight = (GeoPoint) values[1];
filter.topLeft(topLeft.getLat(), topLeft.getLon());
filter.bottomRight(bottomRight.getLat(), bottomRight.getLon());
} else {
String topLeft = (String) values[0];
String bottomRight = (String) values[1];
filter.topLeft(topLeft);
filter.bottomRight(bottomRight);
}
}
private List<FilterBuilder> buildNegationFilter(String fieldName, Iterator<Criteria.CriteriaEntry> it) {
List<FilterBuilder> notFilterList = new LinkedList<FilterBuilder>();
while (it.hasNext()){
while (it.hasNext()) {
Criteria.CriteriaEntry criteriaEntry = it.next();
FilterBuilder notFilter = notFilter(processCriteriaEntry(criteriaEntry.getKey(), criteriaEntry.getValue(), fieldName));
FilterBuilder notFilter = notFilter(processCriteriaEntry(criteriaEntry.getKey(), criteriaEntry.getValue(), fieldName));
notFilterList.add(notFilter);
}

View File

@@ -133,6 +133,16 @@ public interface ElasticsearchOperations {
*/
<T> List<T> queryForList(StringQuery query, Class<T> clazz);
/**
* Execute the search query against elasticsearch and return result as {@link List}
*
* @param query
* @param clazz
* @param <T>
* @return
*/
<T> List<T> queryForList(SearchQuery query, Class<T> clazz);
/**
* Execute the query against elasticsearch and return ids
*

View File

@@ -170,6 +170,11 @@ public class ElasticsearchTemplate implements ElasticsearchOperations {
return queryForPage(query, clazz).getContent();
}
@Override
public <T> List<T> queryForList(SearchQuery query, Class<T> clazz) {
return queryForPage(query, clazz).getContent();
}
@Override
public <T> List<String> queryForIds(SearchQuery query) {
SearchRequestBuilder request = prepareSearch(query).setQuery(query.getQuery()).setNoFields();

View File

@@ -18,7 +18,7 @@ package org.springframework.data.elasticsearch.core;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.facet.FacetRequest;
import org.springframework.data.elasticsearch.core.geo.GeoLocation;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
@@ -48,6 +48,7 @@ class MappingBuilder {
public static final String INDEX_VALUE_NOT_ANALYZED = "not_analyzed";
public static final String TYPE_VALUE_STRING = "string";
public static final String TYPE_VALUE_OBJECT = "object";
public static final String TYPE_VALUE_GEO_POINT = "geo_point";
private static SimpleTypeHolder SIMPLE_TYPE_HOLDER = new SimpleTypeHolder();
@@ -73,12 +74,14 @@ class MappingBuilder {
mapEntity(xContentBuilder, field.getType(), false, EMPTY, field.getName());
}
if(field.getType() == GeoLocation.class) {
applyGeoLocationFieldMapping(xContentBuilder, field);
}
Field singleField = field.getAnnotation(Field.class);
MultiField multiField = field.getAnnotation(MultiField.class);
GeoPointField geoPoint = field.getAnnotation(GeoPointField.class);
if (field.getType() == GeoPoint.class || geoPoint != null) {
applyGeoPointFieldMapping(xContentBuilder, field);
}
if (isRootObject && singleField != null && isIdField(field, idFieldName)) {
applyDefaultIdFieldMapping(xContentBuilder, field);
} else if (multiField != null) {
@@ -94,10 +97,10 @@ class MappingBuilder {
}
private static void applyGeoLocationFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
private static void applyGeoPointFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field) throws IOException {
xContentBuilder.startObject(field.getName());
xContentBuilder.field("type", "geo_point")
.endObject();
xContentBuilder.field(FIELD_TYPE, TYPE_VALUE_GEO_POINT)
.endObject();
}
private static void applyDefaultIdFieldMapping(XContentBuilder xContentBuilder, java.lang.reflect.Field field)

View File

@@ -15,35 +15,27 @@
*/
package org.springframework.data.elasticsearch.core.geo;
import java.util.List;
/**
* Geo bbox used for #{@link org.springframework.data.elasticsearch.core.query.Criteria}.
*
* @author Franck Marchand
*/
public class GeoBBox {
private GeoLocation topLeft;
private GeoLocation bottomRight;
public class GeoEnvelope {
public GeoBBox(GeoLocation topLeft, GeoLocation bottomRight) {
private GeoPoint topLeft;
private GeoPoint bottomRight;
public GeoEnvelope(GeoPoint topLeft, GeoPoint bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
public GeoLocation getTopLeft() {
public GeoPoint getTopLeft() {
return topLeft;
}
public void setTopLeft(GeoLocation topLeft) {
this.topLeft = topLeft;
}
public GeoLocation getBottomRight() {
public GeoPoint getBottomRight() {
return bottomRight;
}
public void setBottomRight(GeoLocation bottomRight) {
this.bottomRight = bottomRight;
}
}

View File

@@ -20,24 +20,16 @@ package org.springframework.data.elasticsearch.core.geo;
*
* @author Franck Marchand
*/
public class GeoLocation {
public class GeoPoint {
private double lat;
private double lon;
public GeoLocation lat(double lat) {
setLat(lat);
return this;
private GeoPoint() {
//required by mapper to instantiate object
}
public GeoLocation lon(double lon) {
setLon(lon);
return this;
}
public GeoLocation() {
}
public GeoLocation(double latitude, double longitude) {
public GeoPoint(double latitude, double longitude) {
this.lat = latitude;
this.lon = longitude;
}
@@ -46,15 +38,8 @@ public class GeoLocation {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
}

View File

@@ -25,341 +25,341 @@ import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.elasticsearch.core.geo.GeoBBox;
import org.springframework.data.elasticsearch.core.geo.GeoLocation;
import org.springframework.data.elasticsearch.core.geo.GeoEnvelope;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.util.Assert;
/**
* Criteria is the central class when constructing queries. It follows more or less a fluent API style, which allows to
* easily chain together multiple criteria.
*
*
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Franck Marchand
*/
public class Criteria {
public static final String WILDCARD = "*";
public static final String CRITERIA_VALUE_SEPERATOR = " ";
public static final String WILDCARD = "*";
public static final String CRITERIA_VALUE_SEPERATOR = " ";
private static final String OR_OPERATOR = " OR ";
private static final String AND_OPERATOR = " AND ";
private static final String OR_OPERATOR = " OR ";
private static final String AND_OPERATOR = " AND ";
private Field field;
private float boost = Float.NaN;
private boolean negating = false;
private Field field;
private float boost = Float.NaN;
private boolean negating = false;
private List<Criteria> criteriaChain = new ArrayList<Criteria>(1);
private List<Criteria> criteriaChain = new ArrayList<Criteria>(1);
private Set<CriteriaEntry> queryCriteria = new LinkedHashSet<CriteriaEntry>();
private Set<CriteriaEntry> queryCriteria = new LinkedHashSet<CriteriaEntry>();
private Set<CriteriaEntry> filterCriteria = new LinkedHashSet<CriteriaEntry>();
public Criteria() {
}
public Criteria() {
}
/**
* Creates a new CriterSimpleFieldia for the Filed with provided name
*
* @param fieldname
*/
public Criteria(String fieldname) {
this(new SimpleField(fieldname));
}
/**
* Creates a new CriterSimpleFieldia for the Filed with provided name
*
* @param fieldname
*/
public Criteria(String fieldname) {
this(new SimpleField(fieldname));
}
/**
* Creates a new Criteria for the given field
*
* @param field
*/
public Criteria(Field field) {
Assert.notNull(field, "Field for criteria must not be null");
Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
/**
* Creates a new Criteria for the given field
*
* @param field
*/
public Criteria(Field field) {
Assert.notNull(field, "Field for criteria must not be null");
Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
this.criteriaChain.add(this);
this.field = field;
}
this.criteriaChain.add(this);
this.field = field;
}
protected Criteria(List<Criteria> criteriaChain, String fieldname) {
this(criteriaChain, new SimpleField(fieldname));
}
protected Criteria(List<Criteria> criteriaChain, String fieldname) {
this(criteriaChain, new SimpleField(fieldname));
}
protected Criteria(List<Criteria> criteriaChain, Field field) {
Assert.notNull(criteriaChain, "CriteriaChain must not be null");
Assert.notNull(field, "Field for criteria must not be null");
Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
protected Criteria(List<Criteria> criteriaChain, Field field) {
Assert.notNull(criteriaChain, "CriteriaChain must not be null");
Assert.notNull(field, "Field for criteria must not be null");
Assert.hasText(field.getName(), "Field.name for criteria must not be null/empty");
this.criteriaChain.addAll(criteriaChain);
this.criteriaChain.add(this);
this.field = field;
}
this.criteriaChain.addAll(criteriaChain);
this.criteriaChain.add(this);
this.field = field;
}
/**
* Static factory method to create a new Criteria for field with given name
*
* @param field
* @return
*/
public static Criteria where(String field) {
return where(new SimpleField(field));
}
/**
* Static factory method to create a new Criteria for field with given name
*
* @param field
* @return
*/
public static Criteria where(String field) {
return where(new SimpleField(field));
}
/**
* Static factory method to create a new Criteria for provided field
*
* @param field
* @return
*/
public static Criteria where(Field field) {
return new Criteria(field);
}
/**
* Static factory method to create a new Criteria for provided field
*
* @param field
* @return
*/
public static Criteria where(Field field) {
return new Criteria(field);
}
/**
* Chain using {@code AND}
*
* @param field
* @return
*/
public Criteria and(Field field) {
return new Criteria(this.criteriaChain, field);
}
/**
* Chain using {@code AND}
*
* @param field
* @return
*/
public Criteria and(Field field) {
return new Criteria(this.criteriaChain, field);
}
/**
* Chain using {@code AND}
*
* @param fieldName
* @return
*/
public Criteria and(String fieldName) {
return new Criteria(this.criteriaChain, fieldName);
}
/**
* Chain using {@code AND}
*
* @param fieldName
* @return
*/
public Criteria and(String fieldName) {
return new Criteria(this.criteriaChain, fieldName);
}
/**
* Chain using {@code AND}
*
* @param criteria
* @return
*/
public Criteria and(Criteria criteria) {
this.criteriaChain.add(criteria);
return this;
}
/**
* Chain using {@code AND}
*
* @param criteria
* @return
*/
public Criteria and(Criteria criteria) {
this.criteriaChain.add(criteria);
return this;
}
/**
* Chain using {@code AND}
*
* @param criterias
* @return
*/
public Criteria and(Criteria... criterias) {
this.criteriaChain.addAll(Arrays.asList(criterias));
return this;
}
/**
* Chain using {@code AND}
*
* @param criterias
* @return
*/
public Criteria and(Criteria... criterias) {
this.criteriaChain.addAll(Arrays.asList(criterias));
return this;
}
/**
* Chain using {@code OR}
*
* @param field
* @return
*/
public Criteria or(Field field) {
return new OrCriteria(this.criteriaChain, field);
}
/**
* Chain using {@code OR}
*
* @param field
* @return
*/
public Criteria or(Field field) {
return new OrCriteria(this.criteriaChain, field);
}
/**
* Chain using {@code OR}
*
* @param criteria
* @return
*/
public Criteria or(Criteria criteria) {
Assert.notNull(criteria, "Cannot chain 'null' criteria.");
/**
* Chain using {@code OR}
*
* @param criteria
* @return
*/
public Criteria or(Criteria criteria) {
Assert.notNull(criteria, "Cannot chain 'null' criteria.");
Criteria orConnectedCritiera = new OrCriteria(this.criteriaChain, criteria.getField());
orConnectedCritiera.queryCriteria.addAll(criteria.queryCriteria);
return orConnectedCritiera;
}
Criteria orConnectedCritiera = new OrCriteria(this.criteriaChain, criteria.getField());
orConnectedCritiera.queryCriteria.addAll(criteria.queryCriteria);
return orConnectedCritiera;
}
/**
* Chain using {@code OR}
*
* @param fieldName
* @return
*/
public Criteria or(String fieldName) {
return or(new SimpleField(fieldName));
}
/**
* Chain using {@code OR}
*
* @param fieldName
* @return
*/
public Criteria or(String fieldName) {
return or(new SimpleField(fieldName));
}
/**
* Crates new CriteriaEntry without any wildcards
*
* @param o
* @return
*/
public Criteria is(Object o) {
queryCriteria.add(new CriteriaEntry(OperationKey.EQUALS, o));
return this;
}
/**
* Crates new CriteriaEntry without any wildcards
*
* @param o
* @return
*/
public Criteria is(Object o) {
queryCriteria.add(new CriteriaEntry(OperationKey.EQUALS, o));
return this;
}
/**
* Crates new CriteriaEntry with leading and trailing wildcards <br/>
* <strong>NOTE: </strong> mind your schema as leading wildcards may not be supported and/or execution might be slow.
*
* @param s
* @return
*/
public Criteria contains(String s) {
assertNoBlankInWildcardedQuery(s, true, true);
queryCriteria.add(new CriteriaEntry(OperationKey.CONTAINS, s));
return this;
}
/**
* Crates new CriteriaEntry with leading and trailing wildcards <br/>
* <strong>NOTE: </strong> mind your schema as leading wildcards may not be supported and/or execution might be slow.
*
* @param s
* @return
*/
public Criteria contains(String s) {
assertNoBlankInWildcardedQuery(s, true, true);
queryCriteria.add(new CriteriaEntry(OperationKey.CONTAINS, s));
return this;
}
/**
* Crates new CriteriaEntry with trailing wildcard
*
* @param s
* @return
*/
public Criteria startsWith(String s) {
assertNoBlankInWildcardedQuery(s, true, false);
queryCriteria.add(new CriteriaEntry(OperationKey.STARTS_WITH, s));
return this;
}
/**
* Crates new CriteriaEntry with trailing wildcard
*
* @param s
* @return
*/
public Criteria startsWith(String s) {
assertNoBlankInWildcardedQuery(s, true, false);
queryCriteria.add(new CriteriaEntry(OperationKey.STARTS_WITH, s));
return this;
}
/**
* Crates new CriteriaEntry with leading wildcard <br />
* <strong>NOTE: </strong> mind your schema and execution times as leading wildcards may not be supported.
*
* @param s
* @return
*/
public Criteria endsWith(String s) {
assertNoBlankInWildcardedQuery(s, false, true);
queryCriteria.add(new CriteriaEntry(OperationKey.ENDS_WITH, s));
return this;
}
/**
* Crates new CriteriaEntry with leading wildcard <br />
* <strong>NOTE: </strong> mind your schema and execution times as leading wildcards may not be supported.
*
* @param s
* @return
*/
public Criteria endsWith(String s) {
assertNoBlankInWildcardedQuery(s, false, true);
queryCriteria.add(new CriteriaEntry(OperationKey.ENDS_WITH, s));
return this;
}
/**
* Crates new CriteriaEntry with trailing -
*
* @return
*/
public Criteria not() {
this.negating = true;
return this;
}
/**
* Crates new CriteriaEntry with trailing -
*
* @return
*/
public Criteria not() {
this.negating = true;
return this;
}
/**
* Crates new CriteriaEntry with trailing ~
*
* @param s
* @return
*/
public Criteria fuzzy(String s) {
/**
* Crates new CriteriaEntry with trailing ~
*
* @param s
* @return
*/
public Criteria fuzzy(String s) {
queryCriteria.add(new CriteriaEntry(OperationKey.FUZZY, s));
return this;
}
}
/**
* Crates new CriteriaEntry allowing native elasticsearch expressions
*
* @param s
* @return
*/
public Criteria expression(String s) {
queryCriteria.add(new CriteriaEntry(OperationKey.EXPRESSION, s));
return this;
}
/**
* Crates new CriteriaEntry allowing native elasticsearch expressions
*
* @param s
* @return
*/
public Criteria expression(String s) {
queryCriteria.add(new CriteriaEntry(OperationKey.EXPRESSION, s));
return this;
}
/**
* Boost positive hit with given factor. eg. ^2.3
*
* @param boost
* @return
*/
public Criteria boost(float boost) {
if (boost < 0) {
throw new InvalidDataAccessApiUsageException("Boost must not be negative.");
}
this.boost = boost;
return this;
}
/**
* Boost positive hit with given factor. eg. ^2.3
*
* @param boost
* @return
*/
public Criteria boost(float boost) {
if (boost < 0) {
throw new InvalidDataAccessApiUsageException("Boost must not be negative.");
}
this.boost = boost;
return this;
}
/**
* Crates new CriteriaEntry for {@code RANGE [lowerBound TO upperBound]}
*
* @param lowerBound
* @param upperBound
* @return
*/
public Criteria between(Object lowerBound, Object upperBound) {
if (lowerBound == null && upperBound == null) {
throw new InvalidDataAccessApiUsageException("Range [* TO *] is not allowed");
}
/**
* Crates new CriteriaEntry for {@code RANGE [lowerBound TO upperBound]}
*
* @param lowerBound
* @param upperBound
* @return
*/
public Criteria between(Object lowerBound, Object upperBound) {
if (lowerBound == null && upperBound == null) {
throw new InvalidDataAccessApiUsageException("Range [* TO *] is not allowed");
}
queryCriteria.add(new CriteriaEntry(OperationKey.BETWEEN, new Object[]{lowerBound, upperBound}));
return this;
}
queryCriteria.add(new CriteriaEntry(OperationKey.BETWEEN, new Object[]{lowerBound, upperBound}));
return this;
}
/**
* Crates new CriteriaEntry for {@code RANGE [* TO upperBound]}
*
* @param upperBound
* @return
*/
public Criteria lessThanEqual(Object upperBound) {
between(null, upperBound);
return this;
}
/**
* Crates new CriteriaEntry for {@code RANGE [* TO upperBound]}
*
* @param upperBound
* @return
*/
public Criteria lessThanEqual(Object upperBound) {
between(null, upperBound);
return this;
}
/**
* Crates new CriteriaEntry for {@code RANGE [lowerBound TO *]}
*
* @param lowerBound
* @return
*/
public Criteria greaterThanEqual(Object lowerBound) {
between(lowerBound, null);
return this;
}
/**
* Crates new CriteriaEntry for {@code RANGE [lowerBound TO *]}
*
* @param lowerBound
* @return
*/
public Criteria greaterThanEqual(Object lowerBound) {
between(lowerBound, null);
return this;
}
/**
* Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
*
* @param values
* @return
*/
public Criteria in(Object... values) {
if (values.length == 0 || (values.length > 1 && values[1] instanceof Collection)) {
throw new InvalidDataAccessApiUsageException("At least one element "
+ (values.length > 0 ? ("of argument of type " + values[1].getClass().getName()) : "")
+ " has to be present.");
}
return in(Arrays.asList(values));
}
/**
* Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
*
* @param values
* @return
*/
public Criteria in(Object... values) {
if (values.length == 0 || (values.length > 1 && values[1] instanceof Collection)) {
throw new InvalidDataAccessApiUsageException("At least one element "
+ (values.length > 0 ? ("of argument of type " + values[1].getClass().getName()) : "")
+ " has to be present.");
}
return in(Arrays.asList(values));
}
/**
* Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
*
* @param values the collection containing the values to match against
* @return
*/
public Criteria in(Iterable<?> values) {
Assert.notNull(values, "Collection of 'in' values must not be null");
/**
* Crates new CriteriaEntry for multiple values {@code (arg0 arg1 arg2 ...)}
*
* @param values the collection containing the values to match against
* @return
*/
public Criteria in(Iterable<?> values) {
Assert.notNull(values, "Collection of 'in' values must not be null");
queryCriteria.add(new CriteriaEntry(OperationKey.IN, values));
return this;
}
/**
* Creates new CriteriaEntry for {@code location WITHIN distance}
* @param location {@link GeoLocation} center coordinates
* @param distance {@link String} radius as a string (e.g. : '100km').
* Distance unit :
* either mi/miles or km can be set
*
* @param location {@link org.springframework.data.elasticsearch.core.geo.GeoPoint} center coordinates
* @param distance {@link String} radius as a string (e.g. : '100km').
* Distance unit :
* either mi/miles or km can be set
* @return Criteria the chaind criteria with the new 'within' criteria included.
*/
public Criteria within(GeoLocation location, String distance) {
public Criteria within(GeoPoint location, String distance) {
Assert.notNull(location, "Location value for near criteria must not be null");
Assert.notNull(location, "Distance value for near criteria must not be null");
filterCriteria.add(new CriteriaEntry(OperationKey.WITHIN, new Object[]{location, distance}));
@@ -367,125 +367,172 @@ public class Criteria {
}
/**
* Creates new CriteriaEntry for {@code location BBOX bounding box}
* @param bbox {@link org.springframework.data.elasticsearch.core.geo.GeoBBox} center coordinates
* Creates new CriteriaEntry for {@code geoLocation WITHIN distance}
*
* @param geoLocation {@link String} center point
* supported formats:
* lat on = > "41.2,45.1",
* geohash = > "asd9as0d"
* @param distance {@link String} radius as a string (e.g. : '100km').
* Distance unit :
* either mi/miles or km can be set
* @return
*/
public Criteria within(String geoLocation, String distance) {
Assert.isTrue(StringUtils.isNotBlank(geoLocation), "geoLocation value must not be null");
filterCriteria.add(new CriteriaEntry(OperationKey.WITHIN, new Object[]{geoLocation, distance}));
return this;
}
/**
* Creates new CriteriaEntry for {@code location BBOX bounding box}
*
* @param bbox {@link org.springframework.data.elasticsearch.core.geo.GeoEnvelope} bounding box(left top corner + right bottom corner)
* @return Criteria the chaind criteria with the new 'bbox' criteria included.
*/
public Criteria bbox(GeoBBox bbox) {
public Criteria bbox(GeoEnvelope bbox) {
Assert.notNull(bbox, "bbox value for bbox criteria must not be null");
filterCriteria.add(new CriteriaEntry(OperationKey.BBOX, new Object[]{bbox}));
return this;
}
}
private void assertNoBlankInWildcardedQuery(String searchString, boolean leadingWildcard, boolean trailingWildcard) {
if (StringUtils.contains(searchString, CRITERIA_VALUE_SEPERATOR)) {
throw new InvalidDataAccessApiUsageException("Cannot constructQuery '" + (leadingWildcard ? "*" : "") + "\""
+ searchString + "\"" + (trailingWildcard ? "*" : "") + "'. Use epxression or mulitple clauses instead.");
}
}
/**
* Field targeted by this Criteria
*
* @return
*/
public Field getField() {
return this.field;
}
/**
* Creates new CriteriaEntry for bounding box created from points
*
* @param topLeft left top corner of bounding box
* @param bottomRight right bottom corner of bounding box
* @return Criteria the chaind criteria with the new 'bbox' criteria included.
*/
public Criteria bbox(String topLeft, String bottomRight) {
Assert.isTrue(StringUtils.isNotBlank(topLeft), "topLeft point must not be empty");
Assert.isTrue(StringUtils.isNotBlank(bottomRight), "bottomRight point must not be empty");
filterCriteria.add(new CriteriaEntry(OperationKey.BBOX, new Object[]{topLeft, bottomRight}));
return this;
}
public Set<CriteriaEntry> getQueryCriteriaEntries() {
return Collections.unmodifiableSet(this.queryCriteria);
}
/**
* Creates new CriteriaEntry for bounding box created from points
*
* @param topLeft left top corner of bounding box
* @param bottomRight right bottom corner of bounding box
* @return Criteria the chaind criteria with the new 'bbox' criteria included.
*/
public Criteria bbox(GeoPoint topLeft, GeoPoint bottomRight) {
Assert.notNull(topLeft, "topLeft point must not be null");
Assert.notNull(bottomRight, "bottomRight point must not be null");
filterCriteria.add(new CriteriaEntry(OperationKey.BBOX, new Object[]{topLeft, bottomRight}));
return this;
}
public Set<CriteriaEntry> getFilterCriteriaEntries() {
return Collections.unmodifiableSet(this.filterCriteria);
}
private void assertNoBlankInWildcardedQuery(String searchString, boolean leadingWildcard, boolean trailingWildcard) {
if (StringUtils.contains(searchString, CRITERIA_VALUE_SEPERATOR)) {
throw new InvalidDataAccessApiUsageException("Cannot constructQuery '" + (leadingWildcard ? "*" : "") + "\""
+ searchString + "\"" + (trailingWildcard ? "*" : "") + "'. Use epxression or mulitple clauses instead.");
}
}
/**
* Field targeted by this Criteria
*
* @return
*/
public Field getField() {
return this.field;
}
public Set<CriteriaEntry> getQueryCriteriaEntries() {
return Collections.unmodifiableSet(this.queryCriteria);
}
public Set<CriteriaEntry> getFilterCriteriaEntries() {
return Collections.unmodifiableSet(this.filterCriteria);
}
public Set<CriteriaEntry> getFilterCriteria() {
return filterCriteria;
}
/**
* Conjunction to be used with this criteria (AND | OR)
*
* @return
*/
public String getConjunctionOperator() {
return AND_OPERATOR;
}
/**
* Conjunction to be used with this criteria (AND | OR)
*
* @return
*/
public String getConjunctionOperator() {
return AND_OPERATOR;
}
public List<Criteria> getCriteriaChain() {
return Collections.unmodifiableList(this.criteriaChain);
}
public List<Criteria> getCriteriaChain() {
return Collections.unmodifiableList(this.criteriaChain);
}
public boolean isNegating() {
return this.negating;
}
public boolean isNegating() {
return this.negating;
}
public boolean isAnd() {
return AND_OPERATOR == getConjunctionOperator();
}
public boolean isAnd() {
return AND_OPERATOR == getConjunctionOperator();
}
public boolean isOr() {
return OR_OPERATOR == getConjunctionOperator();
}
public boolean isOr() {
return OR_OPERATOR == getConjunctionOperator();
}
public float getBoost() {
return this.boost;
}
public float getBoost() {
return this.boost;
}
static class OrCriteria extends Criteria {
static class OrCriteria extends Criteria {
public OrCriteria() {
super();
}
public OrCriteria() {
super();
}
public OrCriteria(Field field) {
super(field);
}
public OrCriteria(Field field) {
super(field);
}
public OrCriteria(List<Criteria> criteriaChain, Field field) {
super(criteriaChain, field);
}
public OrCriteria(List<Criteria> criteriaChain, Field field) {
super(criteriaChain, field);
}
public OrCriteria(List<Criteria> criteriaChain, String fieldname) {
super(criteriaChain, fieldname);
}
public OrCriteria(List<Criteria> criteriaChain, String fieldname) {
super(criteriaChain, fieldname);
}
public OrCriteria(String fieldname) {
super(fieldname);
}
public OrCriteria(String fieldname) {
super(fieldname);
}
@Override
public String getConjunctionOperator() {
return OR_OPERATOR;
}
@Override
public String getConjunctionOperator() {
return OR_OPERATOR;
}
}
}
public enum OperationKey {
EQUALS, CONTAINS, STARTS_WITH, ENDS_WITH, EXPRESSION, BETWEEN, FUZZY, IN, WITHIN, BBOX, NEAR;
}
public enum OperationKey {
EQUALS, CONTAINS, STARTS_WITH, ENDS_WITH, EXPRESSION, BETWEEN, FUZZY, IN, WITHIN, BBOX, NEAR;
}
public static class CriteriaEntry {
public static class CriteriaEntry {
private OperationKey key;
private Object value;
private OperationKey key;
private Object value;
CriteriaEntry(OperationKey key, Object value) {
this.key = key;
this.value = value;
}
CriteriaEntry(OperationKey key, Object value) {
this.key = key;
this.value = value;
}
public OperationKey getKey() {
return key;
}
public OperationKey getKey() {
return key;
}
public Object getValue() {
return value;
}
public Object getValue() {
return value;
}
}
}
}