Skip to content

Fix geo normalization for ranges and points #1998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ private void parseObjectLatLon(ParseContext context) throws IOException {
}

private void parseLatLon(ParseContext context, double lat, double lon) throws IOException {
if (normalizeLon) {
lon = GeoUtils.normalizeLon(lon);
}
if (normalizeLat) {
lat = GeoUtils.normalizeLat(lat);
if (normalizeLat || normalizeLon) {
Point pt = new Point(lat, lon);
GeoUtils.normalizePoint(pt, normalizeLat, normalizeLon);
lat = pt.lat;
lon = pt.lon;
}

if (validateLat) {
Expand Down Expand Up @@ -409,11 +409,11 @@ private void parseGeohash(ParseContext context, String geohash) throws IOExcepti
double lat = values[0];
double lon = values[1];

if (normalizeLon) {
lon = GeoUtils.normalizeLon(lon);
}
if (normalizeLat) {
lat = GeoUtils.normalizeLat(lat);
if (normalizeLat || normalizeLon) {
Point pt = new Point(lat, lon);
GeoUtils.normalizePoint(pt, normalizeLat, normalizeLon);
lat = pt.lat;
lon = pt.lon;
}

if (validateLat) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.lucene.search.Filter;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.lucene.search.XBooleanFilter;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
import org.elasticsearch.index.mapper.FieldMapper;
Expand All @@ -29,6 +30,8 @@
import org.elasticsearch.index.search.geo.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.index.query.support.QueryParsers.wrapSmartNameFilter;

Expand Down Expand Up @@ -61,8 +64,7 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
String filterName = null;
String currentFieldName = null;
XContentParser.Token token;
boolean normalizeLon = true;
boolean normalizeLat = true;
boolean normalize = true;

String type = "memory";

Expand Down Expand Up @@ -151,23 +153,21 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
} else if ("_cache_key".equals(currentFieldName) || "_cacheKey".equals(currentFieldName)) {
cacheKey = new CacheKeyFilter.Key(parser.text());
} else if ("normalize".equals(currentFieldName)) {
normalizeLat = parser.booleanValue();
normalizeLon = parser.booleanValue();
normalize = parser.booleanValue();
} else if ("type".equals(currentFieldName)) {
type = parser.text();
} else {
throw new QueryParsingException(parseContext.index(), "[qeo_bbox] filter does not support [" + currentFieldName + "]");
throw new QueryParsingException(parseContext.index(), "[geo_bbox] filter does not support [" + currentFieldName + "]");
}
}
}

if (normalizeLat) {
topLeft.lat = GeoUtils.normalizeLat(topLeft.lat);
bottomRight.lat = GeoUtils.normalizeLat(bottomRight.lat);
}
if (normalizeLon) {
topLeft.lon = GeoUtils.normalizeLon(topLeft.lon);
bottomRight.lon = GeoUtils.normalizeLon(bottomRight.lon);
List<Range> ranges = null;
if (normalize) {
ranges = GeoUtils.normalizeRange(topLeft, bottomRight);
} else {
ranges = new ArrayList<Range>(1);
ranges.add(new Range(topLeft, bottomRight));
}

MapperService.SmartNameFieldMappers smartMappers = parseContext.smartFieldMappers(fieldName);
Expand All @@ -182,15 +182,20 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar

fieldName = mapper.names().indexName();

Filter filter;
if ("indexed".equals(type)) {
filter = IndexedGeoBoundingBoxFilter.create(topLeft, bottomRight, geoMapper);
} else if ("memory".equals(type)) {
filter = new InMemoryGeoBoundingBoxFilter(topLeft, bottomRight, fieldName, parseContext.indexCache().fieldData());
} else {
throw new QueryParsingException(parseContext.index(), "geo bounding box type [" + type + "] not supported, either 'indexed' or 'memory' are allowed");
XBooleanFilter boolFilter = new XBooleanFilter();
for (Range r : ranges) {
Filter f;
if ("indexed".equals(type)) {
f = IndexedGeoBoundingBoxFilter.create(topLeft, bottomRight, geoMapper);
} else if ("memory".equals(type)) {
f = new InMemoryGeoBoundingBoxFilter(topLeft, bottomRight, fieldName, parseContext.indexCache().fieldData());
} else {
throw new QueryParsingException(parseContext.index(), "geo bounding box type [" + type + "] not supported, either 'indexed' or 'memory' are allowed");
}
boolFilter.addShould(f);
}

Filter filter = boolFilter;
if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,11 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
}
distance = geoDistance.normalize(distance, DistanceUnit.MILES);

if (normalizeLat) {
lat = GeoUtils.normalizeLat(lat);
}
if (normalizeLon) {
lon = GeoUtils.normalizeLon(lon);
if (normalizeLat || normalizeLon) {
Point pt = new Point(lat, lon);
GeoUtils.normalizePoint(pt, normalizeLat, normalizeLon);
lat = pt.lat;
lon = pt.lon;
}

MapperService.SmartNameFieldMappers smartMappers = parseContext.smartFieldMappers(fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
to = geoDistance.normalize(to, DistanceUnit.MILES);
}

if (normalizeLat) {
lat = GeoUtils.normalizeLat(lat);
}
if (normalizeLon) {
lon = GeoUtils.normalizeLon(lon);
if (normalizeLat || normalizeLon) {
Point pt = new Point(lat, lon);
GeoUtils.normalizePoint(pt, normalizeLat, normalizeLon);
lat = pt.lat;
lon = pt.lon;
}

MapperService.SmartNameFieldMappers smartMappers = parseContext.smartFieldMappers(fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,9 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
throw new QueryParsingException(parseContext.index(), "no points defined for geo_polygon filter");
}

for (Point point : points) {
if (normalizeLat) {
point.lat = GeoUtils.normalizeLat(point.lat);
}
if (normalizeLon) {
point.lon = GeoUtils.normalizeLon(point.lon);
if (normalizeLat || normalizeLon) {
for (Point point : points) {
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
}
}

Expand Down
Loading