Search in sources :

Example 16 with ShapeBuilder

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

the class GeoShapeQueryTests method testPointsOnly.

public void testPointsOnly() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_shape").field("tree", randomBoolean() ? "quadtree" : "geohash").field("tree_levels", "6").field("distance_error_pct", "0.01").field("points_only", true).endObject().endObject().endObject().endObject().string();
    client().admin().indices().prepareCreate("geo_points_only").addMapping("type1", mapping, XContentType.JSON).execute().actionGet();
    ensureGreen();
    ShapeBuilder shape = RandomShapeGenerator.createShape(random());
    try {
        client().prepareIndex("geo_points_only", "type1", "1").setSource(jsonBuilder().startObject().field("location", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    } catch (MapperParsingException e) {
        // RandomShapeGenerator created something other than a POINT type, verify the correct exception is thrown
        assertThat(e.getCause().getMessage(), containsString("is configured for points only"));
        return;
    }
    // test that point was inserted
    SearchResponse response = client().prepareSearch("geo_points_only").setTypes("type1").setQuery(geoIntersectionQuery("location", shape)).execute().actionGet();
    assertEquals(1, response.getHits().getTotalHits());
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with ShapeBuilder

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

the class GeoShapeQueryTests method testIndexedShapeReferenceSourceDisabled.

public void testIndexedShapeReferenceSourceDisabled() throws Exception {
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("location").field("type", "geo_shape").field("tree", "quadtree").endObject().endObject().endObject();
    client().admin().indices().prepareCreate("test").addMapping("type1", mapping).get();
    createIndex("shapes", Settings.EMPTY, "shape_type", "_source", "enabled=false");
    ensureGreen();
    ShapeBuilder shape = ShapeBuilders.newEnvelope(new Coordinate(-45, 45), new Coordinate(45, -45));
    client().prepareIndex("shapes", "shape_type", "Big_Rectangle").setSource(jsonBuilder().startObject().field("shape", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client().prepareSearch("test").setTypes("type1").setQuery(geoIntersectionQuery("location", "Big_Rectangle", "shape_type")).get());
    assertThat(e.getRootCause(), instanceOf(IllegalArgumentException.class));
    assertThat(e.getRootCause().getMessage(), containsString("source disabled"));
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) ElasticsearchException(org.elasticsearch.ElasticsearchException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 18 with ShapeBuilder

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

the class GeoShapeQueryTests method testIndexedShapeReference.

public void testIndexedShapeReference() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "geo_shape").field("tree", "quadtree").endObject().endObject().endObject().endObject().string();
    client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).execute().actionGet();
    createIndex("shapes");
    ensureGreen();
    ShapeBuilder shape = ShapeBuilders.newEnvelope(new Coordinate(-45, 45), new Coordinate(45, -45));
    client().prepareIndex("shapes", "shape_type", "Big_Rectangle").setSource(jsonBuilder().startObject().field("shape", shape).endObject()).setRefreshPolicy(IMMEDIATE).get();
    client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "Document 1").startObject("location").field("type", "point").startArray("coordinates").value(-30).value(-30).endArray().endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("type1").setQuery(geoIntersectionQuery("location", "Big_Rectangle", "shape_type")).execute().actionGet();
    assertSearchResponse(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
    searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("location", "Big_Rectangle", "shape_type")).execute().actionGet();
    assertSearchResponse(searchResponse);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
    assertThat(searchResponse.getHits().getHits().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
}
Also used : ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 19 with ShapeBuilder

use of org.elasticsearch.common.geo.builders.ShapeBuilder 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)

Example 20 with ShapeBuilder

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

the class RandomShapeGenerator method createGeometryCollection.

protected static GeometryCollectionBuilder createGeometryCollection(Random r, Point nearPoint, Rectangle bounds, int numGeometries) throws InvalidShapeException {
    if (numGeometries <= 0) {
        // cap geometry collection at 4 shapes (to save test time)
        numGeometries = RandomNumbers.randomIntBetween(r, 2, 4);
    }
    if (nearPoint == null) {
        nearPoint = xRandomPoint(r);
    }
    if (bounds == null) {
        bounds = xRandomRectangle(r, nearPoint);
    }
    GeometryCollectionBuilder gcb = new GeometryCollectionBuilder();
    for (int i = 0; i < numGeometries; ) {
        ShapeBuilder builder = createShapeWithin(r, bounds);
        // Not the most efficient but its the lesser of the evil alternatives
        if (builder != null) {
            gcb.shape(builder);
            ++i;
        }
    }
    return gcb;
}
Also used : GeometryCollectionBuilder(org.elasticsearch.common.geo.builders.GeometryCollectionBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Point(org.locationtech.spatial4j.shape.Point)

Aggregations

ShapeBuilder (org.elasticsearch.common.geo.builders.ShapeBuilder)20 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 GeometryCollectionBuilder (org.elasticsearch.common.geo.builders.GeometryCollectionBuilder)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Query (org.apache.lucene.search.Query)2 SpatialStrategy (org.elasticsearch.common.geo.SpatialStrategy)2 GeoShapeQueryBuilder (org.elasticsearch.index.query.GeoShapeQueryBuilder)2 ShapeType (org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType)2 Point (org.locationtech.spatial4j.shape.Point)2 JtsGeometry (org.locationtech.spatial4j.shape.jts.JtsGeometry)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 IndexableField (org.apache.lucene.index.IndexableField)1