|
|
@@ -19,6 +19,7 @@
|
|
|
|
|
|
package org.elasticsearch.index.fielddata;
|
|
|
|
|
|
+import org.elasticsearch.index.fielddata.ScriptDocValues.GeoPoints;
|
|
|
import org.elasticsearch.common.geo.GeoPoint;
|
|
|
import org.elasticsearch.common.geo.GeoUtils;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
|
@@ -28,31 +29,30 @@ import java.util.Arrays;
|
|
|
|
|
|
public class ScriptDocValuesGeoPointsTests extends ESTestCase {
|
|
|
|
|
|
- private static MultiGeoPointValues wrap(final GeoPoint... points) {
|
|
|
+ private static MultiGeoPointValues wrap(GeoPoint[][] points) {
|
|
|
return new MultiGeoPointValues() {
|
|
|
- int docID = -1;
|
|
|
+ GeoPoint[] current;
|
|
|
int i;
|
|
|
|
|
|
@Override
|
|
|
public GeoPoint nextValue() {
|
|
|
- if (docID != 0) {
|
|
|
- fail();
|
|
|
- }
|
|
|
- return points[i++];
|
|
|
+ return current[i++];
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean advanceExact(int docId) {
|
|
|
- docID = docId;
|
|
|
- return points.length > 0;
|
|
|
+ if (docId < points.length) {
|
|
|
+ current = points[docId];
|
|
|
+ } else {
|
|
|
+ current = new GeoPoint[0];
|
|
|
+ }
|
|
|
+ i = 0;
|
|
|
+ return current.length > 0;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int docValueCount() {
|
|
|
- if (docID != 0) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return points.length;
|
|
|
+ return current.length;
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
@@ -71,7 +71,8 @@ public class ScriptDocValuesGeoPointsTests extends ESTestCase {
|
|
|
final double lon1 = randomLon();
|
|
|
final double lon2 = randomLon();
|
|
|
|
|
|
- final MultiGeoPointValues values = wrap(new GeoPoint(lat1, lon1), new GeoPoint(lat2, lon2));
|
|
|
+ GeoPoint[][] points = {{new GeoPoint(lat1, lon1), new GeoPoint(lat2, lon2)}};
|
|
|
+ final MultiGeoPointValues values = wrap(points);
|
|
|
final ScriptDocValues.GeoPoints script = new ScriptDocValues.GeoPoints(values);
|
|
|
|
|
|
script.setNextDocId(1);
|
|
|
@@ -88,11 +89,13 @@ public class ScriptDocValuesGeoPointsTests extends ESTestCase {
|
|
|
public void testGeoDistance() throws IOException {
|
|
|
final double lat = randomLat();
|
|
|
final double lon = randomLon();
|
|
|
- final MultiGeoPointValues values = wrap(new GeoPoint(lat, lon));
|
|
|
+ GeoPoint[][] points = {{new GeoPoint(lat, lon)}};
|
|
|
+ final MultiGeoPointValues values = wrap(points);
|
|
|
final ScriptDocValues.GeoPoints script = new ScriptDocValues.GeoPoints(values);
|
|
|
script.setNextDocId(0);
|
|
|
|
|
|
- final ScriptDocValues.GeoPoints emptyScript = new ScriptDocValues.GeoPoints(wrap());
|
|
|
+ GeoPoint[][] points2 = {new GeoPoint[0]};
|
|
|
+ final ScriptDocValues.GeoPoints emptyScript = new ScriptDocValues.GeoPoints(wrap(points2));
|
|
|
emptyScript.setNextDocId(0);
|
|
|
|
|
|
final double otherLat = randomLat();
|
|
|
@@ -110,4 +113,31 @@ public class ScriptDocValuesGeoPointsTests extends ESTestCase {
|
|
|
script.planeDistanceWithDefault(otherLat, otherLon, 42) / 1000d, 0.01);
|
|
|
assertEquals(42, emptyScript.planeDistanceWithDefault(otherLat, otherLon, 42), 0);
|
|
|
}
|
|
|
+
|
|
|
+ public void testMissingValues() throws IOException {
|
|
|
+ GeoPoint[][] points = new GeoPoint[between(3, 10)][];
|
|
|
+ for (int d = 0; d < points.length; d++) {
|
|
|
+ points[d] = new GeoPoint[randomBoolean() ? 0 : between(1, 10)];
|
|
|
+ }
|
|
|
+ final ScriptDocValues.GeoPoints geoPoints = new GeoPoints(wrap(points));
|
|
|
+ for (int d = 0; d < points.length; d++) {
|
|
|
+ geoPoints.setNextDocId(d);
|
|
|
+ if (points[d].length > 0) {
|
|
|
+ assertEquals(points[d][0], geoPoints.getValue());
|
|
|
+ } else {
|
|
|
+ Exception e = expectThrows(IllegalStateException.class, () -> geoPoints.getValue());
|
|
|
+ assertEquals("A document doesn't have a value for a field! " +
|
|
|
+ "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
|
|
|
+ e = expectThrows(IllegalStateException.class, () -> geoPoints.get(0));
|
|
|
+ assertEquals("A document doesn't have a value for a field! " +
|
|
|
+ "Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
|
|
|
+ }
|
|
|
+ assertEquals(points[d].length, geoPoints.size());
|
|
|
+ for (int i = 0; i < points[d].length; i++) {
|
|
|
+ assertEquals(points[d][i], geoPoints.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|