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());
}
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"));
}
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"));
}
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));
}
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;
}
Aggregations