Search in sources :

Example 6 with PointBuilder

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

the class GeoPointShapeQueryTests method testQueryPoint.

public void testQueryPoint() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    PointBuilder pb = new PointBuilder().coordinate(-35, -25);
    Point point = pb.buildGeometry();
    try {
        client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point)).get();
    } catch (Exception e) {
        assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.POINT + " queries"));
    }
}
Also used : MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Point(org.opensearch.geometry.Point) MultiPoint(org.opensearch.geometry.MultiPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 7 with PointBuilder

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

the class GeoShapeQueryTests method testDistanceQuery.

public void testDistanceQuery() throws Exception {
    String mapping = Strings.toString(createRandomMapping());
    client().admin().indices().prepareCreate("test_distance").addMapping("type1", mapping, XContentType.JSON).get();
    ensureGreen();
    CircleBuilder circleBuilder = new CircleBuilder().center(new Coordinate(1, 0)).radius(350, DistanceUnit.KILOMETERS);
    client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(2, 2)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
    client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(3, 1)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
    client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(-20, -30)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
    client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(20, 30)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
    SearchResponse response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.WITHIN)).get();
    assertEquals(2, response.getHits().getTotalHits().value);
    response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.INTERSECTS)).get();
    assertEquals(2, response.getHits().getTotalHits().value);
    response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.DISJOINT)).get();
    assertEquals(2, response.getHits().getTotalHits().value);
    response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.CONTAINS)).get();
    assertEquals(0, response.getHits().getTotalHits().value);
}
Also used : MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) Matchers.containsString(org.hamcrest.Matchers.containsString) CircleBuilder(org.opensearch.common.geo.builders.CircleBuilder) IndexRequest(org.opensearch.action.index.IndexRequest) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 8 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder 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 9 with PointBuilder

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

the class GeoFilterIT method testShapeRelations.

public void testShapeRelations() throws Exception {
    assertTrue("Intersect relation is not supported", intersectSupport);
    assertTrue("Disjoint relation is not supported", disjointSupport);
    assertTrue("within relation is not supported", withinSupport);
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("polygon").startObject("properties").startObject("area").field("type", "geo_shape").field("tree", "geohash").endObject().endObject().endObject().endObject());
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("shapes").addMapping("polygon", mapping, XContentType.JSON);
    mappingRequest.get();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
    // Create a multipolygon with two polygons. The first is an rectangle of size 10x10
    // with a hole of size 5x5 equidistant from all sides. This hole in turn contains
    // the second polygon of size 4x4 equidistant from all sites
    MultiPolygonBuilder polygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    BytesReference data = BytesReference.bytes(jsonBuilder().startObject().field("area", polygon).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // Point in polygon
    SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(3, 3))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point in polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
    assertHitCount(result, 0);
    // by definition the border of a polygon belongs to the inner
    // so the border of a polygons hole also belongs to the inner
    // of the polygon NOT the hole
    // Point on polygon border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(10.0, 5.0))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    // Point on hole border
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(5.0, 2.0))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("1"));
    if (disjointSupport) {
        // Point not in polygon
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(3, 3))).get();
        assertHitCount(result, 0);
        // Point in polygon hole
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(4.5, 4.5))).get();
        assertHitCount(result, 1);
        assertFirstHit(result, hasId("1"));
    }
    // Create a polygon that fills the empty area of the polygon defined above
    PolygonBuilder inverse = new PolygonBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", inverse).endObject());
    client().prepareIndex("shapes").setId("2").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // re-check point on polygon hole
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
    assertHitCount(result, 1);
    assertFirstHit(result, hasId("2"));
    // Create Polygon with hole and common edge
    PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(10, 5).coordinate(10, -5).close()));
    if (withinSupport) {
        // Polygon WithIn Polygon
        builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-30, -30).coordinate(-30, 30).coordinate(30, 30).coordinate(30, -30).close());
        result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoWithinQuery("area", builder.buildGeometry())).get();
        assertHitCount(result, 2);
    }
    // Create a polygon crossing longitude 180.
    builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    // Create a polygon crossing longitude 180 with hole.
    builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, -5).coordinate(185, -5).coordinate(185, 5).coordinate(175, 5).close()));
    data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
    client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
    client().admin().indices().prepareRefresh().get();
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(174, -4).buildGeometry())).get();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(-174, -4).buildGeometry())).get();
    assertHitCount(result, 1);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -4).buildGeometry())).get();
    assertHitCount(result, 0);
    result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -6).buildGeometry())).get();
    assertHitCount(result, 1);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CoordinatesBuilder(org.opensearch.common.geo.builders.CoordinatesBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) CreateIndexRequestBuilder(org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder) MultiPolygonBuilder(org.opensearch.common.geo.builders.MultiPolygonBuilder) PolygonBuilder(org.opensearch.common.geo.builders.PolygonBuilder) LineStringBuilder(org.opensearch.common.geo.builders.LineStringBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 10 with PointBuilder

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

the class GeoWKTParser method parsePoint.

private static PointBuilder parsePoint(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
    if (nextEmptyOrOpen(stream).equals(EMPTY)) {
        return null;
    }
    PointBuilder pt = new PointBuilder(nextNumber(stream), nextNumber(stream));
    if (isNumberNext(stream)) {
        GeoPoint.assertZValue(ignoreZValue, nextNumber(stream));
    }
    nextCloser(stream);
    return pt;
}
Also used : MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder)

Aggregations

PointBuilder (org.opensearch.common.geo.builders.PointBuilder)13 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)9 SearchResponse (org.opensearch.action.search.SearchResponse)6 Coordinate (org.locationtech.jts.geom.Coordinate)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Point (org.locationtech.spatial4j.shape.Point)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)2 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)2 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)2 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)2 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)2 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)2 MultiPoint (org.opensearch.geometry.MultiPoint)2 Point (org.opensearch.geometry.Point)2 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)2 ArrayList (java.util.ArrayList)1 ConvexHull (org.locationtech.jts.algorithm.ConvexHull)1