Search in sources :

Example 11 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder 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 12 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method testNoRelation.

public void testNoRelation() throws IOException {
    ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null);
    GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.relation(null));
    assertEquals("No Shape Relation defined", e.getMessage());
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder)

Example 13 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method testInvalidRelation.

public void testInvalidRelation() throws IOException {
    ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null);
    GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    builder.strategy(SpatialStrategy.TERM);
    expectThrows(IllegalArgumentException.class, () -> builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN)));
    GeoShapeQueryBuilder builder2 = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    builder2.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN));
    expectThrows(IllegalArgumentException.class, () -> builder2.strategy(SpatialStrategy.TERM));
    GeoShapeQueryBuilder builder3 = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    builder3.strategy(SpatialStrategy.TERM);
    expectThrows(IllegalArgumentException.class, () -> builder3.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN)));
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder)

Example 14 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected GeoShapeQueryBuilder doCreateTestQueryBuilder() {
    ShapeType shapeType = ShapeType.randomType(random());
    ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
    GeoShapeQueryBuilder builder;
    clearShapeFields();
    if (randomBoolean()) {
        builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
    } else {
        indexedShapeToReturn = shape;
        indexedShapeId = randomAsciiOfLengthBetween(3, 20);
        indexedShapeType = randomAsciiOfLengthBetween(3, 20);
        builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, indexedShapeId, indexedShapeType);
        if (randomBoolean()) {
            indexedShapeIndex = randomAsciiOfLengthBetween(3, 20);
            builder.indexedShapeIndex(indexedShapeIndex);
        }
        if (randomBoolean()) {
            indexedShapePath = randomAsciiOfLengthBetween(3, 20);
            builder.indexedShapePath(indexedShapePath);
        }
    }
    if (randomBoolean()) {
        SpatialStrategy strategy = randomFrom(SpatialStrategy.values());
        // we try to avoid that combination
        while (shapeType == ShapeType.MULTILINESTRING && strategy == SpatialStrategy.TERM) {
            strategy = randomFrom(SpatialStrategy.values());
        }
        builder.strategy(strategy);
        if (strategy != SpatialStrategy.TERM) {
            builder.relation(randomFrom(ShapeRelation.values()));
        }
    }
    if (randomBoolean()) {
        builder.ignoreUnmapped(randomBoolean());
    }
    return builder;
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) ShapeType(org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType) SpatialStrategy(org.elasticsearch.common.geo.SpatialStrategy)

Example 15 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testShapeFilterWithRandomGeoCollection.

public void testShapeFilterWithRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    logger.info("Created Random GeometryCollection containing {} shapes", gcb.numShapes());
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree").execute().actionGet();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape);
    filter.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)20 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 SpatialStrategy (org.elasticsearch.common.geo.SpatialStrategy)2 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)2 ShapeType (org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)2 Point (org.locationtech.spatial4j.shape.Point)2 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 IndexableField (org.apache.lucene.index.IndexableField)1