diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoBoundingBoxFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoBoundingBoxFilter.java index 1eb03177b33e1..9afdb1fcd5153 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoBoundingBoxFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoBoundingBoxFilter.java @@ -24,8 +24,9 @@ import org.apache.lucene.search.Filter; import org.elasticsearch.common.lucene.docset.GetDocSet; import org.elasticsearch.index.cache.field.data.FieldDataCache; -import org.elasticsearch.index.field.data.FieldDataType; -import org.elasticsearch.index.field.data.NumericFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; import java.io.IOException; @@ -38,20 +39,14 @@ public class GeoBoundingBoxFilter extends Filter { private final Point bottomRight; - private final String latFieldName; - - private final String lonFieldName; - - private final FieldDataType fieldDataType; + private final String fieldName; private final FieldDataCache fieldDataCache; - public GeoBoundingBoxFilter(Point topLeft, Point bottomRight, String latFieldName, String lonFieldName, FieldDataType fieldDataType, FieldDataCache fieldDataCache) { + public GeoBoundingBoxFilter(Point topLeft, Point bottomRight, String fieldName, FieldDataCache fieldDataCache) { this.topLeft = topLeft; this.bottomRight = bottomRight; - this.latFieldName = latFieldName; - this.lonFieldName = lonFieldName; - this.fieldDataType = fieldDataType; + this.fieldName = fieldName; this.fieldDataCache = fieldDataCache; } @@ -63,55 +58,46 @@ public Point bottomRight() { return bottomRight; } - public String latFieldName() { - return latFieldName; - } - - public String lonFieldName() { - return lonFieldName; + public String fieldName() { + return fieldName; } @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { - final NumericFieldData latFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, latFieldName); - final NumericFieldData lonFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, lonFieldName); + final GeoPointFieldData fieldData = (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, fieldName); //checks to see if bounding box crosses 180 degrees if (topLeft.lon > bottomRight.lon) { return new GetDocSet(reader.maxDoc()) { @Override public boolean get(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return false; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - double lat = lats[i]; - double lon = lons[i]; - if (lon < 0) { - if (-180.0 <= lon && bottomRight.lon >= lon - && topLeft.lat >= lat && bottomRight.lat <= lat) { + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); + for (GeoPoint point : points) { + if (point.lon() < 0) { + if (-180.0 <= point.lon() && bottomRight.lon >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } else { - if (topLeft.lon <= lon && 180 >= lon - && topLeft.lat >= lat && bottomRight.lat <= lat) { + if (topLeft.lon <= point.lon() && 180 >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } } } else { - double lat = latFieldData.doubleValue(doc); - double lon = lonFieldData.doubleValue(doc); - if (lon < 0) { - if (-180.0 <= lon && bottomRight.lon >= lon - && topLeft.lat >= lat && bottomRight.lat <= lat) { + GeoPoint point = fieldData.value(doc); + if (point.lon() < 0) { + if (-180.0 <= point.lon() && bottomRight.lon >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } else { - if (topLeft.lon <= lon && 180 >= lon - && topLeft.lat >= lat && bottomRight.lat <= lat) { + if (topLeft.lon <= point.lon() && 180 >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } @@ -122,25 +108,23 @@ public String lonFieldName() { } else { return new GetDocSet(reader.maxDoc()) { @Override public boolean get(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return false; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - if (topLeft.lon <= lons[i] && bottomRight.lon >= lons[i] - && topLeft.lat >= lats[i] && bottomRight.lat <= lats[i]) { + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); + for (GeoPoint point : points) { + if (topLeft.lon <= point.lon() && bottomRight.lon >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } } else { - double lat = latFieldData.doubleValue(doc); - double lon = lonFieldData.doubleValue(doc); + GeoPoint point = fieldData.value(doc); - if (topLeft.lon <= lon && bottomRight.lon >= lon - && topLeft.lat >= lat && bottomRight.lat <= lat) { + if (topLeft.lon <= point.lon() && bottomRight.lon >= point.lon() + && topLeft.lat >= point.lat() && bottomRight.lat <= point.lat()) { return true; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceDataComparator.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceDataComparator.java index fbfd7d3f59fa3..9cf93f931750d 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceDataComparator.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceDataComparator.java @@ -25,11 +25,11 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.cache.field.data.FieldDataCache; -import org.elasticsearch.index.field.data.FieldDataType; -import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; import java.io.IOException; @@ -78,9 +78,7 @@ private InnerSource(String fieldName, double lat, double lon, DistanceUnit unit, protected final String fieldName; - protected final String indexLatFieldName; - - protected final String indexLonFieldName; + protected final String indexFieldName; protected final double lat; @@ -92,11 +90,7 @@ private InnerSource(String fieldName, double lat, double lon, DistanceUnit unit, protected final FieldDataCache fieldDataCache; - protected final FieldDataType fieldDataType; - - protected NumericFieldData latFieldData; - - protected NumericFieldData lonFieldData; + protected GeoPointFieldData fieldData; private final double[] values; @@ -113,23 +107,18 @@ public GeoDistanceDataComparator(int numHits, String fieldName, double lat, doub this.geoDistance = geoDistance; this.fieldDataCache = fieldDataCache; - FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName + GeoPointFieldMapper.Names.LAT_SUFFIX); + FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName); if (mapper == null) { throw new ElasticSearchIllegalArgumentException("No mapping found for field [" + fieldName + "] for geo distance sort"); } - this.indexLatFieldName = mapper.names().indexName(); - - mapper = mapperService.smartNameFieldMapper(fieldName + GeoPointFieldMapper.Names.LON_SUFFIX); - if (mapper == null) { - throw new ElasticSearchIllegalArgumentException("No mapping found for field [" + fieldName + "] for geo distance sort"); + if (mapper.fieldDataType() != GeoPointFieldDataType.TYPE) { + throw new ElasticSearchIllegalArgumentException("field [" + fieldName + "] is not a geo_point field"); } - this.indexLonFieldName = mapper.names().indexName(); - this.fieldDataType = mapper.fieldDataType(); + this.indexFieldName = mapper.names().indexName(); } @Override public void setNextReader(IndexReader reader, int docBase) throws IOException { - latFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, indexLatFieldName); - lonFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, indexLonFieldName); + fieldData = (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, indexFieldName); } @Override public int compare(int slot1, int slot2) { @@ -146,11 +135,12 @@ public GeoDistanceDataComparator(int numHits, String fieldName, double lat, doub @Override public int compareBottom(int doc) { double distance; - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { // is this true? push this to the "end" distance = Double.MAX_VALUE; } else { - distance = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), unit); + GeoPoint point = fieldData.value(doc); + distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); } final double v2 = distance; if (bottom > v2) { @@ -164,11 +154,12 @@ public GeoDistanceDataComparator(int numHits, String fieldName, double lat, doub @Override public void copy(int slot, int doc) { double distance; - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { // is this true? push this to the "end" distance = Double.MAX_VALUE; } else { - distance = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), unit); + GeoPoint point = fieldData.value(doc); + distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); } values[slot] = distance; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceFilter.java index 438755105be01..66ccdf8d2a9fa 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoDistanceFilter.java @@ -25,8 +25,9 @@ import org.elasticsearch.common.lucene.docset.GetDocSet; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.cache.field.data.FieldDataCache; -import org.elasticsearch.index.field.data.FieldDataType; -import org.elasticsearch.index.field.data.NumericFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; import java.io.IOException; @@ -43,23 +44,16 @@ public class GeoDistanceFilter extends Filter { private final GeoDistance geoDistance; - private final String latFieldName; - - private final String lonFieldName; - - private final FieldDataType fieldDataType; + private final String fieldName; private final FieldDataCache fieldDataCache; - public GeoDistanceFilter(double lat, double lon, double distance, GeoDistance geoDistance, String latFieldName, String lonFieldName, - FieldDataType fieldDataType, FieldDataCache fieldDataCache) { + public GeoDistanceFilter(double lat, double lon, double distance, GeoDistance geoDistance, String fieldName, FieldDataCache fieldDataCache) { this.lat = lat; this.lon = lon; this.distance = distance; this.geoDistance = geoDistance; - this.latFieldName = latFieldName; - this.lonFieldName = lonFieldName; - this.fieldDataType = fieldDataType; + this.fieldName = fieldName; this.fieldDataCache = fieldDataCache; } @@ -79,39 +73,34 @@ public GeoDistance geoDistance() { return geoDistance; } - public String latFieldName() { - return latFieldName; - } - - public String lonFieldName() { - return lonFieldName; + public String fieldName() { + return fieldName; } @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { - final NumericFieldData latFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, latFieldName); - final NumericFieldData lonFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, lonFieldName); + final GeoPointFieldData fieldData = (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, fieldName); return new GetDocSet(reader.maxDoc()) { @Override public boolean isCacheable() { return false; } @Override public boolean get(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return false; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - double d = geoDistance.calculate(lat, lon, lats[i], lons[i], DistanceUnit.MILES); + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); + for (GeoPoint point : points) { + double d = geoDistance.calculate(lat, lon, point.lat(), point.lon(), DistanceUnit.MILES); if (d < distance) { return true; } } return false; } else { - double d = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), DistanceUnit.MILES); + GeoPoint point = fieldData.value(doc); + double d = geoDistance.calculate(lat, lon, point.lat(), point.lon(), DistanceUnit.MILES); return d < distance; } } @@ -123,16 +112,13 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - GeoDistanceFilter that = (GeoDistanceFilter) o; + GeoDistanceFilter filter = (GeoDistanceFilter) o; - if (Double.compare(that.distance, distance) != 0) return false; - if (Double.compare(that.lat, lat) != 0) return false; - if (Double.compare(that.lon, lon) != 0) return false; - if (geoDistance != that.geoDistance) return false; - if (latFieldName != null ? !latFieldName.equals(that.latFieldName) : that.latFieldName != null) - return false; - if (lonFieldName != null ? !lonFieldName.equals(that.lonFieldName) : that.lonFieldName != null) - return false; + if (Double.compare(filter.distance, distance) != 0) return false; + if (Double.compare(filter.lat, lat) != 0) return false; + if (Double.compare(filter.lon, lon) != 0) return false; + if (fieldName != null ? !fieldName.equals(filter.fieldName) : filter.fieldName != null) return false; + if (geoDistance != filter.geoDistance) return false; return true; } @@ -148,8 +134,7 @@ public int hashCode() { temp = distance != +0.0d ? Double.doubleToLongBits(distance) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); result = 31 * result + (geoDistance != null ? geoDistance.hashCode() : 0); - result = 31 * result + (latFieldName != null ? latFieldName.hashCode() : 0); - result = 31 * result + (lonFieldName != null ? lonFieldName.hashCode() : 0); + result = 31 * result + (fieldName != null ? fieldName.hashCode() : 0); return result; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoHashUtils.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoHashUtils.java index da0cd7b15aa56..49aff6353a4a5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoHashUtils.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoHashUtils.java @@ -113,7 +113,9 @@ public static String encode(double latitude, double longitude, int precision) { } public static double[] decode(String geohash) { - return decode(geohash, new double[2]); + double[] ret = new double[2]; + decode(geohash, ret); + return ret; } /** @@ -122,7 +124,7 @@ public static double[] decode(String geohash) { * @param geohash Geohash to deocde * @return Array with the latitude at index 0, and longitude at index 1 */ - public static double[] decode(String geohash, double[] ret) { + public static void decode(String geohash, double[] ret) { // double[] latInterval = {-90.0, 90.0}; // double[] lngInterval = {-180.0, 180.0}; double latInterval0 = -90.0; @@ -162,6 +164,6 @@ public static double[] decode(String geohash, double[] ret) { // longitude = (lngInterval[0] + lngInterval[1]) / 2D; ret[1] = (lngInterval0 + lngInterval1) / 2D; - return ret; +// return ret; } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoPolygonFilter.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoPolygonFilter.java index 7da3dbcdaaac6..a198ded48b2d9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoPolygonFilter.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/geo/GeoPolygonFilter.java @@ -24,8 +24,9 @@ import org.apache.lucene.search.Filter; import org.elasticsearch.common.lucene.docset.GetDocSet; import org.elasticsearch.index.cache.field.data.FieldDataCache; -import org.elasticsearch.index.field.data.FieldDataType; -import org.elasticsearch.index.field.data.NumericFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; import java.io.IOException; @@ -36,19 +37,13 @@ public class GeoPolygonFilter extends Filter { private final Point[] points; - private final String latFieldName; - - private final String lonFieldName; - - private final FieldDataType fieldDataType; + private final String fieldName; private final FieldDataCache fieldDataCache; - public GeoPolygonFilter(Point[] points, String latFieldName, String lonFieldName, FieldDataType fieldDataType, FieldDataCache fieldDataCache) { + public GeoPolygonFilter(Point[] points, String fieldName, FieldDataCache fieldDataCache) { this.points = points; - this.latFieldName = latFieldName; - this.lonFieldName = lonFieldName; - this.fieldDataType = fieldDataType; + this.fieldName = fieldName; this.fieldDataCache = fieldDataCache; } @@ -56,36 +51,29 @@ public Point[] points() { return points; } - public String latFieldName() { - return latFieldName; - } - - public String lonFieldName() { - return lonFieldName; + public String fieldName() { + return this.fieldName; } @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { - final NumericFieldData latFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, latFieldName); - final NumericFieldData lonFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, lonFieldName); + final GeoPointFieldData fieldData = (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, fieldName); return new GetDocSet(reader.maxDoc()) { @Override public boolean get(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return false; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - if (pointInPolygon(points, lats[i], lons[i])) { + if (fieldData.multiValued()) { + GeoPoint[] docPoints = fieldData.values(doc); + for (GeoPoint docPoint : docPoints) { + if (pointInPolygon(points, docPoint.lat(), docPoint.lon())) { return true; } } } else { - double lat = latFieldData.doubleValue(doc); - double lon = lonFieldData.doubleValue(doc); - return pointInPolygon(points, lat, lon); + GeoPoint point = fieldData.value(doc); + return pointInPolygon(points, point.lat(), point.lon()); } return false; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java index 913c651956d8d..66631a9cb334e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/FieldData.java @@ -20,9 +20,7 @@ package org.elasticsearch.index.field.data; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.thread.ThreadLocals; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import java.io.IOException; @@ -89,8 +87,6 @@ public static interface StringValueInDocProc { */ public abstract FieldDataType type(); - public abstract FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed); - public static FieldData load(FieldDataType type, IndexReader reader, String fieldName) throws IOException { return type.load(reader, fieldName); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java index 94715c661d129..8e16cc5b9134f 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/doubles/DoubleFieldData.java @@ -21,9 +21,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.trove.TDoubleArrayList; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -44,10 +42,6 @@ protected DoubleFieldData(String fieldName, double[] values) { this.values = values; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new DoubleFieldDataComparator(numHits, field, fieldDataCache); - } - abstract public double value(int docId); abstract public double[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/FloatFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/FloatFieldData.java index b219da0af4b38..a346896899a64 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/FloatFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/floats/FloatFieldData.java @@ -21,9 +21,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.trove.TFloatArrayList; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -44,10 +42,6 @@ protected FloatFieldData(String fieldName, float[] values) { this.values = values; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new FloatFieldDataComparator(numHits, field, fieldDataCache); - } - abstract public float value(int docId); abstract public float[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java index 5ee38e9941535..b14d08a904050 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/ints/IntFieldData.java @@ -21,9 +21,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.trove.TIntArrayList; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -44,10 +42,6 @@ protected IntFieldData(String fieldName, int[] values) { this.values = values; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new IntFieldDataComparator(numHits, field, fieldDataCache); - } - abstract public int value(int docId); abstract public int[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java index 134c8b38a00f9..0e26adca109e0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/longs/LongFieldData.java @@ -21,11 +21,9 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.joda.time.MutableDateTime; import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.trove.TLongArrayList; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -53,10 +51,6 @@ protected LongFieldData(String fieldName, long[] values) { this.values = values; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new LongFieldDataComparator(numHits, field, fieldDataCache); - } - abstract public long value(int docId); abstract public long[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java index 0c13f5ba3cf94..8aaa477aaddcd 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/shorts/ShortFieldData.java @@ -21,9 +21,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.FieldCache; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.trove.TShortArrayList; -import org.elasticsearch.index.cache.field.data.FieldDataCache; import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.field.data.support.FieldDataLoader; @@ -44,10 +42,6 @@ protected ShortFieldData(String fieldName, short[] values) { this.values = values; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new ShortFieldDataComparator(numHits, field, fieldDataCache); - } - abstract public short value(int docId); abstract public short[] values(int docId); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java index a0d17da747cd2..4b9cdfe4092e4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/MultiValueStringFieldData.java @@ -19,10 +19,8 @@ package org.elasticsearch.index.field.data.strings; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.Strings; import org.elasticsearch.common.thread.ThreadLocals; -import org.elasticsearch.index.cache.field.data.FieldDataCache; /** * @author kimchy (shay.banon) @@ -49,10 +47,6 @@ public MultiValueStringFieldData(String fieldName, int[][] order, String[] value this.order = order; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new StringValFieldDataComparator(numHits, field, fieldDataCache); - } - @Override public boolean multiValued() { return true; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java index 98fdb97a54230..e58b792d95c92 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/field/data/strings/SingleValueStringFieldData.java @@ -19,10 +19,8 @@ package org.elasticsearch.index.field.data.strings; -import org.apache.lucene.search.FieldComparator; import org.elasticsearch.common.Strings; import org.elasticsearch.common.thread.ThreadLocals; -import org.elasticsearch.index.cache.field.data.FieldDataCache; /** * @author kimchy (shay.banon) @@ -43,10 +41,6 @@ public SingleValueStringFieldData(String fieldName, int[] order, String[] values this.order = order; } - @Override public FieldComparator newComparator(FieldDataCache fieldDataCache, int numHits, String field, int sortPos, boolean reversed) { - return new StringOrdValFieldDataComparator(numHits, field, sortPos, reversed, fieldDataCache); - } - int[] order() { return order; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapperParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapperParser.java index 43773b9f243a7..ae804881814a4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapperParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentDocumentMapperParser.java @@ -35,6 +35,7 @@ import org.elasticsearch.index.mapper.DocumentMapperParser; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.index.settings.IndexSettings; import javax.annotation.Nullable; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPoint.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPoint.java new file mode 100644 index 0000000000000..c3646857e0518 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPoint.java @@ -0,0 +1,61 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.elasticsearch.common.lucene.geo.GeoHashUtils; + +/** + * @author kimchy (shay.banon) + */ +public class GeoPoint { + + private final double lat; + + private final double lon; + + public GeoPoint(double lat, double lon) { + this.lat = lat; + this.lon = lon; + } + + public final double lat() { + return this.lat; + } + + public final double getLat() { + return this.lat; + } + + public final double lon() { + return this.lon; + } + + public final double getLon() { + return this.lon; + } + + public final String geohash() { + return GeoHashUtils.encode(lat, lon); + } + + public final String getGeohash() { + return GeoHashUtils.encode(lat, lon); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointDocFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointDocFieldData.java new file mode 100644 index 0000000000000..05bc80f3abc7e --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointDocFieldData.java @@ -0,0 +1,40 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.elasticsearch.index.field.data.DocFieldData; + +/** + * @author kimchy (shay.banon) + */ +public class GeoPointDocFieldData extends DocFieldData { + + public GeoPointDocFieldData(GeoPointFieldData fieldData) { + super(fieldData); + } + + public GeoPoint getValue() { + return fieldData.value(docId); + } + + public GeoPoint[] getValues() { + return fieldData.values(docId); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java new file mode 100644 index 0000000000000..a8d7f95f67632 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldData.java @@ -0,0 +1,110 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.apache.lucene.index.IndexReader; +import org.elasticsearch.common.lucene.geo.GeoHashUtils; +import org.elasticsearch.index.field.data.FieldData; +import org.elasticsearch.index.field.data.FieldDataType; +import org.elasticsearch.index.field.data.support.FieldDataLoader; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * @author kimchy (shay.banon) + */ +public abstract class GeoPointFieldData extends FieldData { + + public static final GeoPoint[] EMPTY_ARRAY = new GeoPoint[0]; + + protected final GeoPoint[] values; + + protected GeoPointFieldData(String fieldName, GeoPoint[] values) { + super(fieldName); + this.values = values; + } + + abstract public GeoPoint value(int docId); + + abstract public GeoPoint[] values(int docId); + + @Override public GeoPointDocFieldData docFieldData(int docId) { + return super.docFieldData(docId); + } + + @Override public String stringValue(int docId) { + return value(docId).geohash(); + } + + @Override protected GeoPointDocFieldData createFieldData() { + return new GeoPointDocFieldData(this); + } + + @Override public FieldDataType type() { + return GeoPointFieldDataType.TYPE; + } + + @Override public void forEachValue(StringValueProc proc) { + for (int i = 1; i < values.length; i++) { + proc.onValue(values[i].geohash()); + } + } + + public void forEachValue(ValueProc proc) { + for (int i = 1; i < values.length; i++) { + proc.onValue(values[i]); + } + } + + public static interface ValueProc { + void onValue(GeoPoint value); + } + + public static GeoPointFieldData load(IndexReader reader, String field) throws IOException { + return FieldDataLoader.load(reader, field, new StringTypeLoader()); + } + + static class StringTypeLoader extends FieldDataLoader.FreqsTypeLoader { + + private final ArrayList terms = new ArrayList(); + + private final double[] latlon = new double[2]; + + StringTypeLoader() { + super(); + // the first one indicates null value + terms.add(null); + } + + @Override public void collectTerm(String term) { + GeoHashUtils.decode(term, latlon); + terms.add(new GeoPoint(latlon[0], latlon[1])); + } + + @Override public GeoPointFieldData buildSingleValue(String field, int[] order) { + return new SingleValueGeoPointFieldData(field, order, terms.toArray(new GeoPoint[terms.size()])); + } + + @Override public GeoPointFieldData buildMultiValue(String field, int[][] order) { + return new MultiValueGeoPointFieldData(field, order, terms.toArray(new GeoPoint[terms.size()])); + } + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldDataType.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldDataType.java new file mode 100644 index 0000000000000..28451ad29d7df --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldDataType.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.search.FieldComparator; +import org.apache.lucene.search.FieldComparatorSource; +import org.elasticsearch.index.cache.field.data.FieldDataCache; +import org.elasticsearch.index.field.data.FieldDataType; +import org.elasticsearch.index.field.data.strings.StringOrdValFieldDataComparator; + +import java.io.IOException; + +/** + * @author kimchy (shay.banon) + */ +public class GeoPointFieldDataType implements FieldDataType { + + public static final GeoPointFieldDataType TYPE = new GeoPointFieldDataType(); + + @Override public Class fieldDataClass() { + return GeoPointFieldData.class; + } + + @Override public FieldComparatorSource newFieldComparatorSource(final FieldDataCache cache) { + return new FieldComparatorSource() { + @Override public FieldComparator newComparator(String fieldname, int numHits, int sortPos, boolean reversed) throws IOException { + return new StringOrdValFieldDataComparator(numHits, fieldname, sortPos, reversed, cache); + } + }; + } + + @Override public GeoPointFieldData load(IndexReader reader, String fieldName) throws IOException { + return GeoPointFieldData.load(reader, fieldName); + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/GeoPointFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldMapper.java similarity index 79% rename from modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/GeoPointFieldMapper.java rename to modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldMapper.java index eef38236341f4..a6c3d3ef52c90 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/GeoPointFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/GeoPointFieldMapper.java @@ -17,18 +17,20 @@ * under the License. */ -package org.elasticsearch.index.mapper.xcontent; +package org.elasticsearch.index.mapper.xcontent.geo; import org.apache.lucene.document.Field; -import org.elasticsearch.ElasticSearchIllegalArgumentException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.lucene.geo.GeoHashUtils; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; +import org.elasticsearch.index.analysis.NamedAnalyzer; +import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.mapper.FieldMapperListener; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MergeMappingException; +import org.elasticsearch.index.mapper.xcontent.*; import java.io.IOException; import java.util.Map; @@ -70,15 +72,11 @@ public static class Builder extends XContentMapper.Builder { + + protected String nullValue = Defaults.NULL_VALUE; + + public Builder(String name) { + super(name); + builder = this; + } + + public Builder nullValue(String nullValue) { + this.nullValue = nullValue; + return this; + } + + @Override public Builder includeInAll(Boolean includeInAll) { + this.includeInAll = includeInAll; + return this; + } + + @Override public GeoHashFieldMapper build(BuilderContext context) { + GeoHashFieldMapper fieldMapper = new GeoHashFieldMapper(buildNames(context), + index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, + indexAnalyzer, searchAnalyzer); + fieldMapper.includeInAll(includeInAll); + return fieldMapper; + } + } + + public GeoHashFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector, float boost, boolean omitNorms, boolean omitTermFreqAndPositions, String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) { + super(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer, searchAnalyzer); + } + + @Override public FieldDataType fieldDataType() { + return GeoPointFieldDataType.TYPE; + } + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java new file mode 100644 index 0000000000000..8b4ddd88ded6e --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java @@ -0,0 +1,91 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.elasticsearch.common.thread.ThreadLocals; + +/** + * @author kimchy (shay.banon) + */ +public class MultiValueGeoPointFieldData extends GeoPointFieldData { + + private static final int VALUE_CACHE_SIZE = 100; + + private static ThreadLocal> valuesCache = new ThreadLocal>() { + @Override protected ThreadLocals.CleanableValue initialValue() { + GeoPoint[][] value = new GeoPoint[VALUE_CACHE_SIZE][]; + for (int i = 0; i < value.length; i++) { + value[i] = new GeoPoint[i]; + } + return new ThreadLocals.CleanableValue(value); + } + }; + + // order with value 0 indicates no value + private final int[][] order; + + public MultiValueGeoPointFieldData(String fieldName, int[][] order, GeoPoint[] values) { + super(fieldName, values); + this.order = order; + } + + @Override public boolean multiValued() { + return true; + } + + @Override public boolean hasValue(int docId) { + return order[docId] != null; + } + + @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { + int[] docOrders = order[docId]; + if (docOrders == null) { + return; + } + for (int docOrder : docOrders) { + proc.onValue(docId, values[docOrder].geohash()); + } + } + + @Override public GeoPoint value(int docId) { + int[] docOrders = order[docId]; + if (docOrders == null) { + return null; + } + return values[docOrders[0]]; + } + + @Override public GeoPoint[] values(int docId) { + int[] docOrders = order[docId]; + if (docOrders == null) { + return EMPTY_ARRAY; + } + GeoPoint[] points; + if (docOrders.length < VALUE_CACHE_SIZE) { + points = valuesCache.get().get()[docOrders.length]; + } else { + points = new GeoPoint[docOrders.length]; + } + for (int i = 0; i < docOrders.length; i++) { + points[i] = values[docOrders[i]]; + } + return points; + } +} \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java new file mode 100644 index 0000000000000..d1afdc3639c4c --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/SingleValueGeoPointFieldData.java @@ -0,0 +1,80 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geo; + +import org.elasticsearch.common.thread.ThreadLocals; + +/** + * @author kimchy (shay.banon) + */ +public class SingleValueGeoPointFieldData extends GeoPointFieldData { + + private static ThreadLocal> valuesCache = new ThreadLocal>() { + @Override protected ThreadLocals.CleanableValue initialValue() { + return new ThreadLocals.CleanableValue(new GeoPoint[1]); + } + }; + + // order with value 0 indicates no value + private final int[] order; + + public SingleValueGeoPointFieldData(String fieldName, int[] order, GeoPoint[] values) { + super(fieldName, values); + this.order = order; + } + + int[] order() { + return order; + } + + GeoPoint[] values() { + return this.values; + } + + @Override public boolean multiValued() { + return false; + } + + @Override public boolean hasValue(int docId) { + return order[docId] != 0; + } + + @Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) { + int loc = order[docId]; + if (loc == 0) { + return; + } + proc.onValue(docId, values[loc].geohash()); + } + + @Override public GeoPoint value(int docId) { + return values[order[docId]]; + } + + @Override public GeoPoint[] values(int docId) { + int loc = order[docId]; + if (loc == 0) { + return EMPTY_ARRAY; + } + GeoPoint[] ret = valuesCache.get().get(); + ret[0] = values[loc]; + return ret; + } +} diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoBoundingBoxFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoBoundingBoxFilterParser.java index 0819635278c5b..665fbf5f0bef8 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoBoundingBoxFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoBoundingBoxFilterParser.java @@ -29,7 +29,8 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; @@ -55,8 +56,7 @@ public class GeoBoundingBoxFilterParser extends AbstractIndexComponent implement @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - String latFieldName = null; - String lonFieldName = null; + String fieldName = null; GeoBoundingBoxFilter.Point topLeft = new GeoBoundingBoxFilter.Point(); GeoBoundingBoxFilter.Point bottomRight = new GeoBoundingBoxFilter.Point(); @@ -67,8 +67,7 @@ public class GeoBoundingBoxFilterParser extends AbstractIndexComponent implement if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - latFieldName = currentFieldName + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = currentFieldName + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -117,8 +116,7 @@ public class GeoBoundingBoxFilterParser extends AbstractIndexComponent implement } } else if (token.isValue()) { if ("field".equals(currentFieldName)) { - latFieldName = parser.text() + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = parser.text() + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = parser.text(); } else { GeoBoundingBoxFilter.Point point = null; if ("top_left".equals(currentFieldName) || "topLeft".equals(currentFieldName)) { @@ -151,21 +149,18 @@ public class GeoBoundingBoxFilterParser extends AbstractIndexComponent implement MapperService mapperService = parseContext.mapperService(); - FieldMapper mapper = mapperService.smartNameFieldMapper(latFieldName); + FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName); if (mapper == null) { - throw new QueryParsingException(index, "failed to find lat field [" + latFieldName + "]"); + throw new QueryParsingException(index, "failed to find geo_point field [" + fieldName + "]"); } - latFieldName = mapper.names().indexName(); - - mapper = mapperService.smartNameFieldMapper(lonFieldName); - if (mapper == null) { - throw new QueryParsingException(index, "failed to find lon field [" + lonFieldName + "]"); + if (mapper.fieldDataType() != GeoPointFieldDataType.TYPE) { + throw new QueryParsingException(index, "field [" + fieldName + "] is not a geo_point field"); } - lonFieldName = mapper.names().indexName(); + fieldName = mapper.names().indexName(); - Filter filter = new GeoBoundingBoxFilter(topLeft, bottomRight, latFieldName, lonFieldName, mapper.fieldDataType(), parseContext.indexCache().fieldData()); - filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(latFieldName), parseContext); + Filter filter = new GeoBoundingBoxFilter(topLeft, bottomRight, fieldName, parseContext.indexCache().fieldData()); + filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(fieldName), parseContext); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java index cb701bb53c8a9..c95c734f49688 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java @@ -31,7 +31,8 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; @@ -70,8 +71,7 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X String currentFieldName = null; double lat = 0; double lon = 0; - String latFieldName = null; - String lonFieldName = null; + String fieldName = null; double distance = 0; DistanceUnit unit = null; GeoDistance geoDistance = GeoDistance.ARC; @@ -86,13 +86,11 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { } - latFieldName = currentFieldName + "." + GeoPointFieldMapper.Names.LAT; - lonFieldName = currentFieldName + "." + GeoPointFieldMapper.Names.LON; + fieldName = currentFieldName; } else if (token == XContentParser.Token.START_OBJECT) { // the json in the format of -> field : { lat : 30, lon : 12 } String currentName = parser.currentName(); - latFieldName = currentFieldName + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = currentFieldName + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentName = parser.currentName(); @@ -121,16 +119,15 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X geoDistance = GeoDistance.fromString(parser.text()); } else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LAT_SUFFIX)) { lat = parser.doubleValue(); - latFieldName = currentFieldName; + fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.LAT_SUFFIX.length()); } else if (currentFieldName.endsWith(GeoPointFieldMapper.Names.LON_SUFFIX)) { lon = parser.doubleValue(); - lonFieldName = currentFieldName; + 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]; - latFieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length()) + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length()) + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = currentFieldName.substring(0, currentFieldName.length() - GeoPointFieldMapper.Names.GEOHASH_SUFFIX.length()); } else if ("_name".equals(currentFieldName)) { filterName = parser.text(); } else { @@ -145,9 +142,7 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X lat = values[0]; lon = values[1]; } - - latFieldName = currentFieldName + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = currentFieldName + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = currentFieldName; } } } @@ -157,20 +152,17 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X } MapperService mapperService = parseContext.mapperService(); - FieldMapper mapper = mapperService.smartNameFieldMapper(latFieldName); + FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName); if (mapper == null) { - throw new QueryParsingException(index, "failed to find lat field [" + latFieldName + "]"); + throw new QueryParsingException(index, "failed to find geo_point field [" + fieldName + "]"); } - latFieldName = mapper.names().indexName(); - - mapper = mapperService.smartNameFieldMapper(lonFieldName); - if (mapper == null) { - throw new QueryParsingException(index, "failed to find lon field [" + lonFieldName + "]"); + if (mapper.fieldDataType() != GeoPointFieldDataType.TYPE) { + throw new QueryParsingException(index, "field [" + fieldName + "] is not a geo_point field"); } - lonFieldName = mapper.names().indexName(); + fieldName = mapper.names().indexName(); - Filter filter = new GeoDistanceFilter(lat, lon, distance, geoDistance, latFieldName, lonFieldName, mapper.fieldDataType(), parseContext.indexCache().fieldData()); - filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(latFieldName), parseContext); + Filter filter = new GeoDistanceFilter(lat, lon, distance, geoDistance, fieldName, parseContext.indexCache().fieldData()); + filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(fieldName), parseContext); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoPolygonFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoPolygonFilterParser.java index c4cfae68bcd6c..043841d05f4d2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoPolygonFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoPolygonFilterParser.java @@ -30,7 +30,8 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.settings.IndexSettings; @@ -68,8 +69,7 @@ public class GeoPolygonFilterParser extends AbstractIndexComponent implements XC @Override public Filter parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); - String latFieldName = null; - String lonFieldName = null; + String fieldName = null; List points = Lists.newArrayList(); @@ -81,8 +81,7 @@ public class GeoPolygonFilterParser extends AbstractIndexComponent implements XC if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { - latFieldName = currentFieldName + GeoPointFieldMapper.Names.LAT_SUFFIX; - lonFieldName = currentFieldName + GeoPointFieldMapper.Names.LON_SUFFIX; + fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -150,20 +149,17 @@ public class GeoPolygonFilterParser extends AbstractIndexComponent implements XC } MapperService mapperService = parseContext.mapperService(); - FieldMapper mapper = mapperService.smartNameFieldMapper(latFieldName); + FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName); if (mapper == null) { - throw new QueryParsingException(index, "failed to find lat field [" + latFieldName + "]"); + throw new QueryParsingException(index, "failed to find geo_point field [" + fieldName + "]"); } - latFieldName = mapper.names().indexName(); - - mapper = mapperService.smartNameFieldMapper(lonFieldName); - if (mapper == null) { - throw new QueryParsingException(index, "failed to find lon field [" + lonFieldName + "]"); + if (mapper.fieldDataType() != GeoPointFieldDataType.TYPE) { + throw new QueryParsingException(index, "field [" + fieldName + "] is not a geo_point field"); } - lonFieldName = mapper.names().indexName(); + fieldName = mapper.names().indexName(); - Filter filter = new GeoPolygonFilter(points.toArray(new GeoPolygonFilter.Point[points.size()]), latFieldName, lonFieldName, mapper.fieldDataType(), parseContext.indexCache().fieldData()); - filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(latFieldName), parseContext); + Filter filter = new GeoPolygonFilter(points.toArray(new GeoPolygonFilter.Point[points.size()]), fieldName, parseContext.indexCache().fieldData()); + filter = wrapSmartNameFilter(filter, parseContext.smartFieldMappers(fieldName), parseContext); if (filterName != null) { parseContext.addNamedFilter(filterName, filter); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java index e36b5b8f8a950..a331c7c75cc61 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollector.java @@ -23,11 +23,10 @@ import org.elasticsearch.common.lucene.geo.GeoDistance; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.cache.field.data.FieldDataCache; -import org.elasticsearch.index.field.data.FieldDataType; -import org.elasticsearch.index.field.data.NumericFieldData; -import org.elasticsearch.index.mapper.FieldMapper; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldData; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldDataType; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.FacetPhaseExecutionException; import org.elasticsearch.search.facets.support.AbstractFacetCollector; @@ -42,9 +41,7 @@ public class GeoDistanceFacetCollector extends AbstractFacetCollector { protected final String fieldName; - protected final String indexLatFieldName; - - protected final String indexLonFieldName; + protected final String indexFieldName; protected final double lat; @@ -56,11 +53,7 @@ public class GeoDistanceFacetCollector extends AbstractFacetCollector { protected final FieldDataCache fieldDataCache; - protected final FieldDataType fieldDataType; - - protected NumericFieldData latFieldData; - - protected NumericFieldData lonFieldData; + protected GeoPointFieldData fieldData; protected final GeoDistanceFacet.Entry[] entries; @@ -75,41 +68,35 @@ public GeoDistanceFacetCollector(String facetName, String fieldName, double lat, this.geoDistance = geoDistance; this.fieldDataCache = context.fieldDataCache(); - MapperService.SmartNameFieldMappers smartMappers = context.mapperService().smartName(fieldName + GeoPointFieldMapper.Names.LAT_SUFFIX); + MapperService.SmartNameFieldMappers smartMappers = context.mapperService().smartName(fieldName); if (smartMappers == null || !smartMappers.hasMapper()) { throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldName + "]"); } + if (smartMappers.mapper().fieldDataType() != GeoPointFieldDataType.TYPE) { + throw new FacetPhaseExecutionException(facetName, "field [" + fieldName + "] is not a geo_point field"); + } // add type filter if there is exact doc mapper associated with it if (smartMappers.hasDocMapper()) { setFilter(context.filterCache().cache(smartMappers.docMapper().typeFilter())); } - this.indexLatFieldName = smartMappers.mapper().names().indexName(); - - FieldMapper mapper = context.mapperService().smartNameFieldMapper(fieldName + GeoPointFieldMapper.Names.LON_SUFFIX); - if (mapper == null) { - throw new FacetPhaseExecutionException(facetName, "No mapping found for field [" + fieldName + "]"); - } - this.indexLonFieldName = mapper.names().indexName(); - this.fieldDataType = mapper.fieldDataType(); + this.indexFieldName = smartMappers.mapper().names().indexName(); } @Override protected void doSetNextReader(IndexReader reader, int docBase) throws IOException { - latFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, indexLatFieldName); - lonFieldData = (NumericFieldData) fieldDataCache.cache(fieldDataType, reader, indexLonFieldName); + fieldData = (GeoPointFieldData) fieldDataCache.cache(GeoPointFieldDataType.TYPE, reader, indexFieldName); } @Override protected void doCollect(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - double distance = geoDistance.calculate(lat, lon, lats[i], lons[i], unit); + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); + for (GeoPoint point : points) { + double distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; @@ -118,7 +105,8 @@ public GeoDistanceFacetCollector(String facetName, String fieldName, double lat, } } } else { - double distance = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), unit); + GeoPoint point = fieldData.value(doc); + double distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollectorParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollectorParser.java index e5dffe5bfdace..6c2f198f75679 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollectorParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/GeoDistanceFacetCollectorParser.java @@ -25,7 +25,7 @@ import org.elasticsearch.common.thread.ThreadLocals; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.search.facets.FacetPhaseExecutionException; import org.elasticsearch.search.facets.collector.FacetCollector; import org.elasticsearch.search.facets.collector.FacetCollectorParser; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ScriptGeoDistanceFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ScriptGeoDistanceFacetCollector.java index 09efec8cafd4b..14237bc198f04 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ScriptGeoDistanceFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ScriptGeoDistanceFacetCollector.java @@ -22,6 +22,7 @@ import org.apache.lucene.index.IndexReader; import org.elasticsearch.common.lucene.geo.GeoDistance; import org.elasticsearch.common.unit.DistanceUnit; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; import org.elasticsearch.script.search.SearchScript; import org.elasticsearch.search.internal.SearchContext; @@ -49,17 +50,16 @@ public ScriptGeoDistanceFacetCollector(String facetName, String fieldName, doubl } @Override protected void doCollect(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return; } double value = ((Number) script.execute(doc)).doubleValue(); - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); - for (int i = 0; i < lats.length; i++) { - double distance = geoDistance.calculate(lat, lon, lats[i], lons[i], unit); + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); + for (GeoPoint point : points) { + double distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; @@ -68,7 +68,8 @@ public ScriptGeoDistanceFacetCollector(String facetName, String fieldName, doubl } } } else { - double distance = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), unit); + GeoPoint point = fieldData.value(doc); + double distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ValueGeoDistanceFacetCollector.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ValueGeoDistanceFacetCollector.java index e8b4809da6398..035bf8c309758 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ValueGeoDistanceFacetCollector.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/geodistance/ValueGeoDistanceFacetCollector.java @@ -25,6 +25,7 @@ import org.elasticsearch.index.field.data.FieldDataType; import org.elasticsearch.index.field.data.NumericFieldData; import org.elasticsearch.index.mapper.FieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPoint; import org.elasticsearch.search.facets.Facet; import org.elasticsearch.search.facets.FacetPhaseExecutionException; import org.elasticsearch.search.internal.SearchContext; @@ -63,16 +64,15 @@ public ValueGeoDistanceFacetCollector(String facetName, String fieldName, double } @Override protected void doCollect(int doc) throws IOException { - if (!latFieldData.hasValue(doc) || !lonFieldData.hasValue(doc)) { + if (!fieldData.hasValue(doc)) { return; } - if (latFieldData.multiValued()) { - double[] lats = latFieldData.doubleValues(doc); - double[] lons = latFieldData.doubleValues(doc); + if (fieldData.multiValued()) { + GeoPoint[] points = fieldData.values(doc); double[] values = valueFieldData.multiValued() ? valueFieldData.doubleValues(doc) : null; - for (int i = 0; i < lats.length; i++) { - double distance = geoDistance.calculate(lat, lon, lats[i], lons[i], unit); + for (int i = 0; i < points.length; i++) { + double distance = geoDistance.calculate(lat, lon, points[i].lat(), points[i].lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; @@ -87,7 +87,8 @@ public ValueGeoDistanceFacetCollector(String facetName, String fieldName, double } } } else { - double distance = geoDistance.calculate(lat, lon, latFieldData.doubleValue(doc), lonFieldData.doubleValue(doc), unit); + GeoPoint point = fieldData.value(doc); + double distance = geoDistance.calculate(lat, lon, point.lat(), point.lon(), unit); for (GeoDistanceFacet.Entry entry : entries) { if (distance >= entry.getFrom() && distance < entry.getTo()) { entry.count++; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java index 1278f0fc4edda..94f1ca0394fec 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/sort/GeoDistanceSortParser.java @@ -25,7 +25,7 @@ import org.elasticsearch.common.lucene.geo.GeoHashUtils; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.mapper.xcontent.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.xcontent.geo.GeoPointFieldMapper; import org.elasticsearch.search.internal.SearchContext; /** diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/GeohashMappingGeoPointTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/GeohashMappingGeoPointTests.java index a172a1abb318c..026a49b550f01 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/GeohashMappingGeoPointTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/GeohashMappingGeoPointTests.java @@ -36,7 +36,7 @@ public class GeohashMappingGeoPointTests { @Test public void testLatLonValues() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).field("lat_lon", false).endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", false).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -49,12 +49,12 @@ public class GeohashMappingGeoPointTests { MatcherAssert.assertThat(doc.doc().getField("point.lat"), nullValue()); MatcherAssert.assertThat(doc.doc().getField("point.lon"), nullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); + MatcherAssert.assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonInOneValue() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).field("lat_lon", false).endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", false).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -67,12 +67,12 @@ public class GeohashMappingGeoPointTests { MatcherAssert.assertThat(doc.doc().getField("point.lat"), nullValue()); MatcherAssert.assertThat(doc.doc().getField("point.lon"), nullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); + MatcherAssert.assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testGeoHashValue() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).field("lat_lon", false).endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -85,6 +85,6 @@ public class GeohashMappingGeoPointTests { MatcherAssert.assertThat(doc.doc().getField("point.lat"), nullValue()); MatcherAssert.assertThat(doc.doc().getField("point.lon"), nullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); + MatcherAssert.assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonAndGeohashMappingGeoPointTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonAndGeohashMappingGeoPointTests.java deleted file mode 100644 index 949ade52b872d..0000000000000 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonAndGeohashMappingGeoPointTests.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Licensed to Elastic Search and Shay Banon under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. Elastic Search 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.mapper.xcontent.geopoint; - -import org.elasticsearch.common.lucene.geo.GeoHashUtils; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.mapper.xcontent.MapperTests; -import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper; -import org.hamcrest.MatcherAssert; -import org.testng.annotations.Test; - -import static org.hamcrest.Matchers.*; - -/** - * @author kimchy (shay.banon) - */ -public class LatLonAndGeohashMappingGeoPointTests { - - @Test public void testLatLonValues() throws Exception { - String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).endObject().endObject() - .endObject().endObject().string(); - - XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); - - ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() - .startObject() - .startObject("point").field("lat", 1.2).field("lon", 1.3).endObject() - .endObject() - .copiedBytes()); - - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); - } - - @Test public void testLatLonInOneValue() throws Exception { - String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).endObject().endObject() - .endObject().endObject().string(); - - XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); - - ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() - .startObject() - .field("point", "1.2,1.3") - .endObject() - .copiedBytes()); - - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); - } - - @Test public void testGeoHashValue() throws Exception { - String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("geohash", true).endObject().endObject() - .endObject().endObject().string(); - - XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); - - ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() - .startObject() - .field("point", GeoHashUtils.encode(1.2, 1.3)) - .endObject() - .copiedBytes()); - - MatcherAssert.assertThat(doc.doc().getField("point.lat"), notNullValue()); - MatcherAssert.assertThat(doc.doc().getField("point.lon"), notNullValue()); - MatcherAssert.assertThat(doc.doc().get("point.geohash"), equalTo(GeoHashUtils.encode(1.2, 1.3))); - } -} diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java index eaf1c9dbae507..f3bbac7cc6f9a 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/mapper/xcontent/geopoint/LatLonMappingGeoPointTests.java @@ -37,7 +37,7 @@ public class LatLonMappingGeoPointTests { @Test public void testLatLonValues() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -52,12 +52,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat").getBinaryValue(), nullValue()); assertThat(doc.doc().getField("point.lon"), notNullValue()); assertThat(doc.doc().getField("point.lon").getBinaryValue(), nullValue()); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonValuesStored() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -72,12 +72,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getField("point.lon"), notNullValue()); assertThat(doc.doc().getField("point.lon").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testArrayLatLonValues() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -95,15 +95,15 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getFields("point.lon").length, equalTo(2)); assertThat(doc.doc().getFields("point.lat")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getFields("point.lon")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getFields("point")[0].stringValue(), equalTo(GeoHashUtils.encode(1.2, 1.3))); assertThat(doc.doc().getFields("point.lat")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.4))); assertThat(doc.doc().getFields("point.lon")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.5))); - - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getFields("point")[1].stringValue(), equalTo(GeoHashUtils.encode(1.4, 1.5))); } @Test public void testLatLonInOneValue() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -116,12 +116,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat"), notNullValue()); assertThat(doc.doc().getField("point.lon"), notNullValue()); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonInOneValueStored() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -136,12 +136,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getField("point.lon"), notNullValue()); assertThat(doc.doc().getField("point.lon").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonInOneValueArray() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -159,15 +159,15 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getFields("point.lon").length, equalTo(2)); assertThat(doc.doc().getFields("point.lat")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getFields("point.lon")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getFields("point")[0].stringValue(), equalTo(GeoHashUtils.encode(1.2, 1.3))); assertThat(doc.doc().getFields("point.lat")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.4))); assertThat(doc.doc().getFields("point.lon")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.5))); - - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getFields("point")[1].stringValue(), equalTo(GeoHashUtils.encode(1.4, 1.5))); } @Test public void testGeoHashValue() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -180,12 +180,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat"), notNullValue()); assertThat(doc.doc().getField("point.lon"), notNullValue()); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonArray() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -200,12 +200,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat").getBinaryValue(), nullValue()); assertThat(doc.doc().getField("point.lon"), notNullValue()); assertThat(doc.doc().getField("point.lon").getBinaryValue(), nullValue()); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonArrayStored() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -220,12 +220,12 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getField("point.lat").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getField("point.lon"), notNullValue()); assertThat(doc.doc().getField("point.lon").getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().get("point"), equalTo(GeoHashUtils.encode(1.2, 1.3))); } @Test public void testLatLonArrayArrayStored() throws Exception { String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") - .startObject("properties").startObject("point").field("type", "geo_point").field("store", "yes").endObject().endObject() + .startObject("properties").startObject("point").field("type", "geo_point").field("lat_lon", true).field("store", "yes").endObject().endObject() .endObject().endObject().string(); XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); @@ -243,9 +243,9 @@ public class LatLonMappingGeoPointTests { assertThat(doc.doc().getFields("point.lon").length, equalTo(2)); assertThat(doc.doc().getFields("point.lat")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.2))); assertThat(doc.doc().getFields("point.lon")[0].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.3))); + assertThat(doc.doc().getFields("point")[0].stringValue(), equalTo(GeoHashUtils.encode(1.2, 1.3))); assertThat(doc.doc().getFields("point.lat")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.4))); assertThat(doc.doc().getFields("point.lon")[1].getBinaryValue(), equalTo(Numbers.doubleToBytes(1.5))); - - assertThat(doc.doc().getField("point.geohash"), nullValue()); + assertThat(doc.doc().getFields("point")[1].stringValue(), equalTo(GeoHashUtils.encode(1.4, 1.5))); } } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java index ccaad8f43db2a..428d08f370d68 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/SimpleIndexQueryParserTests.java @@ -90,6 +90,8 @@ public class SimpleIndexQueryParserTests { new IndexNameModule(index) ).createInjector(); + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/query/xcontent/mapping.json"); + injector.getInstance(MapperService.class).add("person", mapping); injector.getInstance(MapperService.class).type("person").parse(copyToBytesFromClasspath("/org/elasticsearch/index/query/xcontent/data.json")); this.queryParser = injector.getInstance(IndexQueryParserService.class); } @@ -1130,8 +1132,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); GeoDistanceFilter filter = (GeoDistanceFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + assertThat(filter.fieldName(), equalTo("location")); assertThat(filter.lat(), closeTo(40, 0.00001)); assertThat(filter.lon(), closeTo(-70, 0.00001)); assertThat(filter.distance(), closeTo(12, 0.00001)); @@ -1144,8 +1145,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoDistanceFilter filter = (GeoDistanceFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + assertThat(filter.fieldName(), equalTo("location")); assertThat(filter.lat(), closeTo(40, 0.00001)); assertThat(filter.lon(), closeTo(-70, 0.00001)); assertThat(filter.distance(), closeTo(12, 0.00001)); @@ -1158,8 +1158,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoDistanceFilter filter = (GeoDistanceFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + assertThat(filter.fieldName(), equalTo("location")); assertThat(filter.lat(), closeTo(40, 0.00001)); assertThat(filter.lon(), closeTo(-70, 0.00001)); assertThat(filter.distance(), closeTo(12, 0.00001)); @@ -1172,8 +1171,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoDistanceFilter filter = (GeoDistanceFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + assertThat(filter.fieldName(), equalTo("location")); assertThat(filter.lat(), closeTo(40, 0.00001)); assertThat(filter.lon(), closeTo(-70, 0.00001)); assertThat(filter.distance(), closeTo(12, 0.00001)); @@ -1186,8 +1184,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoDistanceFilter filter = (GeoDistanceFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + assertThat(filter.fieldName(), equalTo("location")); assertThat(filter.lat(), closeTo(40, 0.00001)); assertThat(filter.lon(), closeTo(-70, 0.00001)); assertThat(filter.distance(), closeTo(12, 0.00001)); @@ -1201,8 +1198,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery.namedFilters().containsKey("test"), equalTo(true)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); GeoBoundingBoxFilter filter = (GeoBoundingBoxFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1217,8 +1213,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoBoundingBoxFilter filter = (GeoBoundingBoxFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1232,8 +1227,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoBoundingBoxFilter filter = (GeoBoundingBoxFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1247,8 +1241,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoBoundingBoxFilter filter = (GeoBoundingBoxFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1262,8 +1255,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoBoundingBoxFilter filter = (GeoBoundingBoxFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1278,8 +1270,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery.query(), instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery.query(); GeoPolygonFilter filter = (GeoPolygonFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1296,8 +1287,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoPolygonFilter filter = (GeoPolygonFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1314,8 +1304,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoPolygonFilter filter = (GeoPolygonFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1332,8 +1321,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoPolygonFilter filter = (GeoPolygonFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); @@ -1350,8 +1338,7 @@ private XContentIndexQueryParser queryParser() throws IOException { assertThat(parsedQuery, instanceOf(FilteredQuery.class)); FilteredQuery filteredQuery = (FilteredQuery) parsedQuery; GeoPolygonFilter filter = (GeoPolygonFilter) filteredQuery.getFilter(); - assertThat(filter.latFieldName(), equalTo("location.lat")); - assertThat(filter.lonFieldName(), equalTo("location.lon")); + 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)); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/mapping.json b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/mapping.json new file mode 100644 index 0000000000000..168a09ba717fb --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/query/xcontent/mapping.json @@ -0,0 +1,7 @@ +{ + "person" : { + "properties" : { + "location" : { "type" : "geo_point" } + } + } +} \ No newline at end of file diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoBoundingBoxTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoBoundingBoxTests.java index d10744e1d1519..437d3a0f06f5d 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoBoundingBoxTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoBoundingBoxTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchHit; import org.elasticsearch.test.integration.AbstractNodesTests; import org.testng.annotations.AfterClass; @@ -62,7 +63,10 @@ protected Client getClient() { } catch (Exception e) { // ignore } - client.admin().indices().prepareCreate("test").execute().actionGet(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location").field("type", "geo_point").field("lat_lon", true).endObject().endObject() + .endObject().endObject().string(); + client.admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() @@ -125,7 +129,10 @@ protected Client getClient() { } catch (Exception e) { // ignore } - client.admin().indices().prepareCreate("test").setSettings(settingsBuilder().put("number_of_shards", "1")).execute().actionGet(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location").field("type", "geo_point").field("lat_lon", true).endObject().endObject() + .endObject().endObject().string(); + client.admin().indices().prepareCreate("test").addMapping("type1", mapping).setSettings(settingsBuilder().put("number_of_shards", "1")).execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceFacetTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceFacetTests.java index ecedc5f8884c4..f54b244b8f4d1 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceFacetTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceFacetTests.java @@ -22,6 +22,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.unit.DistanceUnit; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.facets.geodistance.GeoDistanceFacet; import org.elasticsearch.test.integration.AbstractNodesTests; import org.testng.annotations.AfterClass; @@ -62,7 +63,10 @@ protected Client getClient() { } catch (Exception e) { // ignore } - client.admin().indices().prepareCreate("test").execute().actionGet(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location").field("type", "geo_point").field("lat_lon", true).endObject().endObject() + .endObject().endObject().string(); + client.admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); // to NY: 0 diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceTests.java index a0b8ceabbba30..4c5e88082459f 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/geo/GeoDistanceTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; @@ -63,7 +64,10 @@ protected Client getClient() { } catch (Exception e) { // ignore } - client.admin().indices().prepareCreate("test").execute().actionGet(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location").field("type", "geo_point").field("lat_lon", true).endObject().endObject() + .endObject().endObject().string(); + client.admin().indices().prepareCreate("test").addMapping("type1", mapping).execute().actionGet(); client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet(); client.prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject() diff --git a/plugins/river/twitter/src/main/java/org/elasticsearch/river/twitter/TwitterRiver.java b/plugins/river/twitter/src/main/java/org/elasticsearch/river/twitter/TwitterRiver.java index 7d0383d702b4b..046bd29902d6a 100644 --- a/plugins/river/twitter/src/main/java/org/elasticsearch/river/twitter/TwitterRiver.java +++ b/plugins/river/twitter/src/main/java/org/elasticsearch/river/twitter/TwitterRiver.java @@ -106,7 +106,10 @@ public class TwitterRiver extends AbstractRiverComponent implements River { @Override public void start() { logger.info("starting twitter stream"); try { - client.admin().indices().prepareCreate(indexName).execute().actionGet(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject(typeName) + .startObject("properties").startObject("location").field("type", "geo_point").endObject().endObject() + .endObject().endObject().string(); + client.admin().indices().prepareCreate(indexName).addMapping(typeName, mapping).execute().actionGet(); } catch (Exception e) { if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) { // that's fine