refactor geo to extract common classes

This commit is contained in:
Shay Banon 2013-01-23 14:13:12 +01:00
parent 9c729fad2c
commit a74e7f8099
42 changed files with 358 additions and 492 deletions

View File

@ -17,7 +17,7 @@
* under the License.
*/
package org.elasticsearch.index.search.geo;
package org.elasticsearch.common.geo;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.unit.DistanceUnit;
@ -136,8 +136,8 @@ public enum GeoDistance {
maxLon = MAX_LON;
}
Point topLeft = new Point(Math.toDegrees(maxLat), Math.toDegrees(minLon));
Point bottomRight = new Point(Math.toDegrees(minLat), Math.toDegrees(maxLon));
GeoPoint topLeft = new GeoPoint(Math.toDegrees(maxLat), Math.toDegrees(minLon));
GeoPoint bottomRight = new GeoPoint(Math.toDegrees(minLat), Math.toDegrees(maxLon));
if (minLon > maxLon) {
return new Meridian180DistanceBoundingCheck(topLeft, bottomRight);
}
@ -164,9 +164,9 @@ public enum GeoDistance {
boolean isWithin(double targetLatitude, double targetLongitude);
Point topLeft();
GeoPoint topLeft();
Point bottomRight();
GeoPoint bottomRight();
}
public static AlwaysDistanceBoundingCheck ALWAYS_INSTANCE = new AlwaysDistanceBoundingCheck();
@ -178,65 +178,65 @@ public enum GeoDistance {
}
@Override
public Point topLeft() {
public GeoPoint topLeft() {
return null;
}
@Override
public Point bottomRight() {
public GeoPoint bottomRight() {
return null;
}
}
public static class Meridian180DistanceBoundingCheck implements DistanceBoundingCheck {
private final Point topLeft;
private final Point bottomRight;
private final GeoPoint topLeft;
private final GeoPoint bottomRight;
public Meridian180DistanceBoundingCheck(Point topLeft, Point bottomRight) {
public Meridian180DistanceBoundingCheck(GeoPoint topLeft, GeoPoint bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
@Override
public boolean isWithin(double targetLatitude, double targetLongitude) {
return (targetLatitude >= bottomRight.lat && targetLatitude <= topLeft.lat) &&
(targetLongitude >= topLeft.lon || targetLongitude <= bottomRight.lon);
return (targetLatitude >= bottomRight.lat() && targetLatitude <= topLeft.lat()) &&
(targetLongitude >= topLeft.lon() || targetLongitude <= bottomRight.lon());
}
@Override
public Point topLeft() {
public GeoPoint topLeft() {
return topLeft;
}
@Override
public Point bottomRight() {
public GeoPoint bottomRight() {
return bottomRight;
}
}
public static class SimpleDistanceBoundingCheck implements DistanceBoundingCheck {
private final Point topLeft;
private final Point bottomRight;
private final GeoPoint topLeft;
private final GeoPoint bottomRight;
public SimpleDistanceBoundingCheck(Point topLeft, Point bottomRight) {
public SimpleDistanceBoundingCheck(GeoPoint topLeft, GeoPoint bottomRight) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
}
@Override
public boolean isWithin(double targetLatitude, double targetLongitude) {
return (targetLatitude >= bottomRight.lat && targetLatitude <= topLeft.lat) &&
(targetLongitude >= topLeft.lon && targetLongitude <= bottomRight.lon);
return (targetLatitude >= bottomRight.lat() && targetLatitude <= topLeft.lat()) &&
(targetLongitude >= topLeft.lon() && targetLongitude <= bottomRight.lon());
}
@Override
public Point topLeft() {
public GeoPoint topLeft() {
return topLeft;
}
@Override
public Point bottomRight() {
public GeoPoint bottomRight() {
return bottomRight;
}
}

View File

@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.elasticsearch.index.search.geo;
package org.elasticsearch.common.geo;
import gnu.trove.map.hash.TIntIntHashMap;
@ -112,10 +112,10 @@ public class GeoHashUtils {
return geohash.toString();
}
public static double[] decode(String geohash) {
double[] ret = new double[2];
decode(geohash, ret);
return ret;
public static GeoPoint decode(String geohash) {
GeoPoint point = new GeoPoint();
decode(geohash, point);
return point;
}
/**
@ -124,7 +124,7 @@ public class GeoHashUtils {
* @param geohash Geohash to deocde
* @return Array with the latitude at index 0, and longitude at index 1
*/
public static void decode(String geohash, double[] ret) {
public static void decode(String geohash, GeoPoint ret) {
// double[] latInterval = {-90.0, 90.0};
// double[] lngInterval = {-180.0, 180.0};
double latInterval0 = -90.0;
@ -160,10 +160,9 @@ public class GeoHashUtils {
}
// latitude = (latInterval[0] + latInterval[1]) / 2D;
ret[0] = (latInterval0 + latInterval1) / 2D;
// longitude = (lngInterval[0] + lngInterval[1]) / 2D;
ret[1] = (lngInterval0 + lngInterval1) / 2D;
ret.reset((latInterval0 + latInterval1) / 2D, (lngInterval0 + lngInterval1) / 2D);
// return ret;
}
}

View File

@ -17,9 +17,7 @@
* under the License.
*/
package org.elasticsearch.index.mapper.geo;
import org.elasticsearch.index.search.geo.GeoHashUtils;
package org.elasticsearch.common.geo;
/**
*
@ -43,6 +41,32 @@ public class GeoPoint {
return this;
}
public GeoPoint resetLat(double lat) {
this.lat = lat;
return this;
}
public GeoPoint resetLon(double lon) {
this.lon = lon;
return this;
}
public GeoPoint resetFromString(String value) {
int comma = value.indexOf(',');
if (comma != -1) {
lat = Double.parseDouble(value.substring(0, comma).trim());
lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
resetFromGeoHash(value);
}
return this;
}
public GeoPoint resetFromGeoHash(String hash) {
GeoHashUtils.decode(hash, this);
return this;
}
void latlon(double lat, double lon) {
this.lat = lat;
this.lon = lon;
@ -95,4 +119,8 @@ public class GeoPoint {
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public String toString() {
return "[" + lat + ", " + lon + "]";
}
}

View File

@ -17,7 +17,7 @@
* under the License.
*/
package org.elasticsearch.index.search.geo;
package org.elasticsearch.common.geo;
/**
*/
@ -27,7 +27,6 @@ public class GeoUtils {
* Normalize longitude to lie within the -180 (exclusive) to 180 (inclusive) range.
*
* @param lon Longitude to normalize
* @see #normalizePoint(Point)
* @return The normalized longitude.
*/
public static double normalizeLon(double lon) {
@ -36,16 +35,16 @@ public class GeoUtils {
/**
* Normalize latitude to lie within the -90 to 90 (both inclusive) range.
* <p>
* <p/>
* Note: You should not normalize longitude and latitude separately,
* because when normalizing latitude it may be necessary to
* add a shift of 180&deg; in the longitude.
* For this purpose, you should call the
* {@link #normalizePoint(Point)} function.
* because when normalizing latitude it may be necessary to
* add a shift of 180&deg; in the longitude.
* For this purpose, you should call the
* {@link #normalizePoint(GeoPoint)} function.
*
* @param lat Latitude to normalize
* @see #normalizePoint(Point)
* @return The normalized latitude.
* @see #normalizePoint(GeoPoint)
*/
public static double normalizeLat(double lat) {
lat = centeredModulus(lat, 360);
@ -60,59 +59,62 @@ public class GeoUtils {
/**
* Normalize the geo {@code Point} for its coordinates to lie within their
* respective normalized ranges.
* <p>
* <p/>
* Note: A shift of 180&deg; is applied in the longitude if necessary,
* in order to normalize properly the latitude.
* in order to normalize properly the latitude.
*
* @param point The point to normalize in-place.
*/
public static void normalizePoint(Point point) {
public static void normalizePoint(GeoPoint point) {
normalizePoint(point, true, true);
}
/**
* Normalize the geo {@code Point} for the given coordinates to lie within
* their respective normalized ranges.
*
* <p/>
* You can control which coordinate gets normalized with the two flags.
* <p>
* <p/>
* Note: A shift of 180&deg; is applied in the longitude if necessary,
* in order to normalize properly the latitude.
* If normalizing latitude but not longitude, it is assumed that
* the longitude is in the form x+k*360, with x in ]-180;180],
* and k is meaningful to the application.
* Therefore x will be adjusted while keeping k preserved.
* in order to normalize properly the latitude.
* If normalizing latitude but not longitude, it is assumed that
* the longitude is in the form x+k*360, with x in ]-180;180],
* and k is meaningful to the application.
* Therefore x will be adjusted while keeping k preserved.
*
* @param point The point to normalize in-place.
* @param point The point to normalize in-place.
* @param normLat Whether to normalize latitude or leave it as is.
* @param normLon Whether to normalize longitude.
*/
public static void normalizePoint(Point point, boolean normLat, boolean normLon) {
public static void normalizePoint(GeoPoint point, boolean normLat, boolean normLon) {
double lat = point.lat();
double lon = point.lon();
if (normLat) {
point.lat = centeredModulus(point.lat, 360);
lat = centeredModulus(lat, 360);
boolean shift = true;
if (point.lat < -90) {
point.lat = -180 - point.lat;
} else if (point.lat > 90) {
point.lat = 180 - point.lat;
if (lat < -90) {
lat = -180 - lat;
} else if (lat > 90) {
lat = 180 - lat;
} else {
// No need to shift the longitude, and the latitude is normalized
shift = false;
}
if (shift) {
if (normLon) {
point.lon += 180;
lon += 180;
} else {
// Longitude won't be normalized,
// keep it in the form x+k*360 (with x in ]-180;180])
// by only changing x, assuming k is meaningful for the user application.
point.lon += normalizeLon(point.lon) > 0 ? -180 : 180;
lon += normalizeLon(lon) > 0 ? -180 : 180;
}
}
}
if (normLon) {
point.lon = centeredModulus(point.lon, 360);
lon = centeredModulus(lon, 360);
}
point.reset(lat, lon);
}
private static double centeredModulus(double dividend, double divisor) {

View File

@ -20,8 +20,8 @@
package org.elasticsearch.index.fielddata;
import org.elasticsearch.ElasticSearchIllegalStateException;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.util.GeoPointArrayRef;
import org.elasticsearch.index.mapper.geo.GeoPoint;
/**
*/

View File

@ -20,10 +20,10 @@
package org.elasticsearch.index.fielddata;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.util.*;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.joda.time.MutableDateTime;
/**

View File

@ -21,11 +21,11 @@ package org.elasticsearch.index.fielddata.fieldcomparator;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.FieldComparator;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.index.search.geo.GeoDistance;
import java.io.IOException;

View File

@ -21,10 +21,10 @@ package org.elasticsearch.index.fielddata.fieldcomparator;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.SortField;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.search.geo.GeoDistance;
import java.io.IOException;

View File

@ -21,13 +21,13 @@ package org.elasticsearch.index.fielddata.plain;
import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.common.RamUsage;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.index.fielddata.*;
import org.elasticsearch.index.fielddata.ordinals.Ordinals;
import org.elasticsearch.index.fielddata.util.GeoPointArrayRef;
import org.elasticsearch.index.fielddata.util.IntArrayRef;
import org.elasticsearch.index.fielddata.util.StringArrayRef;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.index.search.geo.GeoHashUtils;
/**
*/

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.fielddata.util;
import org.apache.lucene.util.ArrayUtil;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoPoint;
import java.util.AbstractList;
import java.util.RandomAccess;

View File

@ -23,6 +23,9 @@ import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.FieldInfo.IndexOptions;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@ -34,9 +37,6 @@ import org.elasticsearch.index.mapper.core.DoubleFieldMapper;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
import org.elasticsearch.index.mapper.core.StringFieldMapper;
import org.elasticsearch.index.mapper.object.ArrayValueMapperParser;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.index.search.geo.GeoUtils;
import org.elasticsearch.index.search.geo.Point;
import java.io.IOException;
import java.util.Map;
@ -387,10 +387,10 @@ public class GeoPointFieldMapper implements Mapper, ArrayValueMapperParser {
private void parseLatLon(ParseContext context, double lat, double lon) throws IOException {
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoPoint point = new GeoPoint(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
lat = point.lat();
lon = point.lon();
}
if (validateLat) {
@ -419,38 +419,33 @@ public class GeoPointFieldMapper implements Mapper, ArrayValueMapperParser {
}
private void parseGeohash(ParseContext context, String geohash) throws IOException {
double[] values = GeoHashUtils.decode(geohash);
double lat = values[0];
double lon = values[1];
GeoPoint point = GeoHashUtils.decode(geohash);
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
}
if (validateLat) {
if (lat > 90.0 || lat < -90.0) {
throw new ElasticSearchIllegalArgumentException("illegal latitude value [" + lat + "] for " + name);
if (point.lat() > 90.0 || point.lat() < -90.0) {
throw new ElasticSearchIllegalArgumentException("illegal latitude value [" + point.lat() + "] for " + name);
}
}
if (validateLon) {
if (lon > 180.0 || lon < -180) {
throw new ElasticSearchIllegalArgumentException("illegal longitude value [" + lon + "] for " + name);
if (point.lon() > 180.0 || point.lon() < -180) {
throw new ElasticSearchIllegalArgumentException("illegal longitude value [" + point.lon() + "] for " + name);
}
}
context.externalValue(Double.toString(lat) + ',' + Double.toString(lon));
context.externalValue(Double.toString(point.lat()) + ',' + Double.toString(point.lat()));
geoStringMapper.parse(context);
if (enableGeoHash) {
context.externalValue(geohash);
geohashMapper.parse(context);
}
if (enableLatLon) {
context.externalValue(lat);
context.externalValue(point.lat());
latMapper.parse(context);
context.externalValue(lon);
context.externalValue(point.lon());
lonMapper.parse(context);
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.query;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.search.geo.Point;
import java.io.IOException;
@ -31,11 +31,11 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
private final String name;
private Point topLeft;
private GeoPoint topLeft;
private String topLeftGeohash;
private Point bottomRight;
private GeoPoint bottomRight;
private String bottomRightGeohash;
@ -57,9 +57,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
* @param lon The longitude
*/
public GeoBoundingBoxFilterBuilder topLeft(double lat, double lon) {
topLeft = new Point();
topLeft.lat = lat;
topLeft.lon = lon;
topLeft = new GeoPoint(lat, lon);
return this;
}
@ -70,9 +68,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
* @param lon The longitude
*/
public GeoBoundingBoxFilterBuilder bottomRight(double lat, double lon) {
bottomRight = new Point();
bottomRight.lat = lat;
bottomRight.lon = lon;
bottomRight = new GeoPoint(lat, lon);
return this;
}
@ -124,7 +120,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
if (topLeftGeohash != null) {
builder.field("top_left", topLeftGeohash);
} else if (topLeft != null) {
builder.startArray("top_left").value(topLeft.lon).value(topLeft.lat).endArray();
builder.startArray("top_left").value(topLeft.lon()).value(topLeft.lat()).endArray();
} else {
throw new QueryBuilderException("geo_bounding_box requires 'top_left' to be set");
}
@ -132,7 +128,7 @@ public class GeoBoundingBoxFilterBuilder extends BaseFilterBuilder {
if (bottomRightGeohash != null) {
builder.field("bottom_right", bottomRightGeohash);
} else if (bottomRight != null) {
builder.startArray("bottom_right").value(bottomRight.lon).value(bottomRight.lat).endArray();
builder.startArray("bottom_right").value(bottomRight.lon()).value(bottomRight.lat()).endArray();
} else {
throw new QueryBuilderException("geo_bounding_box requires 'bottom_right' to be set");
}

View File

@ -20,6 +20,9 @@
package org.elasticsearch.index.query;
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;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
@ -27,7 +30,8 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.*;
import org.elasticsearch.index.search.geo.InMemoryGeoBoundingBoxFilter;
import org.elasticsearch.index.search.geo.IndexedGeoBoundingBoxFilter;
import java.io.IOException;
@ -56,8 +60,8 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String fieldName = null;
Point topLeft = new Point();
Point bottomRight = new Point();
GeoPoint topLeft = new GeoPoint();
GeoPoint bottomRight = new GeoPoint();
String filterName = null;
String currentFieldName = null;
@ -76,7 +80,7 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
Point point = null;
GeoPoint point = null;
if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
point = topLeft;
} else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
@ -85,15 +89,15 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
if (point != null) {
token = parser.nextToken();
point.lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
token = parser.nextToken();
point.lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
}
} else if (token == XContentParser.Token.START_OBJECT) {
Point point = null;
GeoPoint point = null;
if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
point = topLeft;
} else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
@ -106,13 +110,11 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (currentFieldName.equals(GeoPointFieldMapper.Names.LAT)) {
point.lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) {
point.lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
point.lat = values[0];
point.lon = values[1];
GeoHashUtils.decode(parser.text(), point);
}
}
}
@ -121,7 +123,7 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
if ("field".equals(currentFieldName)) {
fieldName = parser.text();
} else {
Point point = null;
GeoPoint point = null;
if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) {
point = topLeft;
} else if ("bottom_right".equals(currentFieldName) || "bottomRight".equals(currentFieldName)) {
@ -130,15 +132,7 @@ public class GeoBoundingBoxFilterParser implements FilterParser {
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];
}
point.resetFromString(value);
}
}
}

View File

@ -19,9 +19,9 @@
package org.elasticsearch.index.query;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.search.geo.GeoDistance;
import java.io.IOException;

View File

@ -20,6 +20,10 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser;
@ -28,7 +32,7 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.*;
import org.elasticsearch.index.search.geo.GeoDistanceFilter;
import java.io.IOException;
@ -65,8 +69,7 @@ public class GeoDistanceFilterParser implements FilterParser {
CacheKeyFilter.Key cacheKey = null;
String filterName = null;
String currentFieldName = null;
double lat = 0;
double lon = 0;
GeoPoint point = new GeoPoint();
String fieldName = null;
double distance = 0;
Object vDistance = null;
@ -80,9 +83,9 @@ public class GeoDistanceFilterParser implements FilterParser {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
token = parser.nextToken();
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
token = parser.nextToken();
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
@ -96,13 +99,11 @@ public class GeoDistanceFilterParser implements FilterParser {
currentName = parser.currentName();
} else if (token.isValue()) {
if (currentName.equals(GeoPointFieldMapper.Names.LAT)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.LON)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
} else {
throw new QueryParsingException(parseContext.index(), "[geo_distance] filter does not support [" + currentFieldName + "]");
}
@ -120,15 +121,13 @@ public class GeoDistanceFilterParser implements FilterParser {
} else if (currentFieldName.equals("distance_type") || currentFieldName.equals("distanceType")) {
geoDistance = GeoDistance.fromString(parser.text());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LAT_SUFFIX)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.LAT_SUFFIX.length());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LON_SUFFIX)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.LON_SUFFIX.length());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.GEOHASH_SUFFIX)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length());
} else if ("_name".equals(currentFieldName)) {
filterName = parser.text();
@ -142,17 +141,7 @@ public class GeoDistanceFilterParser implements FilterParser {
normalizeLat = parser.booleanValue();
normalizeLon = parser.booleanValue();
} else {
// assume the value is the actual value
String value = parser.text();
int comma = value.indexOf(',');
if (comma != -1) {
lat = Double.parseDouble(value.substring(0, comma).trim());
lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
double[] values = GeoHashUtils.decode(value);
lat = values[0];
lon = values[1];
}
point.resetFromString(parser.text());
fieldName = currentFieldName;
}
}
@ -166,10 +155,7 @@ public class GeoDistanceFilterParser implements FilterParser {
distance = geoDistance.normalize(distance, DistanceUnit.MILES);
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
}
MapperService.SmartNameFieldMappers smartMappers = parseContext.smartFieldMappers(fieldName);
@ -184,7 +170,7 @@ public class GeoDistanceFilterParser implements FilterParser {
IndexGeoPointFieldData indexFieldData = parseContext.fieldData().getForField(mapper);
Filter filter = new GeoDistanceFilter(lat, lon, distance, geoDistance, indexFieldData, geoMapper, optimizeBbox);
Filter filter = new GeoDistanceFilter(point.lat(), point.lon(), distance, geoDistance, indexFieldData, geoMapper, optimizeBbox);
if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.query;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.search.geo.GeoDistance;
import java.io.IOException;

View File

@ -20,6 +20,10 @@
package org.elasticsearch.index.query;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser;
@ -28,7 +32,7 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.*;
import org.elasticsearch.index.search.geo.GeoDistanceRangeFilter;
import java.io.IOException;
@ -65,8 +69,7 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
CacheKeyFilter.Key cacheKey = null;
String filterName = null;
String currentFieldName = null;
double lat = 0;
double lon = 0;
GeoPoint point = new GeoPoint();
String fieldName = null;
Object vFrom = null;
Object vTo = null;
@ -82,12 +85,13 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
token = parser.nextToken();
lon = parser.doubleValue();
double lon = parser.doubleValue();
token = parser.nextToken();
lat = parser.doubleValue();
double lat = parser.doubleValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
point.reset(lat, lon);
fieldName = currentFieldName;
} else if (token == XContentParser.Token.START_OBJECT) {
// the json in the format of -> field : { lat : 30, lon : 12 }
@ -98,13 +102,11 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
currentName = parser.currentName();
} else if (token.isValue()) {
if (currentName.equals(GeoPointFieldMapper.Names.LAT)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.LON)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
}
}
}
@ -164,15 +166,13 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
} else if (currentFieldName.equals("distance_type") || currentFieldName.equals("distanceType")) {
geoDistance = GeoDistance.fromString(parser.text());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LAT_SUFFIX)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.LAT_SUFFIX.length());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LON_SUFFIX)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.LON_SUFFIX.length());
} else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.GEOHASH_SUFFIX)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length());
} else if ("_name".equals(currentFieldName)) {
filterName = parser.text();
@ -186,17 +186,7 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
normalizeLat = parser.booleanValue();
normalizeLon = parser.booleanValue();
} else {
// assume the value is the actual value
String value = parser.text();
int comma = value.indexOf(',');
if (comma != -1) {
lat = Double.parseDouble(value.substring(0, comma).trim());
lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
double[] values = GeoHashUtils.decode(value);
lat = values[0];
lon = values[1];
}
point.resetFromString(parser.text());
fieldName = currentFieldName;
}
}
@ -222,10 +212,7 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
}
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
}
MapperService.SmartNameFieldMappers smartMappers = parseContext.smartFieldMappers(fieldName);
@ -239,7 +226,7 @@ public class GeoDistanceRangeFilterParser implements FilterParser {
GeoPointFieldMapper geoMapper = ((GeoPointFieldMapper.GeoStringFieldMapper) mapper).geoMapper();
IndexGeoPointFieldData indexFieldData = parseContext.fieldData().getForField(mapper);
Filter filter = new GeoDistanceRangeFilter(lat, lon, from, to, includeLower, includeUpper, geoDistance, geoMapper, indexFieldData, optimizeBbox);
Filter filter = new GeoDistanceRangeFilter(point, from, to, includeLower, includeUpper, geoDistance, geoMapper, indexFieldData, optimizeBbox);
if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}

View File

@ -20,9 +20,9 @@
package org.elasticsearch.index.query;
import com.google.common.collect.Lists;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.index.search.geo.Point;
import java.io.IOException;
import java.util.List;
@ -34,7 +34,7 @@ public class GeoPolygonFilterBuilder extends BaseFilterBuilder {
private final String name;
private final List<Point> points = Lists.newArrayList();
private final List<GeoPoint> points = Lists.newArrayList();
private Boolean cache;
private String cacheKey;
@ -53,13 +53,13 @@ public class GeoPolygonFilterBuilder extends BaseFilterBuilder {
* @return
*/
public GeoPolygonFilterBuilder addPoint(double lat, double lon) {
points.add(new Point(lat, lon));
points.add(new GeoPoint(lat, lon));
return this;
}
public GeoPolygonFilterBuilder addPoint(String geohash) {
double[] values = GeoHashUtils.decode(geohash);
return addPoint(values[0], values[1]);
points.add(GeoHashUtils.decode(geohash));
return this;
}
/**
@ -89,8 +89,8 @@ public class GeoPolygonFilterBuilder extends BaseFilterBuilder {
builder.startObject(name);
builder.startArray("points");
for (Point point : points) {
builder.startArray().value(point.lon).value(point.lat).endArray();
for (GeoPoint point : points) {
builder.startArray().value(point.lon()).value(point.lat()).endArray();
}
builder.endArray();
builder.endObject();

View File

@ -21,6 +21,9 @@ package org.elasticsearch.index.query;
import com.google.common.collect.Lists;
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;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.cache.filter.support.CacheKeyFilter;
@ -28,10 +31,7 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.index.search.geo.GeoPolygonFilter;
import org.elasticsearch.index.search.geo.GeoUtils;
import org.elasticsearch.index.search.geo.Point;
import java.io.IOException;
import java.util.List;
@ -70,7 +70,7 @@ public class GeoPolygonFilterParser implements FilterParser {
boolean cache = false;
CacheKeyFilter.Key cacheKey = null;
String fieldName = null;
List<Point> points = Lists.newArrayList();
List<GeoPoint> points = Lists.newArrayList();
boolean normalizeLon = true;
boolean normalizeLat = true;
@ -94,46 +94,32 @@ public class GeoPolygonFilterParser implements FilterParser {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
Point point = new Point();
token = parser.nextToken();
point.lon = parser.doubleValue();
double lon = parser.doubleValue();
token = parser.nextToken();
point.lat = parser.doubleValue();
double lat = parser.doubleValue();
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
points.add(point);
points.add(new GeoPoint(lat, lon));
} else if (token == XContentParser.Token.START_OBJECT) {
Point point = new Point();
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.lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.LON)) {
point.lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentFieldName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
point.lat = values[0];
point.lon = values[1];
GeoHashUtils.decode(parser.text(), point);
}
}
}
points.add(point);
} else if (token.isValue()) {
Point point = new Point();
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];
}
points.add(point);
points.add(new GeoPoint().resetFromString(parser.text()));
}
}
} else {
@ -162,7 +148,7 @@ public class GeoPolygonFilterParser implements FilterParser {
}
if (normalizeLat || normalizeLon) {
for (Point point : points) {
for (GeoPoint point : points) {
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
}
}
@ -177,7 +163,7 @@ public class GeoPolygonFilterParser implements FilterParser {
}
IndexGeoPointFieldData indexFieldData = parseContext.fieldData().getForField(mapper);
Filter filter = new GeoPolygonFilter(points.toArray(new Point[points.size()]), indexFieldData);
Filter filter = new GeoPolygonFilter(points.toArray(new GeoPoint[points.size()]), indexFieldData);
if (cache) {
filter = parseContext.cacheFilter(filter, cacheKey);
}

View File

@ -25,13 +25,14 @@ import org.apache.lucene.search.Filter;
import org.apache.lucene.util.Bits;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.docset.AndDocIdSet;
import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.common.lucene.docset.MatchDocIdSet;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import java.io.IOException;

View File

@ -26,13 +26,14 @@ import org.apache.lucene.util.Bits;
import org.apache.lucene.util.NumericUtils;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.docset.AndDocIdSet;
import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.common.lucene.docset.MatchDocIdSet;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import java.io.IOException;
@ -55,10 +56,10 @@ public class GeoDistanceRangeFilter extends Filter {
private final IndexGeoPointFieldData indexFieldData;
public GeoDistanceRangeFilter(double lat, double lon, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper, GeoDistance geoDistance, GeoPointFieldMapper mapper, IndexGeoPointFieldData indexFieldData,
public GeoDistanceRangeFilter(GeoPoint point, Double lowerVal, Double upperVal, boolean includeLower, boolean includeUpper, GeoDistance geoDistance, GeoPointFieldMapper mapper, IndexGeoPointFieldData indexFieldData,
String optimizeBbox) {
this.lat = lat;
this.lon = lon;
this.lat = point.lat();
this.lon = point.lon();
this.geoDistance = geoDistance;
this.indexFieldData = indexFieldData;

View File

@ -24,10 +24,10 @@ import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.util.Bits;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.docset.MatchDocIdSet;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import java.io.IOException;
import java.util.Arrays;
@ -37,16 +37,16 @@ import java.util.Arrays;
*/
public class GeoPolygonFilter extends Filter {
private final Point[] points;
private final GeoPoint[] points;
private final IndexGeoPointFieldData indexFieldData;
public GeoPolygonFilter(Point[] points, IndexGeoPointFieldData indexFieldData) {
public GeoPolygonFilter(GeoPoint[] points, IndexGeoPointFieldData indexFieldData) {
this.points = points;
this.indexFieldData = indexFieldData;
}
public Point[] points() {
public GeoPoint[] points() {
return points;
}
@ -67,9 +67,9 @@ public class GeoPolygonFilter extends Filter {
public static class GeoPolygonDocIdSet extends MatchDocIdSet {
private final GeoPointValues values;
private final Point[] points;
private final GeoPoint[] points;
public GeoPolygonDocIdSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, Point[] points) {
public GeoPolygonDocIdSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, GeoPoint[] points) {
super(maxDoc, acceptDocs);
this.values = values;
this.points = points;
@ -101,16 +101,16 @@ public class GeoPolygonFilter extends Filter {
return false;
}
private static boolean pointInPolygon(Point[] points, double lat, double lon) {
private static boolean pointInPolygon(GeoPoint[] points, double lat, double lon) {
int i;
int j = points.length - 1;
boolean inPoly = false;
for (i = 0; i < points.length; i++) {
if (points[i].lon < lon && points[j].lon >= lon
|| points[j].lon < lon && points[i].lon >= lon) {
if (points[i].lat + (lon - points[i].lon) /
(points[j].lon - points[i].lon) * (points[j].lat - points[i].lat) < lat) {
if (points[i].lon() < lon && points[j].lon() >= lon
|| points[j].lon() < lon && points[i].lon() >= lon) {
if (points[i].lat() + (lon - points[i].lon()) /
(points[j].lon() - points[i].lon()) * (points[j].lat() - points[i].lat()) < lat) {
inPoly = !inPoly;
}
}

View File

@ -24,10 +24,10 @@ import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.apache.lucene.util.Bits;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.docset.MatchDocIdSet;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.mapper.geo.GeoPoint;
import java.io.IOException;
@ -36,22 +36,22 @@ import java.io.IOException;
*/
public class InMemoryGeoBoundingBoxFilter extends Filter {
private final Point topLeft;
private final Point bottomRight;
private final GeoPoint topLeft;
private final GeoPoint bottomRight;
private final IndexGeoPointFieldData indexFieldData;
public InMemoryGeoBoundingBoxFilter(Point topLeft, Point bottomRight, IndexGeoPointFieldData indexFieldData) {
public InMemoryGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, IndexGeoPointFieldData indexFieldData) {
this.topLeft = topLeft;
this.bottomRight = bottomRight;
this.indexFieldData = indexFieldData;
}
public Point topLeft() {
public GeoPoint topLeft() {
return topLeft;
}
public Point bottomRight() {
public GeoPoint bottomRight() {
return bottomRight;
}
@ -64,7 +64,7 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
final GeoPointValues values = indexFieldData.load(context).getGeoPointValues();
//checks to see if bounding box crosses 180 degrees
if (topLeft.lon > bottomRight.lon) {
if (topLeft.lon() > bottomRight.lon()) {
return new Meridian180GeoBoundingBoxDocSet(context.reader().maxDoc(), acceptedDocs, values, topLeft, bottomRight);
} else {
return new GeoBoundingBoxDocSet(context.reader().maxDoc(), acceptedDocs, values, topLeft, bottomRight);
@ -78,10 +78,10 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
public static class Meridian180GeoBoundingBoxDocSet extends MatchDocIdSet {
private final GeoPointValues values;
private final Point topLeft;
private final Point bottomRight;
private final GeoPoint topLeft;
private final GeoPoint bottomRight;
public Meridian180GeoBoundingBoxDocSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, Point topLeft, Point bottomRight) {
public Meridian180GeoBoundingBoxDocSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, GeoPoint topLeft, GeoPoint bottomRight) {
super(maxDoc, acceptDocs);
this.values = values;
this.topLeft = topLeft;
@ -103,16 +103,16 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
GeoPointValues.Iter iter = values.getIter(doc);
while (iter.hasNext()) {
GeoPoint point = iter.next();
if (((topLeft.lon <= point.lon() || bottomRight.lon >= point.lon())) &&
(topLeft.lat >= point.lat() && bottomRight.lat <= point.lat())) {
if (((topLeft.lon() <= point.lon() || bottomRight.lon() >= point.lon())) &&
(topLeft.lat() >= point.lat() && bottomRight.lat() <= point.lat())) {
return true;
}
}
} else {
GeoPoint point = values.getValue(doc);
if (((topLeft.lon <= point.lon() || bottomRight.lon >= point.lon())) &&
(topLeft.lat >= point.lat() && bottomRight.lat <= point.lat())) {
if (((topLeft.lon() <= point.lon() || bottomRight.lon() >= point.lon())) &&
(topLeft.lat() >= point.lat() && bottomRight.lat() <= point.lat())) {
return true;
}
}
@ -122,10 +122,10 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
public static class GeoBoundingBoxDocSet extends MatchDocIdSet {
private final GeoPointValues values;
private final Point topLeft;
private final Point bottomRight;
private final GeoPoint topLeft;
private final GeoPoint bottomRight;
public GeoBoundingBoxDocSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, Point topLeft, Point bottomRight) {
public GeoBoundingBoxDocSet(int maxDoc, @Nullable Bits acceptDocs, GeoPointValues values, GeoPoint topLeft, GeoPoint bottomRight) {
super(maxDoc, acceptDocs);
this.values = values;
this.topLeft = topLeft;
@ -147,15 +147,15 @@ public class InMemoryGeoBoundingBoxFilter extends Filter {
GeoPointValues.Iter iter = values.getIter(doc);
while (iter.hasNext()) {
GeoPoint point = iter.next();
if (topLeft.lon <= point.lon() && bottomRight.lon >= point.lon()
&& topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) {
if (topLeft.lon() <= point.lon() && bottomRight.lon() >= point.lon()
&& topLeft.lat() >= point.lat() && bottomRight.lat() <= point.lat()) {
return true;
}
}
} else {
GeoPoint point = values.getValue(doc);
if (topLeft.lon <= point.lon() && bottomRight.lon >= point.lon()
&& topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) {
if (topLeft.lon() <= point.lon() && bottomRight.lon() >= point.lon()
&& topLeft.lat() >= point.lat() && bottomRight.lat() <= point.lat()) {
return true;
}
}

View File

@ -25,6 +25,7 @@ import org.apache.lucene.search.Filter;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.FixedBitSet;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.docset.DocIdSets;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
@ -34,12 +35,12 @@ import java.io.IOException;
*/
public class IndexedGeoBoundingBoxFilter {
public static Filter create(Point topLeft, Point bottomRight, GeoPointFieldMapper fieldMapper) {
public static Filter create(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapper fieldMapper) {
if (!fieldMapper.isEnableLatLon()) {
throw new ElasticSearchIllegalArgumentException("lat/lon is not enabled (indexed) for field [" + fieldMapper.name() + "], can't use indexed filter on it");
}
//checks to see if bounding box crosses 180 degrees
if (topLeft.lon > bottomRight.lon) {
if (topLeft.lon() > bottomRight.lon()) {
return new LeftGeoBoundingBoxFilter(topLeft, bottomRight, fieldMapper);
} else {
return new RightGeoBoundingBoxFilter(topLeft, bottomRight, fieldMapper);
@ -52,10 +53,10 @@ public class IndexedGeoBoundingBoxFilter {
final Filter lonFilter2;
final Filter latFilter;
public LeftGeoBoundingBoxFilter(Point topLeft, Point bottomRight, GeoPointFieldMapper fieldMapper) {
lonFilter1 = fieldMapper.lonMapper().rangeFilter(null, bottomRight.lon, true, true);
lonFilter2 = fieldMapper.lonMapper().rangeFilter(topLeft.lon, null, true, true);
latFilter = fieldMapper.latMapper().rangeFilter(bottomRight.lat, topLeft.lat, true, true);
public LeftGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapper fieldMapper) {
lonFilter1 = fieldMapper.lonMapper().rangeFilter(null, bottomRight.lon(), true, true);
lonFilter2 = fieldMapper.lonMapper().rangeFilter(topLeft.lon(), null, true, true);
latFilter = fieldMapper.latMapper().rangeFilter(bottomRight.lat(), topLeft.lat(), true, true);
}
@Override
@ -119,9 +120,9 @@ public class IndexedGeoBoundingBoxFilter {
final Filter lonFilter;
final Filter latFilter;
public RightGeoBoundingBoxFilter(Point topLeft, Point bottomRight, GeoPointFieldMapper fieldMapper) {
lonFilter = fieldMapper.lonMapper().rangeFilter(topLeft.lon, bottomRight.lon, true, true);
latFilter = fieldMapper.latMapper().rangeFilter(bottomRight.lat, topLeft.lat, true, true);
public RightGeoBoundingBoxFilter(GeoPoint topLeft, GeoPoint bottomRight, GeoPointFieldMapper fieldMapper) {
lonFilter = fieldMapper.lonMapper().rangeFilter(topLeft.lon(), bottomRight.lon(), true, true);
latFilter = fieldMapper.latMapper().rangeFilter(bottomRight.lat(), topLeft.lat(), true, true);
}
@Override

View File

@ -1,63 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.search.geo;
/**
*/
public class Point {
public double lat;
public double lon;
public Point() {
}
public Point(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (Double.compare(point.lat, lat) != 0) return false;
if (Double.compare(point.lon, lon) != 0) return false;
return true;
}
@Override
public int hashCode() {
int result;
long temp;
temp = lat != +0.0d ? Double.doubleToLongBits(lat) : 0L;
result = (int) (temp ^ (temp >>> 32));
temp = lon != +0.0d ? Double.doubleToLongBits(lon) : 0L;
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
public String toString() {
return "[" + lat + ", " + lon + "]";
}
}

View File

@ -21,10 +21,10 @@ package org.elasticsearch.search.facet.geodistance;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.search.builder.SearchSourceBuilderException;
import org.elasticsearch.search.facet.AbstractFacetBuilder;
@ -36,8 +36,6 @@ import java.util.Map;
* A geo distance builder allowing to create a facet of distances from a specific location including the
* number of hits within each distance range, and aggregated data (like totals of either the distance or
* cusotm value fields).
*
*
*/
public class GeoDistanceFacetBuilder extends AbstractFacetBuilder {

View File

@ -20,10 +20,10 @@
package org.elasticsearch.search.facet.geodistance;
import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.search.facet.AbstractFacetCollector;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.internal.SearchContext;

View File

@ -21,6 +21,10 @@ package org.elasticsearch.search.facet.geodistance;
import com.google.common.collect.Lists;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.DistanceUnit;
@ -29,10 +33,6 @@ import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.index.search.geo.GeoUtils;
import org.elasticsearch.index.search.geo.Point;
import org.elasticsearch.search.facet.Facet;
import org.elasticsearch.search.facet.FacetCollector;
import org.elasticsearch.search.facet.FacetPhaseExecutionException;
@ -66,8 +66,7 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face
String valueScript = null;
String scriptLang = null;
Map<String, Object> params = null;
double lat = Double.NaN;
double lon = Double.NaN;
GeoPoint point = new GeoPoint();
DistanceUnit unit = DistanceUnit.KILOMETERS;
GeoDistance geoDistance = GeoDistance.ARC;
List<GeoDistanceFacet.Entry> entries = Lists.newArrayList();
@ -105,9 +104,9 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face
}
} else {
token = parser.nextToken();
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
token = parser.nextToken();
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
@ -124,13 +123,11 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face
currentName = parser.currentName();
} else if (token.isValue()) {
if (currentName.equals(GeoPointFieldMapper.Names.LAT)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.LON)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
}
}
}
@ -151,35 +148,19 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face
normalizeLon = parser.booleanValue();
} else {
// assume the value is the actual value
String value = parser.text();
int comma = value.indexOf(',');
if (comma != -1) {
lat = Double.parseDouble(value.substring(0, comma).trim());
lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
double[] values = GeoHashUtils.decode(value);
lat = values[0];
lon = values[1];
}
point.resetFromString(parser.text());
fieldName = currentName;
}
}
}
if (Double.isNaN(lat) || Double.isNaN(lon)) {
throw new FacetPhaseExecutionException(facetName, "lat/lon not set for geo_distance facet");
}
if (entries.isEmpty()) {
throw new FacetPhaseExecutionException(facetName, "no ranges defined for geo_distance facet");
}
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
}
FieldMapper keyFieldMapper = context.smartNameFieldMapper(fieldName);
@ -194,16 +175,16 @@ public class GeoDistanceFacetProcessor extends AbstractComponent implements Face
throw new FacetPhaseExecutionException(facetName, "failed to find mapping for [" + valueFieldName + "]");
}
IndexNumericFieldData valueIndexFieldData = context.fieldData().getForField(valueFieldMapper);
return new ValueGeoDistanceFacetCollector(facetName, keyIndexFieldData, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
return new ValueGeoDistanceFacetCollector(facetName, keyIndexFieldData, point.lat(), point.lon(), unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
context, valueIndexFieldData);
}
if (valueScript != null) {
return new ScriptGeoDistanceFacetCollector(facetName, keyIndexFieldData, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
return new ScriptGeoDistanceFacetCollector(facetName, keyIndexFieldData, point.lat(), point.lon(), unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
context, scriptLang, valueScript, params);
}
return new GeoDistanceFacetCollector(facetName, keyIndexFieldData, lat, lon, unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
return new GeoDistanceFacetCollector(facetName, keyIndexFieldData, point.lat(), point.lon(), unit, geoDistance, entries.toArray(new GeoDistanceFacet.Entry[entries.size()]),
context);
}

View File

@ -21,10 +21,10 @@ package org.elasticsearch.search.facet.geodistance;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.Scorer;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.script.SearchScript;
import org.elasticsearch.search.internal.SearchContext;

View File

@ -20,12 +20,12 @@
package org.elasticsearch.search.facet.geodistance;
import org.apache.lucene.index.AtomicReaderContext;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.fielddata.DoubleValues;
import org.elasticsearch.index.fielddata.GeoPointValues;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;

View File

@ -19,16 +19,14 @@
package org.elasticsearch.search.sort;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.search.geo.GeoDistance;
import java.io.IOException;
/**
* A geo distance based sorting on a geo point like field.
*
*
*/
public class GeoDistanceSortBuilder extends SortBuilder {

View File

@ -21,16 +21,16 @@ package org.elasticsearch.search.sort;
import org.apache.lucene.search.SortField;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.fieldcomparator.GeoDistanceComparatorSource;
import org.elasticsearch.index.mapper.FieldMapper;
import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.index.search.geo.GeoUtils;
import org.elasticsearch.index.search.geo.Point;
import org.elasticsearch.search.internal.SearchContext;
/**
@ -46,8 +46,7 @@ public class GeoDistanceSortParser implements SortParser {
@Override
public SortField parse(XContentParser parser, SearchContext context) throws Exception {
String fieldName = null;
double lat = Double.NaN;
double lon = Double.NaN;
GeoPoint point = new GeoPoint();
DistanceUnit unit = DistanceUnit.KILOMETERS;
GeoDistance geoDistance = GeoDistance.ARC;
boolean reverse = false;
@ -62,9 +61,9 @@ public class GeoDistanceSortParser implements SortParser {
currentName = parser.currentName();
} else if (token == XContentParser.Token.START_ARRAY) {
token = parser.nextToken();
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
token = parser.nextToken();
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
}
@ -77,13 +76,11 @@ public class GeoDistanceSortParser implements SortParser {
currentName = parser.currentName();
} else if (token.isValue()) {
if (currentName.equals(GeoPointFieldMapper.Names.LAT)) {
lat = parser.doubleValue();
point.resetLat(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.LON)) {
lon = parser.doubleValue();
point.resetLon(parser.doubleValue());
} else if (currentName.equals(GeoPointFieldMapper.Names.GEOHASH)) {
double[] values = GeoHashUtils.decode(parser.text());
lat = values[0];
lon = values[1];
GeoHashUtils.decode(parser.text(), point);
}
}
}
@ -100,28 +97,14 @@ public class GeoDistanceSortParser implements SortParser {
normalizeLat = parser.booleanValue();
normalizeLon = parser.booleanValue();
} else {
// assume the value is the actual value
String value = parser.text();
int comma = value.indexOf(',');
if (comma != -1) {
lat = Double.parseDouble(value.substring(0, comma).trim());
lon = Double.parseDouble(value.substring(comma + 1).trim());
} else {
double[] values = GeoHashUtils.decode(value);
lat = values[0];
lon = values[1];
}
point.resetFromString(parser.text());
fieldName = currentName;
}
}
}
if (normalizeLat || normalizeLon) {
Point point = new Point(lat, lon);
GeoUtils.normalizePoint(point, normalizeLat, normalizeLon);
lat = point.lat;
lon = point.lon;
}
FieldMapper mapper = context.smartNameFieldMapper(fieldName);
@ -130,6 +113,6 @@ public class GeoDistanceSortParser implements SortParser {
}
IndexGeoPointFieldData indexFieldData = context.fieldData().getForField(mapper);
return new SortField(fieldName, new GeoDistanceComparatorSource(indexFieldData, lat, lon, unit, geoDistance), reverse);
return new SortField(fieldName, new GeoDistanceComparatorSource(indexFieldData, point.lat(), point.lon(), unit, geoDistance), reverse);
}
}

View File

@ -22,11 +22,11 @@ package org.elasticsearch.benchmark.search.geo;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.SizeValue;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;

View File

@ -21,8 +21,8 @@ package org.elasticsearch.test.integration.search.geo;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

View File

@ -21,13 +21,13 @@ package org.elasticsearch.test.integration.validate;
import org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.hamcrest.Matcher;
import org.testng.annotations.AfterClass;

View File

@ -19,10 +19,10 @@
package org.elasticsearch.test.unit.index.mapper.geo;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.hamcrest.MatcherAssert;
import org.testng.annotations.Test;

View File

@ -19,10 +19,10 @@
package org.elasticsearch.test.unit.index.mapper.geo;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;

View File

@ -19,11 +19,11 @@
package org.elasticsearch.test.unit.index.mapper.geo;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;

View File

@ -1803,10 +1803,10 @@ public class SimpleIndexQueryParserTests {
XConstantScoreQuery constantScoreQuery = (XConstantScoreQuery) parsedQuery.query();
InMemoryGeoBoundingBoxFilter filter = (InMemoryGeoBoundingBoxFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.topLeft().lat, closeTo(40, 0.00001));
assertThat(filter.topLeft().lon, closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat, closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon, closeTo(-80, 0.00001));
assertThat(filter.topLeft().lat(), closeTo(40, 0.00001));
assertThat(filter.topLeft().lon(), closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat(), closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon(), closeTo(-80, 0.00001));
}
@ -1819,10 +1819,10 @@ public class SimpleIndexQueryParserTests {
XConstantScoreQuery constantScoreQuery = (XConstantScoreQuery) parsedQuery;
InMemoryGeoBoundingBoxFilter filter = (InMemoryGeoBoundingBoxFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.topLeft().lat, closeTo(40, 0.00001));
assertThat(filter.topLeft().lon, closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat, closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon, closeTo(-80, 0.00001));
assertThat(filter.topLeft().lat(), closeTo(40, 0.00001));
assertThat(filter.topLeft().lon(), closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat(), closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon(), closeTo(-80, 0.00001));
}
@Test
@ -1834,10 +1834,10 @@ public class SimpleIndexQueryParserTests {
XConstantScoreQuery constantScoreQuery = (XConstantScoreQuery) parsedQuery;
InMemoryGeoBoundingBoxFilter filter = (InMemoryGeoBoundingBoxFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.topLeft().lat, closeTo(40, 0.00001));
assertThat(filter.topLeft().lon, closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat, closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon, closeTo(-80, 0.00001));
assertThat(filter.topLeft().lat(), closeTo(40, 0.00001));
assertThat(filter.topLeft().lon(), closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat(), closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon(), closeTo(-80, 0.00001));
}
@Test
@ -1849,10 +1849,10 @@ public class SimpleIndexQueryParserTests {
XConstantScoreQuery constantScoreQuery = (XConstantScoreQuery) parsedQuery;
InMemoryGeoBoundingBoxFilter filter = (InMemoryGeoBoundingBoxFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.topLeft().lat, closeTo(40, 0.00001));
assertThat(filter.topLeft().lon, closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat, closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon, closeTo(-80, 0.00001));
assertThat(filter.topLeft().lat(), closeTo(40, 0.00001));
assertThat(filter.topLeft().lon(), closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat(), closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon(), closeTo(-80, 0.00001));
}
@Test
@ -1864,10 +1864,10 @@ public class SimpleIndexQueryParserTests {
XConstantScoreQuery constantScoreQuery = (XConstantScoreQuery) parsedQuery;
InMemoryGeoBoundingBoxFilter filter = (InMemoryGeoBoundingBoxFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.topLeft().lat, closeTo(40, 0.00001));
assertThat(filter.topLeft().lon, closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat, closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon, closeTo(-80, 0.00001));
assertThat(filter.topLeft().lat(), closeTo(40, 0.00001));
assertThat(filter.topLeft().lon(), closeTo(-70, 0.00001));
assertThat(filter.bottomRight().lat(), closeTo(30, 0.00001));
assertThat(filter.bottomRight().lon(), closeTo(-80, 0.00001));
}
@Test
@ -1881,12 +1881,12 @@ public class SimpleIndexQueryParserTests {
GeoPolygonFilter filter = (GeoPolygonFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.points().length, equalTo(3));
assertThat(filter.points()[0].lat, closeTo(40, 0.00001));
assertThat(filter.points()[0].lon, closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat, closeTo(30, 0.00001));
assertThat(filter.points()[1].lon, closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat, closeTo(20, 0.00001));
assertThat(filter.points()[2].lon, closeTo(-90, 0.00001));
assertThat(filter.points()[0].lat(), closeTo(40, 0.00001));
assertThat(filter.points()[0].lon(), closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat(), closeTo(30, 0.00001));
assertThat(filter.points()[1].lon(), closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat(), closeTo(20, 0.00001));
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
}
@Test
@ -1899,12 +1899,12 @@ public class SimpleIndexQueryParserTests {
GeoPolygonFilter filter = (GeoPolygonFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.points().length, equalTo(3));
assertThat(filter.points()[0].lat, closeTo(40, 0.00001));
assertThat(filter.points()[0].lon, closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat, closeTo(30, 0.00001));
assertThat(filter.points()[1].lon, closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat, closeTo(20, 0.00001));
assertThat(filter.points()[2].lon, closeTo(-90, 0.00001));
assertThat(filter.points()[0].lat(), closeTo(40, 0.00001));
assertThat(filter.points()[0].lon(), closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat(), closeTo(30, 0.00001));
assertThat(filter.points()[1].lon(), closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat(), closeTo(20, 0.00001));
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
}
@Test
@ -1917,12 +1917,12 @@ public class SimpleIndexQueryParserTests {
GeoPolygonFilter filter = (GeoPolygonFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.points().length, equalTo(3));
assertThat(filter.points()[0].lat, closeTo(40, 0.00001));
assertThat(filter.points()[0].lon, closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat, closeTo(30, 0.00001));
assertThat(filter.points()[1].lon, closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat, closeTo(20, 0.00001));
assertThat(filter.points()[2].lon, closeTo(-90, 0.00001));
assertThat(filter.points()[0].lat(), closeTo(40, 0.00001));
assertThat(filter.points()[0].lon(), closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat(), closeTo(30, 0.00001));
assertThat(filter.points()[1].lon(), closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat(), closeTo(20, 0.00001));
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
}
@Test
@ -1935,12 +1935,12 @@ public class SimpleIndexQueryParserTests {
GeoPolygonFilter filter = (GeoPolygonFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.points().length, equalTo(3));
assertThat(filter.points()[0].lat, closeTo(40, 0.00001));
assertThat(filter.points()[0].lon, closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat, closeTo(30, 0.00001));
assertThat(filter.points()[1].lon, closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat, closeTo(20, 0.00001));
assertThat(filter.points()[2].lon, closeTo(-90, 0.00001));
assertThat(filter.points()[0].lat(), closeTo(40, 0.00001));
assertThat(filter.points()[0].lon(), closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat(), closeTo(30, 0.00001));
assertThat(filter.points()[1].lon(), closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat(), closeTo(20, 0.00001));
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
}
@Test
@ -1953,12 +1953,12 @@ public class SimpleIndexQueryParserTests {
GeoPolygonFilter filter = (GeoPolygonFilter) constantScoreQuery.getFilter();
assertThat(filter.fieldName(), equalTo("location"));
assertThat(filter.points().length, equalTo(3));
assertThat(filter.points()[0].lat, closeTo(40, 0.00001));
assertThat(filter.points()[0].lon, closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat, closeTo(30, 0.00001));
assertThat(filter.points()[1].lon, closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat, closeTo(20, 0.00001));
assertThat(filter.points()[2].lon, closeTo(-90, 0.00001));
assertThat(filter.points()[0].lat(), closeTo(40, 0.00001));
assertThat(filter.points()[0].lon(), closeTo(-70, 0.00001));
assertThat(filter.points()[1].lat(), closeTo(30, 0.00001));
assertThat(filter.points()[1].lon(), closeTo(-80, 0.00001));
assertThat(filter.points()[2].lat(), closeTo(20, 0.00001));
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
}
@Test

View File

@ -19,15 +19,13 @@
package org.elasticsearch.test.unit.index.search.geo;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.search.geo.GeoDistance;
import org.elasticsearch.index.search.geo.Point;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.*;
/**
*/
@ -49,22 +47,22 @@ public class GeoDistanceTests {
@Test
public void testArcDistanceVsPlaneInEllipsis() {
Point centre = new Point(48.8534100, 2.3488000);
Point northernPoint = new Point(48.8801108681, 2.35152032666);
Point westernPoint = new Point(48.85265, 2.308896);
GeoPoint centre = new GeoPoint(48.8534100, 2.3488000);
GeoPoint northernPoint = new GeoPoint(48.8801108681, 2.35152032666);
GeoPoint westernPoint = new GeoPoint(48.85265, 2.308896);
// With GeoDistance.ARC both the northern and western points are within the 4km range
assertThat(GeoDistance.ARC.calculate(centre.lat, centre.lon, northernPoint.lat,
northernPoint.lon, DistanceUnit.KILOMETERS), lessThan(4D));
assertThat(GeoDistance.ARC.calculate(centre.lat, centre.lon, westernPoint.lat,
westernPoint.lon, DistanceUnit.KILOMETERS), lessThan(4D));
assertThat(GeoDistance.ARC.calculate(centre.lat(), centre.lon(), northernPoint.lat(),
northernPoint.lon(), DistanceUnit.KILOMETERS), lessThan(4D));
assertThat(GeoDistance.ARC.calculate(centre.lat(), centre.lon(), westernPoint.lat(),
westernPoint.lon(), DistanceUnit.KILOMETERS), lessThan(4D));
// With GeoDistance.PLANE, only the northern point is within the 4km range,
// the western point is outside of the range due to the simple math it employs,
// meaning results will appear elliptical
assertThat(GeoDistance.PLANE.calculate(centre.lat, centre.lon, northernPoint.lat,
northernPoint.lon, DistanceUnit.KILOMETERS), lessThan(4D));
assertThat(GeoDistance.PLANE.calculate(centre.lat, centre.lon, westernPoint.lat,
westernPoint.lon, DistanceUnit.KILOMETERS), greaterThan(4D));
assertThat(GeoDistance.PLANE.calculate(centre.lat(), centre.lon(), northernPoint.lat(),
northernPoint.lon(), DistanceUnit.KILOMETERS), lessThan(4D));
assertThat(GeoDistance.PLANE.calculate(centre.lat(), centre.lon(), westernPoint.lat(),
westernPoint.lon(), DistanceUnit.KILOMETERS), greaterThan(4D));
}
}

View File

@ -19,7 +19,8 @@
package org.elasticsearch.test.unit.index.search.geo;
import org.elasticsearch.index.search.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
@ -50,10 +51,10 @@ public class GeoHashUtilsTests {
public void testDecodePreciseLongitudeLatitude() {
String hash = GeoHashUtils.encode(52.3738007, 4.8909347);
double[] latitudeLongitude = GeoHashUtils.decode(hash);
GeoPoint point = GeoHashUtils.decode(hash);
assertEquals(52.3738007, latitudeLongitude[0], 0.00001D);
assertEquals(4.8909347, latitudeLongitude[1], 0.00001D);
assertEquals(52.3738007, point.lat(), 0.00001D);
assertEquals(4.8909347, point.lon(), 0.00001D);
}
/**
@ -64,10 +65,10 @@ public class GeoHashUtilsTests {
public void testDecodeImpreciseLongitudeLatitude() {
String hash = GeoHashUtils.encode(84.6, 10.5);
double[] latitudeLongitude = GeoHashUtils.decode(hash);
GeoPoint point = GeoHashUtils.decode(hash);
assertEquals(84.6, latitudeLongitude[0], 0.00001D);
assertEquals(10.5, latitudeLongitude[1], 0.00001D);
assertEquals(84.6, point.lat(), 0.00001D);
assertEquals(10.5, point.lon(), 0.00001D);
}
/*
@ -78,16 +79,10 @@ public class GeoHashUtilsTests {
public void testDecodeEncode() {
String geoHash = "u173zq37x014";
assertEquals(geoHash, GeoHashUtils.encode(52.3738007, 4.8909347));
double[] decode = GeoHashUtils.decode(geoHash);
assertEquals(52.37380061d, decode[0], 0.000001d);
assertEquals(4.8909343d, decode[1], 0.000001d);
GeoPoint decode = GeoHashUtils.decode(geoHash);
assertEquals(52.37380061d, decode.lat(), 0.000001d);
assertEquals(4.8909343d, decode.lon(), 0.000001d);
assertEquals(geoHash, GeoHashUtils.encode(decode[0], decode[1]));
geoHash = "u173";
decode = GeoHashUtils.decode("u173");
geoHash = GeoHashUtils.encode(decode[0], decode[1]);
assertEquals(decode[0], GeoHashUtils.decode(geoHash)[0], 0.000001d);
assertEquals(decode[1], GeoHashUtils.decode(geoHash)[1], 0.000001d);
assertEquals(geoHash, GeoHashUtils.encode(decode.lat(), decode.lon()));
}
}

View File

@ -19,8 +19,8 @@
package org.elasticsearch.test.unit.index.search.geo;
import org.elasticsearch.index.search.geo.GeoUtils;
import org.elasticsearch.index.search.geo.Point;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.testng.annotations.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@ -85,7 +85,7 @@ public class GeoUtilsTests {
// Now with points, to check for longitude shifting with latitude normalization
// We've gone past the north pole and down the other side, the longitude will
// be shifted by 180
assertNormalizedPoint(new Point(90.5, 10), new Point(89.5, -170));
assertNormalizedPoint(new GeoPoint(90.5, 10), new GeoPoint(89.5, -170));
// Every 10-units, multiple full turns
for (int shift = -20; shift <= 20; ++shift) {
@ -199,7 +199,7 @@ public class GeoUtilsTests {
assertThat(GeoUtils.normalizeLat(+18000000000091.0), equalTo(GeoUtils.normalizeLat(+091.0)));
}
private static void assertNormalizedPoint(Point input, Point expected) {
private static void assertNormalizedPoint(GeoPoint input, GeoPoint expected) {
GeoUtils.normalizePoint(input);
assertThat(input, equalTo(expected));
}