diff --git a/src/main/java/org/elasticsearch/common/geo/GeoPoint.java b/src/main/java/org/elasticsearch/common/geo/GeoPoint.java index a95c0c71081..35a1fe14d96 100644 --- a/src/main/java/org/elasticsearch/common/geo/GeoPoint.java +++ b/src/main/java/org/elasticsearch/common/geo/GeoPoint.java @@ -33,7 +33,8 @@ public class GeoPoint { public static final String LATITUDE = GeoPointFieldMapper.Names.LAT; public static final String LONGITUDE = GeoPointFieldMapper.Names.LON; - + public static final String GEOHASH = GeoPointFieldMapper.Names.GEOHASH; + private double lat; private double lon; @@ -133,7 +134,7 @@ public class GeoPoint { public String toString() { return "[" + lat + ", " + lon + "]"; } - + /** * Parse a {@link GeoPoint} with a {@link XContentParser}: * @@ -181,8 +182,14 @@ public class GeoPoint { } else { throw new ElasticSearchParseException("latitude must be a number"); } + } else if (GEOHASH.equals(field)) { + if(parser.nextToken() == Token.VALUE_STRING) { + point.resetFromGeoHash(parser.text()); + } else { + throw new ElasticSearchParseException("geohash must be a string"); + } } else { - throw new ElasticSearchParseException("field must be either '"+LATITUDE+"' or '"+LONGITUDE+"'"); + throw new ElasticSearchParseException("field must be either '" + LATITUDE + "', '" + LONGITUDE + "' or '" + GEOHASH + "'"); } } else { throw new ElasticSearchParseException("Token '"+parser.currentToken()+"' not allowed"); @@ -211,7 +218,7 @@ public class GeoPoint { int comma = data.indexOf(','); if(comma > 0) { double lat = Double.parseDouble(data.substring(0, comma).trim()); - double lon = Double.parseDouble(data.substring(comma+1).trim()); + double lon = Double.parseDouble(data.substring(comma + 1).trim()); return point.reset(lat, lon); } else { point.resetFromGeoHash(data); diff --git a/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java index b540e67fa1f..5b041f9667c 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoBoundingBoxFilterParser.java @@ -19,8 +19,9 @@ package org.elasticsearch.index.query; +import org.elasticsearch.ElasticSearchParseException; + import org.apache.lucene.search.Filter; -import org.elasticsearch.common.geo.GeoHashUtils; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.inject.Inject; @@ -43,6 +44,11 @@ import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFi public class GeoBoundingBoxFilterParser implements FilterParser { public static final String NAME = "geo_bbox"; + public static final String TOP_LEFT = "top_left"; + public static final String TOPLEFT = "topLeft"; + public static final String BOTTOM_RIGHT = "bottom_right"; + public static final String BOTTOMRIGHT = "bottomRight"; + public static final String FIELD = "field"; @Inject public GeoBoundingBoxFilterParser() { @@ -79,62 +85,18 @@ public class GeoBoundingBoxFilterParser implements FilterParser { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (token == XContentParser.Token.START_ARRAY) { - GeoPoint point = null; - if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) { - point = topLeft; - } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) { - point = bottomRight; - } - - if (point != null) { - token = parser.nextToken(); - point.resetLon(parser.doubleValue()); - token = parser.nextToken(); - point.resetLat(parser.doubleValue()); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } - } - } else if (token == XContentParser.Token.START_OBJECT) { - GeoPoint point = null; - if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) { - point = topLeft; - } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) { - point = bottomRight; - } - - if (point != null) { - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token.isValue()) { - if (currentFieldName.equals(GeoPointFieldMapper.Names.LAT)) { - point.resetLat(parser.doubleValue()); - } else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) { - point.resetLon(parser.doubleValue()); - } else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) { - GeoHashUtils.decode(parser.text(), point); - } - } - } - } - } else if (token.isValue()) { - if ("field".equals(currentFieldName)) { + token = parser.nextToken(); + if (FIELD.equals(currentFieldName)) { fieldName = parser.text(); + } else if (TOP_LEFT.equals(currentFieldName) || TOPLEFT.equals(currentFieldName)) { + GeoPoint.parse(parser, topLeft); + } else if ( BOTTOM_RIGHT.equals(currentFieldName) || BOTTOMRIGHT.equals(currentFieldName)) { + GeoPoint.parse(parser, bottomRight); } else { - GeoPoint point = null; - if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) { - point = topLeft; - } else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) { - point = bottomRight; - } - - if (point != null) { - String value = parser.text(); - point.resetFromString(value); - } + throw new ElasticSearchParseException("Unexpected field [" + currentFieldName + "]"); } + } else { + throw new ElasticSearchParseException("fieldname expected but [" + token + "] found"); } } } else if (token.isValue()) { diff --git a/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java index 9b6128b81db..d546b8bf1eb 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoDistanceFilterParser.java @@ -82,14 +82,8 @@ public class GeoDistanceFilterParser implements FilterParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - token = parser.nextToken(); - point.resetLon(parser.doubleValue()); - token = parser.nextToken(); - point.resetLat(parser.doubleValue()); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } fieldName = currentFieldName; + GeoPoint.parse(parser, point); } else if (token == XContentParser.Token.START_OBJECT) { // the json in the format of -> field : { lat : 30, lon : 12 } String currentName = parser.currentName(); diff --git a/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java index 36f30598f6a..02c094bebf2 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoDistanceRangeFilterParser.java @@ -84,32 +84,12 @@ public class GeoDistanceRangeFilterParser implements FilterParser { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - token = parser.nextToken(); - double lon = parser.doubleValue(); - token = parser.nextToken(); - double lat = parser.doubleValue(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } - point.reset(lat, lon); + GeoPoint.parse(parser, point); fieldName = currentFieldName; } else if (token == XContentParser.Token.START_OBJECT) { // the json in the format of -> field : { lat : 30, lon : 12 } - String currentName = parser.currentName(); fieldName = currentFieldName; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentName = parser.currentName(); - } else if (token.isValue()) { - if (currentName.equals(GeoPointFieldMapper.Names.LAT)) { - point.resetLat(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.LON)) { - point.resetLon(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) { - GeoHashUtils.decode(parser.text(), point); - } - } - } + GeoPoint.parse(parser, point); } else if (token.isValue()) { if (currentFieldName.equals("from")) { if (token == XContentParser.Token.VALUE_NULL) { diff --git a/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java b/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java index a7babddd1ee..3f337d061a1 100644 --- a/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java +++ b/src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java @@ -91,36 +91,7 @@ public class GeoPolygonFilterParser implements FilterParser { } else if (token == XContentParser.Token.START_ARRAY) { if ("points".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token == XContentParser.Token.START_ARRAY) { - token = parser.nextToken(); - double lon = parser.doubleValue(); - token = parser.nextToken(); - double lat = parser.doubleValue(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } - points.add(new GeoPoint(lat, lon)); - } else if (token == XContentParser.Token.START_OBJECT) { - GeoPoint point = new GeoPoint(); - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token.isValue()) { - if (currentFieldName.equals(GeoPointFieldMapper.Names.LAT)) { - point.resetLat(parser.doubleValue()); - } else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) { - point.resetLon(parser.doubleValue()); - } else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) { - GeoHashUtils.decode(parser.text(), point); - } - } - } - points.add(point); - } else if (token.isValue()) { - points.add(new GeoPoint().resetFromString(parser.text())); - } + points.add(GeoPoint.parse(parser)); } } else { throw new QueryParsingException(parseContext.index(), "[geo_polygon] filter does not support [" + currentFieldName + "]"); diff --git a/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetParser.java b/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetParser.java index d391f155f36..6678888e9df 100644 --- a/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetParser.java +++ b/src/main/java/org/elasticsearch/search/facet/geodistance/GeoDistanceFacetParser.java @@ -112,13 +112,7 @@ public class GeoDistanceFacetParser extends AbstractComponent implements FacetPa entries.add(new GeoDistanceFacet.Entry(from, to, 0, 0, 0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)); } } else { - token = parser.nextToken(); - point.resetLon(parser.doubleValue()); - token = parser.nextToken(); - point.resetLat(parser.doubleValue()); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } + GeoPoint.parse(parser, point); fieldName = currentName; } } else if (token == XContentParser.Token.START_OBJECT) { @@ -127,19 +121,7 @@ public class GeoDistanceFacetParser extends AbstractComponent implements FacetPa } else { // the json in the format of -> field : { lat : 30, lon : 12 } fieldName = currentName; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentName = parser.currentName(); - } else if (token.isValue()) { - if (currentName.equals(GeoPointFieldMapper.Names.LAT)) { - point.resetLat(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.LON)) { - point.resetLon(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) { - GeoHashUtils.decode(parser.text(), point); - } - } - } + GeoPoint.parse(parser, point); } } else if (token.isValue()) { if (currentName.equals("unit")) { diff --git a/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java b/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java index 4f6073cbad5..01dfa2ed94a 100644 --- a/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java +++ b/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java @@ -70,13 +70,7 @@ public class GeoDistanceSortParser implements SortParser { if (token == XContentParser.Token.FIELD_NAME) { currentName = parser.currentName(); } else if (token == XContentParser.Token.START_ARRAY) { - token = parser.nextToken(); - point.resetLon(parser.doubleValue()); - token = parser.nextToken(); - point.resetLat(parser.doubleValue()); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - - } + GeoPoint.parse(parser, point); fieldName = currentName; } else if (token == XContentParser.Token.START_OBJECT) { // the json in the format of -> field : { lat : 30, lon : 12 } @@ -84,19 +78,7 @@ public class GeoDistanceSortParser implements SortParser { nestedFilter = context.queryParserService().parseInnerFilter(parser); } else { fieldName = currentName; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentName = parser.currentName(); - } else if (token.isValue()) { - if (currentName.equals(GeoPointFieldMapper.Names.LAT)) { - point.resetLat(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.LON)) { - point.resetLon(parser.doubleValue()); - } else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) { - GeoHashUtils.decode(parser.text(), point); - } - } - } + GeoPoint.parse(parser, point); } } else if (token.isValue()) { if ("reverse".equals(currentName)) {