Search in sources :

Example 11 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testPointQuery.

/**
 * tests querying a random geometry collection with a point
 */
public void testPointQuery() throws Exception {
    // Create a random geometry collection to index.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    double[] pt = new double[] { GeoTestUtil.nextLongitude(), GeoTestUtil.nextLatitude() };
    PointBuilder pb = new PointBuilder(pt[0], pt[1]);
    gcb.shape(pb);
    if (randomBoolean()) {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").execute().actionGet();
    } else {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").execute().actionGet();
    }
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", pb);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 12 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testContainsShapeQuery.

public void testContainsShapeQuery() throws Exception {
    // Create a random geometry collection.
    Rectangle mbr = xRandomRectangle(random(), xRandomPoint(random()), true);
    boolean usePrefixTrees = randomBoolean();
    GeometryCollectionBuilder gcb;
    if (usePrefixTrees) {
        gcb = createGeometryCollectionWithin(random(), mbr);
    } else {
        // vector strategy does not yet support multipoint queries
        gcb = new GeometryCollectionBuilder();
        int numShapes = RandomNumbers.randomIntBetween(random(), 1, 4);
        for (int i = 0; i < numShapes; ++i) {
            ShapeBuilder shape;
            do {
                shape = RandomShapeGenerator.createShapeWithin(random(), mbr);
            } while (shape instanceof MultiPointBuilder);
            gcb.shape(shape);
        }
    }
    if (usePrefixTrees) {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").execute().actionGet();
    } else {
        client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").execute().actionGet();
    }
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    // index the mbr of the collection
    EnvelopeBuilder env = new EnvelopeBuilder(new Coordinate(mbr.getMinX(), mbr.getMaxY()), new Coordinate(mbr.getMaxX(), mbr.getMinY()));
    docSource = env.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("2").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", filterShape).relation(ShapeRelation.CONTAINS);
    SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getHits().length, greaterThan(0));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle) RandomShapeGenerator.xRandomRectangle(org.opensearch.test.geo.RandomShapeGenerator.xRandomRectangle) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) RandomShapeGenerator.xRandomPoint(org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 13 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testGeometryCollectionRelations.

public void testGeometryCollectionRelations() throws Exception {
    XContentBuilder mapping = createDefaultMapping();
    createIndex("test", Settings.builder().put("index.number_of_shards", 1).build(), "doc", mapping);
    EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(-10, 10), new Coordinate(10, -10));
    client().index(new IndexRequest("test").source(jsonBuilder().startObject().field("geo", envelopeBuilder).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
    {
        // A geometry collection that is fully within the indexed shape
        GeometryCollectionBuilder builder = new GeometryCollectionBuilder();
        builder.shape(new PointBuilder(1, 2));
        builder.shape(new PointBuilder(-2, -1));
        SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.CONTAINS)).get();
        assertHitCount(response, 1);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.INTERSECTS)).get();
        assertHitCount(response, 1);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.DISJOINT)).get();
        assertHitCount(response, 0);
    }
    // A geometry collection that is partially within the indexed shape
    {
        GeometryCollectionBuilder builder = new GeometryCollectionBuilder();
        builder.shape(new PointBuilder(1, 2));
        builder.shape(new PointBuilder(20, 30));
        SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.CONTAINS)).get();
        assertHitCount(response, 0);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.INTERSECTS)).get();
        assertHitCount(response, 1);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.DISJOINT)).get();
        assertHitCount(response, 0);
    }
    {
        // A geometry collection that is disjoint with the indexed shape
        GeometryCollectionBuilder builder = new GeometryCollectionBuilder();
        builder.shape(new PointBuilder(-20, -30));
        builder.shape(new PointBuilder(20, 30));
        SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.CONTAINS)).get();
        assertHitCount(response, 0);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.INTERSECTS)).get();
        assertHitCount(response, 0);
        response = client().prepareSearch("test").setQuery(geoShapeQuery("geo", builder.buildGeometry()).relation(ShapeRelation.DISJOINT)).get();
        assertHitCount(response, 1);
    }
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) IndexRequest(org.opensearch.action.index.IndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 14 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testShapeFilterWithDefinedGeoCollection.

public void testShapeFilterWithDefinedGeoCollection() throws Exception {
    createIndex("shapes");
    client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").get();
    XContentBuilder docSource = jsonBuilder().startObject().startObject("geo").field("type", "geometrycollection").startArray("geometries").startObject().field("type", "point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject().startObject().field("type", "linestring").startArray("coordinates").startArray().value(101.0).value(0.0).endArray().startArray().value(102.0).value(1.0).endArray().endArray().endObject().endArray().endObject().endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0)))).relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    // no shape
    filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder());
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 0);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 15 with GeometryCollectionBuilder

use of org.opensearch.common.geo.builders.GeometryCollectionBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testExistsQuery.

public void testExistsQuery() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    XContentBuilder builder = createRandomMapping();
    client().admin().indices().prepareCreate("test").addMapping("type", builder).execute().actionGet();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ExistsQueryBuilder eqb = QueryBuilders.existsQuery("geo");
    SearchResponse result = client().prepareSearch("test").setQuery(eqb).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) ExistsQueryBuilder(org.opensearch.index.query.ExistsQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)15 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)10 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)10 Coordinate (org.locationtech.jts.geom.Coordinate)6 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)5 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)4 Geometry (org.opensearch.geometry.Geometry)4 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 RandomShapeGenerator.xRandomPoint (org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint)3 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 Point (org.locationtech.spatial4j.shape.Point)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 Shape (org.locationtech.spatial4j.shape.Shape)1