use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceSortBuilderTests method testCommonCaseIsOptimized.
public void testCommonCaseIsOptimized() throws IOException {
// make sure the below tests test something...
assertFalse(SortField.class.equals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass()));
QueryShardContext context = createMockShardContext();
// The common case should use LatLonDocValuesField.newDistanceSort
GeoDistanceSortBuilder builder = new GeoDistanceSortBuilder("", new GeoPoint(3.5, 2.1));
SortFieldAndFormat sort = builder.build(context);
assertEquals(LatLonDocValuesField.newDistanceSort("random_field_name", 3.5, 2.1).getClass(), sort.field.getClass());
// however this might be disabled by fancy options
builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1), new GeoPoint(3.0, 4));
sort = builder.build(context);
// 2 points -> plain SortField with a custom comparator
assertEquals(SortField.class, sort.field.getClass());
builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
builder.unit(DistanceUnit.KILOMETERS);
sort = builder.build(context);
// km rather than m -> plain SortField with a custom comparator
assertEquals(SortField.class, sort.field.getClass());
builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
builder.order(SortOrder.DESC);
sort = builder.build(context);
// descending means the max value should be considered rather than min
assertEquals(SortField.class, sort.field.getClass());
builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
builder.setNestedPath("some_nested_path");
sort = builder.build(context);
// can't use LatLon optimized sorting with nested fields
assertEquals(SortField.class, sort.field.getClass());
builder = new GeoDistanceSortBuilder("random_field_name", new GeoPoint(3.5, 2.1));
builder.order(SortOrder.DESC);
sort = builder.build(context);
// can't use LatLon optimized sorting with DESC sorting
assertEquals(SortField.class, sort.field.getClass());
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceSortBuilderTests method testSortModeSumIsRejectedInSetter.
public void testSortModeSumIsRejectedInSetter() {
GeoDistanceSortBuilder builder = new GeoDistanceSortBuilder("testname", -1, -1);
GeoPoint point = RandomGeoGenerator.randomPoint(random());
builder.point(point.getLat(), point.getLon());
try {
builder.sortMode(SortMode.SUM);
fail("sort mode sum should not be supported");
} catch (IllegalArgumentException e) {
// all good
}
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoDistanceSortBuilderTests method testGeoDistanceSortParserManyPointsNoException.
public void testGeoDistanceSortParserManyPointsNoException() throws Exception {
XContentBuilder sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.startArray().value(1.2).value(3).endArray().startArray().value(5).value(6).endArray();
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.value(new GeoPoint(1.2, 3)).value(new GeoPoint(1.2, 3));
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.value("1,2").value("3,4");
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.value("s3y0zh7w1z0g").value("s6wjr4et3f8v");
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.value(1.2).value(3);
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.field("location", new GeoPoint(1, 2));
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.field("location", "1,2");
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.field("location", "s3y0zh7w1z0g");
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
sortBuilder = jsonBuilder();
sortBuilder.startObject();
sortBuilder.startArray("location");
sortBuilder.value(new GeoPoint(1, 2)).value("s3y0zh7w1z0g").startArray().value(1).value(2).endArray().value("1,2");
sortBuilder.endArray();
sortBuilder.field("order", "desc");
sortBuilder.field("unit", "km");
sortBuilder.field("mode", "max");
sortBuilder.endObject();
parse(sortBuilder);
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoPolygonQueryBuilderTests method randomPolygon.
private static List<GeoPoint> randomPolygon() {
ShapeBuilder shapeBuilder = null;
// in this case keep trying until we successfully generate one
while (shapeBuilder == null) {
shapeBuilder = RandomShapeGenerator.createShapeWithin(random(), null, ShapeType.POLYGON);
}
JtsGeometry shape = (JtsGeometry) shapeBuilder.build();
Coordinate[] coordinates = shape.getGeom().getCoordinates();
ArrayList<GeoPoint> polygonPoints = new ArrayList<>();
for (Coordinate coord : coordinates) {
polygonPoints.add(new GeoPoint(coord.y, coord.x));
}
return polygonPoints;
}
use of org.elasticsearch.common.geo.GeoPoint in project elasticsearch by elastic.
the class GeoPolygonQueryBuilderTests method testInvalidClosedPolygon.
public void testInvalidClosedPolygon() {
List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(0, 90));
points.add(new GeoPoint(90, 90));
points.add(new GeoPoint(0, 90));
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GeoPolygonQueryBuilder(GEO_POINT_FIELD_NAME, points));
assertEquals("too few points defined for geo_polygon query", e.getMessage());
}
Aggregations