Search in sources :

Example 11 with GeoShapeQueryBuilder

use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.

the class GeoShapeQueryTests method testEnvelopeSpanningDateline.

// Test for issue #34418
public void testEnvelopeSpanningDateline() throws Exception {
    XContentBuilder mapping = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).get();
    ensureGreen();
    String doc1 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-33.918711,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
    client().index(new IndexRequest("test").id("1").source(doc1, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
    String doc2 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-49.0,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
    client().index(new IndexRequest("test").id("2").source(doc2, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
    String doc3 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "49.0,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
    client().index(new IndexRequest("test").id("3").source(doc3, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
    @SuppressWarnings("unchecked") CheckedSupplier<GeoShapeQueryBuilder, IOException> querySupplier = randomFrom(() -> QueryBuilders.geoShapeQuery("geo", new EnvelopeBuilder(new Coordinate(-21, 44), new Coordinate(-39, 9))).relation(ShapeRelation.WITHIN), () -> {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("geo").startObject("shape").field("type", "envelope").startArray("coordinates").startArray().value(-21).value(44).endArray().startArray().value(-39).value(9).endArray().endArray().endObject().field("relation", "within").endObject().endObject();
        try (XContentParser parser = createParser(builder)) {
            parser.nextToken();
            return GeoShapeQueryBuilder.fromXContent(parser);
        }
    }, () -> {
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("geo").field("shape", "BBOX (-21, -39, 44, 9)").field("relation", "within").endObject().endObject();
        try (XContentParser parser = createParser(builder)) {
            parser.nextToken();
            return GeoShapeQueryBuilder.fromXContent(parser);
        }
    });
    SearchResponse response = client().prepareSearch("test").setQuery(querySupplier.get()).get();
    assertHitCount(response, 2);
    assertThat(response.getHits().getAt(0).getId(), not(equalTo("1")));
    assertThat(response.getHits().getAt(1).getId(), not(equalTo("1")));
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) EnvelopeBuilder(org.opensearch.common.geo.builders.EnvelopeBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) IndexRequest(org.opensearch.action.index.IndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 12 with GeoShapeQueryBuilder

use of org.opensearch.index.query.GeoShapeQueryBuilder 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)

Aggregations

GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)12 SearchResponse (org.opensearch.action.search.SearchResponse)11 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)11 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)11 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)5 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)5 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)5 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)3 SearchHits (org.opensearch.search.SearchHits)3 RandomShapeGenerator.xRandomPoint (org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint)3 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)2 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)2 IOException (java.io.IOException)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)1