Search in sources :

Example 36 with CoordinatesBuilder

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

the class GeoQueryTests method testPolygonSpanningDateline.

public void testPolygonSpanningDateline() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("4").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    PolygonBuilder polygon = new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(177, 10).coordinate(177, 5).coordinate(-177, 5).coordinate(-177, 10));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", polygon.buildGeometry());
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertHitCount(response, 2);
    SearchHits searchHits = response.getHits();
    assertThat(searchHits.getAt(0).getId(), not(equalTo("1")));
    assertThat(searchHits.getAt(1).getId(), not(equalTo("1")));
    assertThat(searchHits.getAt(0).getId(), not(equalTo("4")));
    assertThat(searchHits.getAt(1).getId(), not(equalTo("4")));
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 37 with CoordinatesBuilder

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

the class GeoQueryTests method testIndexPointsMultiPolygon.

public void testIndexPointsMultiPolygon() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-40 -40)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field("name", "Document 3").field(defaultGeoFieldName, "POINT(-50 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    CoordinatesBuilder encloseDocument1Cb = new CoordinatesBuilder();
    encloseDocument1Cb.coordinate(new Coordinate(-35, -35)).coordinate(new Coordinate(-35, -25)).coordinate(new Coordinate(-25, -25)).coordinate(new Coordinate(-25, -35)).coordinate(new Coordinate(-35, -35));
    PolygonBuilder encloseDocument1Shape = new PolygonBuilder(encloseDocument1Cb);
    CoordinatesBuilder encloseDocument2Cb = new CoordinatesBuilder();
    encloseDocument2Cb.coordinate(new Coordinate(-55, -55)).coordinate(new Coordinate(-55, -45)).coordinate(new Coordinate(-45, -45)).coordinate(new Coordinate(-45, -55)).coordinate(new Coordinate(-55, -55));
    PolygonBuilder encloseDocument2Shape = new PolygonBuilder(encloseDocument2Cb);
    MultiPolygonBuilder mp = new MultiPolygonBuilder();
    mp.polygon(encloseDocument1Shape).polygon(encloseDocument2Shape);
    GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(mp);
    Geometry geometry = builder.buildGeometry();
    SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
    assertSearchResponse(searchResponse);
    assertHitCount(searchResponse, 2);
    assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
    assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) Geometry(org.opensearch.geometry.Geometry) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 38 with CoordinatesBuilder

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

the class GeoQueryTests method testMultiPolygonSpanningDateline.

public void testMultiPolygonSpanningDateline() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    MultiPolygonBuilder multiPolygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-167, 10).coordinate(-171, 10).coordinate(171, 5).coordinate(-167, 5).coordinate(-167, 10))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(177, 10).coordinate(177, 5).coordinate(-177, 5).coordinate(-177, 10)));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", multiPolygon.buildGeometry());
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertHitCount(response, 2);
    SearchHits searchHits = response.getHits();
    assertThat(searchHits.getAt(0).getId(), not(equalTo("3")));
    assertThat(searchHits.getAt(1).getId(), not(equalTo("3")));
}
Also used : CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 39 with CoordinatesBuilder

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

the class GeoPointShapeQueryTests method testQueryMultiLine.

public void testQueryMultiLine() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    CoordinatesBuilder coords1 = new CoordinatesBuilder().coordinate(-35, -35).coordinate(-25, -25);
    CoordinatesBuilder coords2 = new CoordinatesBuilder().coordinate(-15, -15).coordinate(-5, -5);
    LineStringBuilder lsb1 = new LineStringBuilder(coords1);
    LineStringBuilder lsb2 = new LineStringBuilder(coords2);
    MultiLineStringBuilder mlb = new MultiLineStringBuilder().linestring(lsb1).linestring(lsb2);
    MultiLine multiline = (MultiLine) mlb.buildGeometry();
    try {
        client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiline)).get();
    } catch (Exception e) {
        assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTILINESTRING + " queries"));
    }
}
Also used : MultiLine(org.opensearch.geometry.MultiLine) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) MultiLineStringBuilder(org.opensearch.common.geo.builders.MultiLineStringBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 40 with CoordinatesBuilder

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

the class GeoShapeQueryTests method testQueryRandomGeoCollection.

public void testQueryRandomGeoCollection() throws Exception {
    // Create a random geometry collection.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
    CoordinatesBuilder cb = new CoordinatesBuilder();
    for (int i = 0; i < randomPoly.numPoints(); ++i) {
        cb.coordinate(randomPoly.getPolyLon(i), randomPoly.getPolyLat(i));
    }
    gcb.shape(new PolygonBuilder(cb));
    XContentBuilder builder = createRandomMapping();
    client().admin().indices().prepareCreate("test").addMapping("type", builder).get();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(gcb.numShapes() - 1));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assumeTrue("Skipping the check for the polygon with a degenerated dimension until " + " https://issues.apache.org/jira/browse/LUCENE-8634 is fixed", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
    assertHitCount(result, 1);
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) RandomShapeGenerator.xRandomPoint(org.opensearch.test.geo.RandomShapeGenerator.xRandomPoint) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)42 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)37 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)21 MultiLineStringBuilder (org.opensearch.common.geo.builders.MultiLineStringBuilder)18 Coordinate (org.locationtech.jts.geom.Coordinate)12 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)12 MultiPolygonBuilder (org.opensearch.common.geo.builders.MultiPolygonBuilder)11 SearchResponse (org.opensearch.action.search.SearchResponse)8 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)8 InvalidShapeException (org.locationtech.spatial4j.exception.InvalidShapeException)7 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)7 ArrayList (java.util.ArrayList)5 PointBuilder (org.opensearch.common.geo.builders.PointBuilder)5 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)5 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)5 LineString (org.locationtech.jts.geom.LineString)4 Polygon (org.locationtech.jts.geom.Polygon)4 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)4 Settings (org.opensearch.common.settings.Settings)4 OpenSearchGeoAssertions.assertMultiLineString (org.opensearch.test.hamcrest.OpenSearchGeoAssertions.assertMultiLineString)4