Search in sources :

Example 6 with GeoShapeQueryBuilder

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

the class GeoPointShapeQueryTests method testQueryLinearRing.

public void testQueryLinearRing() throws Exception {
    XContentBuilder xcb = createDefaultMapping();
    client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
    ensureGreen();
    LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
    try {
        // LinearRing extends Line implements Geometry: expose the build process
        GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(defaultGeoFieldName, linearRing);
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
        searchRequestBuilder.setQuery(queryBuilder);
        searchRequestBuilder.setIndices("test");
        searchRequestBuilder.get();
    } catch (SearchPhaseExecutionException e) {
        assertThat(e.getCause().getMessage(), containsString("Field [" + defaultGeoFieldName + "] does not support LINEARRING queries"));
    }
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) LinearRing(org.opensearch.geometry.LinearRing) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 7 with GeoShapeQueryBuilder

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

Example 8 with GeoShapeQueryBuilder

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

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

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

the class GeoShapeQueryTests method testShapeFetchingPath.

public void testShapeFetchingPath() throws Exception {
    createIndex("shapes");
    client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").get();
    String location = "\"geo\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}";
    client().prepareIndex("shapes").setId("1").setSource(String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location), XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().startObject("geo").field("type", "polygon").startArray("coordinates").startArray().startArray().value(-20).value(-20).endArray().startArray().value(20).value(-20).endArray().startArray().value(20).value(20).endArray().startArray().value(-20).value(20).endArray().startArray().value(-20).value(-20).endArray().endArray().endArray().endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("geo");
    SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.geo");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.geo");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.3.geo");
    result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    // now test the query variant
    GeoShapeQueryBuilder query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("geo");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.geo");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.2.geo");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
    query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.2.3.geo");
    result = client().prepareSearch("test").setQuery(query).get();
    assertSearchResponse(result);
    assertHitCount(result, 1);
}
Also used : GeoShapeQueryBuilder(org.opensearch.index.query.GeoShapeQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) 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