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,6 +33,7 @@ public class GeoPoint {
public static final String LATITUDE = GeoPointFieldMapper.Names.LAT; public static final String LATITUDE = GeoPointFieldMapper.Names.LAT;
public static final String LONGITUDE = GeoPointFieldMapper.Names.LON; public static final String LONGITUDE = GeoPointFieldMapper.Names.LON;
public static final String GEOHASH = GeoPointFieldMapper.Names.GEOHASH;
private double lat; private double lat;
private double lon; private double lon;
@ -181,8 +182,14 @@ public class GeoPoint {
} else { } else {
throw new ElasticSearchParseException("latitude must be a number"); 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 { } else {
throw new ElasticSearchParseException("field must be either '"+LATITUDE+"' or '"+LONGITUDE+"'"); throw new ElasticSearchParseException("field must be either '" + LATITUDE + "', '" + LONGITUDE + "' or '" + GEOHASH + "'");
} }
} else { } else {
throw new ElasticSearchParseException("Token '"+parser.currentToken()+"' not allowed"); throw new ElasticSearchParseException("Token '"+parser.currentToken()+"' not allowed");
@ -211,7 +218,7 @@ public class GeoPoint {
int comma = data.indexOf(','); int comma = data.indexOf(',');
if(comma > 0) { if(comma > 0) {
double lat = Double.parseDouble(data.substring(0, comma).trim()); 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); return point.reset(lat, lon);
} else { } else {
point.resetFromGeoHash(data); point.resetFromGeoHash(data);

View File

@ -19,8 +19,9 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.elasticsearch.ElasticSearchParseException;
import org.apache.lucene.search.Filter; import org.apache.lucene.search.Filter;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.inject.Inject; 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 class GeoBoundingBoxFilterParser implements FilterParser {
public static final String NAME = "geo_bbox"; 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 @Inject
public GeoBoundingBoxFilterParser() { public GeoBoundingBoxFilterParser() {
@ -79,62 +85,18 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) { token = parser.nextToken();
GeoPoint point = null; if (FIELD.equals(currentFieldName)) {
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)) {
fieldName = parser.text(); 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 { } else {
GeoPoint point = null; throw new ElasticSearchParseException("Unexpected field [" + currentFieldName + "]");
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);
}
} }
} else {
throw new ElasticSearchParseException("fieldname expected but [" + token + "] found");
} }
} }
} else if (token.isValue()) { } else if (token.isValue()) {

View File

@ -82,14 +82,8 @@ public class GeoDistanceFilterParser implements FilterParser {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) { } 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; fieldName = currentFieldName;
GeoPoint.parse(parser, point);
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
// the json in the format of -> field : { lat : 30, lon : 12 } // the json in the format of -> field : { lat : 30, lon : 12 }
String currentName = parser.currentName(); String currentName = parser.currentName();

View File

@ -84,32 +84,12 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
token = parser.nextToken(); GeoPoint.parse(parser, point);
double lon = parser.doubleValue();
token = parser.nextToken();
double lat = parser.doubleValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
point.reset(lat, lon);
fieldName = currentFieldName; fieldName = currentFieldName;
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
// the json in the format of -> field : { lat : 30, lon : 12 } // the json in the format of -> field : { lat : 30, lon : 12 }
String currentName = parser.currentName();
fieldName = currentFieldName; fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { GeoPoint.parse(parser, point);
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);
}
}
}
} else if (token.isValue()) { } else if (token.isValue()) {
if (currentFieldName.equals("from")) { if (currentFieldName.equals("from")) {
if (token == XContentParser.Token.VALUE_NULL) { if (token == XContentParser.Token.VALUE_NULL) {

View File

@ -91,36 +91,7 @@ public class GeoPolygonFilterParser implements FilterParser {
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
if ("points".equals(currentFieldName)) { if ("points".equals(currentFieldName)) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
if (token == XContentParser.Token.FIELD_NAME) { points.add(GeoPoint.parse(parser));
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()));
}
} }
} else { } else {
throw new QueryParsingException(parseContext.index(), "[geo_polygon] filter does not support [" + currentFieldName + "]"); 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)); entries.add(new GeoDistanceFacet.Entry(from, to, 0, 0, 0, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
} }
} else { } else {
token = parser.nextToken(); GeoPoint.parse(parser, point);
point.resetLon(parser.doubleValue());
token = parser.nextToken();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
fieldName = currentName; fieldName = currentName;
} }
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
@ -127,19 +121,7 @@ public class GeoDistanceFacetParser extends AbstractComponent implements FacetPa
} else { } else {
// the json in the format of -> field : { lat : 30, lon : 12 } // the json in the format of -> field : { lat : 30, lon : 12 }
fieldName = currentName; fieldName = currentName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { GeoPoint.parse(parser, point);
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);
}
}
}
} }
} else if (token.isValue()) { } else if (token.isValue()) {
if (currentName.equals("unit")) { if (currentName.equals("unit")) {

View File

@ -70,13 +70,7 @@ public class GeoDistanceSortParser implements SortParser {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentName = parser.currentName(); currentName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {
token = parser.nextToken(); GeoPoint.parse(parser, point);
point.resetLon(parser.doubleValue());
token = parser.nextToken();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
fieldName = currentName; fieldName = currentName;
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
// the json in the format of -> field : { lat : 30, lon : 12 } // 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); nestedFilter = context.queryParserService().parseInnerFilter(parser);
} else { } else {
fieldName = currentName; fieldName = currentName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { GeoPoint.parse(parser, point);
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);
}
}
}
} }
} else if (token.isValue()) { } else if (token.isValue()) {
if ("reverse".equals(currentName)) { if ("reverse".equals(currentName)) {