@Ignore("https://github.com/elasticsearch/elasticsearch/issues/9904") @Test public void testShapeFilterWithRandomGeoCollection() throws Exception { // Create a random geometry collection. GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(getRandom()); logger.info("Created Random GeometryCollection containing " + gcb.numShapes() + " shapes"); createIndex("randshapes"); assertAcked(prepareCreate("test").addMapping("type", "location", "type=geo_shape")); XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("location"), null).endObject(); indexRandom(true, client().prepareIndex("test", "type", "1").setSource(docSource)); ensureSearchable("test"); ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1))); GeoShapeFilterBuilder filter = FilterBuilders.geoShapeFilter("location", filterShape, ShapeRelation.INTERSECTS); SearchResponse result = client() .prepareSearch("test") .setQuery(QueryBuilders.matchAllQuery()) .setPostFilter(filter) .get(); assertSearchResponse(result); assertHitCount(result, 1); }
@Override protected GeoBoundingBoxQueryBuilder doCreateTestQueryBuilder() { GeoBoundingBoxQueryBuilder builder = new GeoBoundingBoxQueryBuilder(GEO_POINT_FIELD_NAME); Rectangle box = RandomShapeGenerator.xRandomRectangle( random(), RandomShapeGenerator.xRandomPoint(random())); if (randomBoolean()) { // check the top-left/bottom-right combination of setters int path = randomIntBetween(0, 2); switch (path) { case 0: builder.setCorners( new GeoPoint(box.getMaxY(), box.getMinX()), new GeoPoint(box.getMinY(), box.getMaxX())); break; case 1: builder.setCorners( GeohashUtils.encodeLatLon(box.getMaxY(), box.getMinX()), GeohashUtils.encodeLatLon(box.getMinY(), box.getMaxX())); break; default: builder.setCorners(box.getMaxY(), box.getMinX(), box.getMinY(), box.getMaxX()); } } else { // check the bottom-left/ top-right combination of setters if (randomBoolean()) { builder.setCornersOGC( new GeoPoint(box.getMinY(), box.getMinX()), new GeoPoint(box.getMaxY(), box.getMaxX())); } else { builder.setCornersOGC( GeohashUtils.encodeLatLon(box.getMinY(), box.getMinX()), GeohashUtils.encodeLatLon(box.getMaxY(), box.getMaxX())); } } if (randomBoolean()) { builder.setValidationMethod(randomFrom(GeoValidationMethod.values())); } if (randomBoolean()) { builder.ignoreUnmapped(randomBoolean()); } builder.type(randomFrom(GeoExecType.values())); return builder; }
static LineStringBuilder createRandomShape() { LineStringBuilder lsb = (LineStringBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.LINESTRING); if (randomBoolean()) { lsb.close(); } return lsb; }
static PolygonBuilder createRandomShape() { PolygonBuilder pgb = (PolygonBuilder) RandomShapeGenerator.createShape(getRandom(), ShapeType.POLYGON); if (randomBoolean()) { pgb = polyWithOposingOrientation(pgb); } return pgb; }
@Test 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(); assertAcked(prepareCreate("geo_points_only").addMapping("type1", mapping)); ensureGreen(); ShapeBuilder shape = RandomShapeGenerator.createShape(random()); try { index( "geo_points_only", "type1", "1", jsonBuilder().startObject().field("location", shape).endObject()); } 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; } refresh(); // test that point was inserted SearchResponse response = client() .prepareSearch() .setQuery(geoIntersectionQuery("location", shape)) .execute() .actionGet(); assertEquals(1, response.getHits().getTotalHits()); }