use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.
the class GeoPolygonQueryBuilderTests method randomPolygon.
private static List<GeoPoint> randomPolygon() {
ShapeBuilder shapeBuilder = null;
// in this case keep trying until we successfully generate one
while (shapeBuilder == null) {
shapeBuilder = RandomShapeGenerator.createShapeWithin(random(), null, ShapeType.POLYGON);
}
JtsGeometry shape = (JtsGeometry) shapeBuilder.build();
Coordinate[] coordinates = shape.getGeom().getCoordinates();
ArrayList<GeoPoint> polygonPoints = new ArrayList<>();
for (Coordinate coord : coordinates) {
polygonPoints.add(new GeoPoint(coord.y, coord.x));
}
return polygonPoints;
}
use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.
the class GeoShapeQueryBuilderTests method testNoRelation.
public void testNoRelation() throws IOException {
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null);
GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.relation(null));
assertEquals("No Shape Relation defined", e.getMessage());
}
use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.
the class GeoShapeQueryBuilderTests method testInvalidRelation.
public void testInvalidRelation() throws IOException {
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null);
GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
builder.strategy(SpatialStrategy.TERM);
expectThrows(IllegalArgumentException.class, () -> builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN)));
GeoShapeQueryBuilder builder2 = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
builder2.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN));
expectThrows(IllegalArgumentException.class, () -> builder2.strategy(SpatialStrategy.TERM));
GeoShapeQueryBuilder builder3 = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
builder3.strategy(SpatialStrategy.TERM);
expectThrows(IllegalArgumentException.class, () -> builder3.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.WITHIN)));
}
use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.
the class GeoShapeQueryBuilderTests method doCreateTestQueryBuilder.
@Override
protected GeoShapeQueryBuilder doCreateTestQueryBuilder() {
ShapeType shapeType = ShapeType.randomType(random());
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
GeoShapeQueryBuilder builder;
clearShapeFields();
if (randomBoolean()) {
builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, shape);
} else {
indexedShapeToReturn = shape;
indexedShapeId = randomAsciiOfLengthBetween(3, 20);
indexedShapeType = randomAsciiOfLengthBetween(3, 20);
builder = new GeoShapeQueryBuilder(GEO_SHAPE_FIELD_NAME, indexedShapeId, indexedShapeType);
if (randomBoolean()) {
indexedShapeIndex = randomAsciiOfLengthBetween(3, 20);
builder.indexedShapeIndex(indexedShapeIndex);
}
if (randomBoolean()) {
indexedShapePath = randomAsciiOfLengthBetween(3, 20);
builder.indexedShapePath(indexedShapePath);
}
}
if (randomBoolean()) {
SpatialStrategy strategy = randomFrom(SpatialStrategy.values());
// we try to avoid that combination
while (shapeType == ShapeType.MULTILINESTRING && strategy == SpatialStrategy.TERM) {
strategy = randomFrom(SpatialStrategy.values());
}
builder.strategy(strategy);
if (strategy != SpatialStrategy.TERM) {
builder.relation(randomFrom(ShapeRelation.values()));
}
}
if (randomBoolean()) {
builder.ignoreUnmapped(randomBoolean());
}
return builder;
}
use of org.elasticsearch.common.geo.builders.ShapeBuilder in project elasticsearch by elastic.
the class GeoShapeQueryTests method testShapeFilterWithRandomGeoCollection.
public void testShapeFilterWithRandomGeoCollection() throws Exception {
// Create a random geometry collection.
GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
logger.info("Created Random GeometryCollection containing {} shapes", gcb.numShapes());
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();
ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("location", filterShape);
filter.relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
}
Aggregations