@Override public String[] names() {
return new String[]{NAME, "geoBbox", "geo_bounding_box", "geoBoundingBox"};
}
@Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
XContentParser parser = parseContext.parser();
boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String fieldName = null;
GeoBoundingBoxFilter.Point topLeft = new GeoBoundingBoxFilter.Point();
GeoBoundingBoxFilter.Point bottomRight = new GeoBoundingBoxFilter.Point();
String filterName = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
GeoBoundingBoxFilter.Point 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.lon = parser.doubleValue();
token = parser.nextToken();
point.lat = parser.doubleValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
}
} else if (token == XContentParser.Token.START_OBJECT) {
GeoBoundingBoxFilter.Point 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.lat = parser.doubleValue();
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) {
point.lon = parser.doubleValue();
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
point.lat = values[0];
point.lon = values[1];
}
}
}
}
} else if (token.isValue()) {
if ("field".equals(currentFieldName)) {
fieldName = parser.text();
} else {
GeoBoundingBoxFilter.Point 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();
int comma = value.indexOf(',');
if (comma != -1) {
point.lat = Double.parseDouble(value.substring(0, comma).trim());
point.lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
double[] values = GeoHashUtils.decode(value);
point.lat = values[0];
point.lon = values[1];
}
}
}
}
}
} else if (token.isValue()) {
if ("_name".equals(currentFieldName)) {
filterName = parser.text();
} else if ("_cache".equals(currentFieldName)) {
cache = parser.booleanValue();
} else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
cacheKey = new CacheKeyFilter.Key(parser.text());
}
}
}
MapperService mapperService = parseContext.mapperService();