Changed GeoPoint parsing in serveral parsers using Geopoint.parse() Closes #3351

This commit is contained in:
Florian Schilling 2013-07-11 13:18:04 +02:00
parent 45a756c203
commit 1e5e8d83b1
7 changed files with 35 additions and 157 deletions

View File

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

View File

@ -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()) {

View File

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

View File

@ -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) {

View File

@ -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 + "]");

View File

@ -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")) {

View File

@ -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)) {