Search in sources :

Example 1 with EnvelopeBuilder

use of org.elasticsearch.common.geo.builders.EnvelopeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryBuilderTests method testThatXContentSerializationInsideOfArrayWorks.

// see #3878
public void testThatXContentSerializationInsideOfArrayWorks() throws Exception {
    EnvelopeBuilder envelopeBuilder = ShapeBuilders.newEnvelope(new Coordinate(0, 0), new Coordinate(10, 10));
    GeoShapeQueryBuilder geoQuery = QueryBuilders.geoShapeQuery("searchGeometry", envelopeBuilder);
    JsonXContent.contentBuilder().startArray().value(geoQuery).endArray();
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) EnvelopeBuilder(org.elasticsearch.common.geo.builders.EnvelopeBuilder)

Example 2 with EnvelopeBuilder

use of org.elasticsearch.common.geo.builders.EnvelopeBuilder in project elasticsearch by elastic.

the class GeoShapeQueryTests method testContainsShapeQuery.

public void testContainsShapeQuery() throws Exception {
    // Create a random geometry collection.
    Rectangle mbr = xRandomRectangle(random(), xRandomPoint(random()), true);
    GeometryCollectionBuilder gcb = createGeometryCollectionWithin(random(), mbr);
    client().admin().indices().prepareCreate("test").addMapping("type", "location", "type=geo_shape,tree=quadtree").execute().actionGet();
    XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject();
    client().prepareIndex("test", "type", "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("location"), null).endObject();
    client().prepareIndex("test", "type", "2").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
    ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
    GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape).relation(ShapeRelation.CONTAINS);
    SearchResponse response = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
    assertSearchResponse(response);
    assertThat(response.getHits().getTotalHits(), greaterThan(0L));
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) Rectangle(org.locationtech.spatial4j.shape.Rectangle) RandomShapeGenerator.xRandomRectangle(org.elasticsearch.test.geo.RandomShapeGenerator.xRandomRectangle) EnvelopeBuilder(org.elasticsearch.common.geo.builders.EnvelopeBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Coordinate (com.vividsolutions.jts.geom.Coordinate)2 EnvelopeBuilder (org.elasticsearch.common.geo.builders.EnvelopeBuilder)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)1 ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)1 RandomShapeGenerator.xRandomRectangle (org.elasticsearch.test.geo.RandomShapeGenerator.xRandomRectangle)1 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)1 Rectangle (org.locationtech.spatial4j.shape.Rectangle)1