Search in sources :

Example 76 with GeoPoint

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());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) SortField(org.apache.lucene.search.SortField)

Example 77 with GeoPoint

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
    }
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 78 with GeoPoint

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);
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 79 with GeoPoint

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;
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) JtsGeometry(org.locationtech.spatial4j.shape.jts.JtsGeometry) ArrayList(java.util.ArrayList)

Example 80 with GeoPoint

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());
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList)

Aggregations

GeoPoint (org.elasticsearch.common.geo.GeoPoint)122 SearchResponse (org.elasticsearch.action.search.SearchResponse)40 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 XContentParser (org.elasticsearch.common.xcontent.XContentParser)9 GeoBounds (org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds)9 Range (org.elasticsearch.search.aggregations.bucket.range.Range)8 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)8 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)7 Version (org.elasticsearch.Version)7 Bucket (org.elasticsearch.search.aggregations.bucket.range.Range.Bucket)7 Settings (org.elasticsearch.common.settings.Settings)6 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)6 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)5 ParsingException (org.elasticsearch.common.ParsingException)5 SearchHit (org.elasticsearch.search.SearchHit)5 Test (org.testng.annotations.Test)5 HashSet (java.util.HashSet)4 DistanceUnit (org.elasticsearch.common.unit.DistanceUnit)4