Search in sources :

Example 1 with GeoShapeQueryBuilder

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

the class GeoQueryTests method testRectangleSpanningDateline.

public void testRectangleSpanningDateline() 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 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
    Rectangle rectangle = new Rectangle(169, -178, 1, -1);
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", rectangle);
    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")));
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Rectangle(org.opensearch.geometry.Rectangle) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with GeoShapeQueryBuilder

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

the class GeoShapeQueryTests method testIndexRectangleSpanningDateLine.

public void testIndexRectangleSpanningDateLine() throws Exception {
    String mapping = Strings.toString(createRandomMapping());
    client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).get();
    ensureGreen();
    EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(178, 10), new Coordinate(-178, -10));
    XContentBuilder docSource = envelopeBuilder.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = new PointBuilder(179, 0);
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : ShapeBuilder(org.opensearch.common.geo.builders.ShapeBuilder) MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) 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) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with GeoShapeQueryBuilder

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

the class GeoShapeQueryTests method testRandomGeoCollectionQuery.

public void testRandomGeoCollectionQuery() throws Exception {
    // Create a random geometry collection to index.
    GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
    org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
    assumeTrue("Skipping the check for the polygon with a degenerated dimension", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
    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 mapping = createRandomMapping();
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).setSettings(settings).get();
    ensureGreen();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
    client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    // Create a random geometry collection to query
    GeometryCollectionBuilder queryCollection = RandomShapeGenerator.createGeometryCollection(random());
    queryCollection.shape(new PolygonBuilder(cb));
    GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", queryCollection);
    geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
    SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
    assertSearchResponse(result);
    assertThat(result.getHits().getHits().length, greaterThan(0));
}
Also used : GeometryCollectionBuilder(org.opensearch.common.geo.builders.GeometryCollectionBuilder) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) 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) Settings(org.opensearch.common.settings.Settings) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with GeoShapeQueryBuilder

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

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

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