use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testPointsOnly.
public void testPointsOnly() throws Exception {
String mapping = Strings.toString(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());
client().admin().indices().prepareCreate("geo_points_only").addMapping("type1", mapping, XContentType.JSON).get();
ensureGreen();
ShapeBuilder shape = RandomShapeGenerator.createShape(random());
try {
client().prepareIndex("geo_points_only").setId("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").setQuery(geoIntersectionQuery("location", shape)).get();
assertHitCount(response, 1);
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class RandomShapeGenerator method createShape.
private static ShapeBuilder createShape(Random r, Point nearPoint, Rectangle within, ShapeType st) throws InvalidShapeException {
ShapeBuilder shape;
short i = 0;
do {
shape = createShape(r, nearPoint, within, st, ST_VALIDATE);
if (shape != null) {
return shape;
}
} while (++i != 100);
throw new InvalidShapeException("Unable to create a valid random shape with provided seed");
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
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;
}
use of org.opensearch.common.geo.builders.ShapeBuilder in project OpenSearch by opensearch-project.
the class GeoJsonParser method parseGeometries.
/**
* Parse the geometries array of a GeometryCollection
*
* @param parser Parser that will be read from
* @return Geometry[] geometries of the GeometryCollection
* @throws IOException Thrown if an error occurs while reading from the XContentParser
*/
static GeometryCollectionBuilder parseGeometries(XContentParser parser, AbstractShapeGeometryFieldMapper mapper) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_ARRAY) {
throw new OpenSearchParseException("geometries must be an array of geojson objects");
}
XContentParser.Token token = parser.nextToken();
GeometryCollectionBuilder geometryCollection = new GeometryCollectionBuilder();
while (token != XContentParser.Token.END_ARRAY) {
ShapeBuilder shapeBuilder = ShapeParser.parse(parser);
geometryCollection.shape(shapeBuilder);
token = parser.nextToken();
}
return geometryCollection;
}
use of org.opensearch.common.geo.builders.ShapeBuilder 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);
}
Aggregations