use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoPointShapeQueryTests method testQueryLinearRing.
public void testQueryLinearRing() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
LinearRing linearRing = new LinearRing(new double[] { -25, -35, -25 }, new double[] { -25, -35, -25 });
try {
// LinearRing extends Line implements Geometry: expose the build process
GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(defaultGeoFieldName, linearRing);
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE);
searchRequestBuilder.setQuery(queryBuilder);
searchRequestBuilder.setIndices("test");
searchRequestBuilder.get();
} catch (SearchPhaseExecutionException e) {
assertThat(e.getCause().getMessage(), containsString("Field [" + defaultGeoFieldName + "] does not support LINEARRING queries"));
}
}
use of org.opensearch.index.query.GeoShapeQueryBuilder 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);
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testPointQuery.
/**
* tests querying a random geometry collection with a point
*/
public void testPointQuery() throws Exception {
// Create a random geometry collection to index.
GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
double[] pt = new double[] { GeoTestUtil.nextLongitude(), GeoTestUtil.nextLatitude() };
PointBuilder pb = new PointBuilder(pt[0], pt[1]);
gcb.shape(pb);
if (randomBoolean()) {
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").execute().actionGet();
} else {
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").execute().actionGet();
}
XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", pb);
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertSearchResponse(result);
assertHitCount(result, 1);
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testContainsShapeQuery.
public void testContainsShapeQuery() throws Exception {
// Create a random geometry collection.
Rectangle mbr = xRandomRectangle(random(), xRandomPoint(random()), true);
boolean usePrefixTrees = randomBoolean();
GeometryCollectionBuilder gcb;
if (usePrefixTrees) {
gcb = createGeometryCollectionWithin(random(), mbr);
} else {
// vector strategy does not yet support multipoint queries
gcb = new GeometryCollectionBuilder();
int numShapes = RandomNumbers.randomIntBetween(random(), 1, 4);
for (int i = 0; i < numShapes; ++i) {
ShapeBuilder shape;
do {
shape = RandomShapeGenerator.createShapeWithin(random(), mbr);
} while (shape instanceof MultiPointBuilder);
gcb.shape(shape);
}
}
if (usePrefixTrees) {
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").execute().actionGet();
} else {
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").execute().actionGet();
}
XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("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("geo"), null).endObject();
client().prepareIndex("test").setId("2").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
ShapeBuilder filterShape = (gcb.getShapeAt(randomIntBetween(0, gcb.numShapes() - 1)));
GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", filterShape).relation(ShapeRelation.CONTAINS);
SearchResponse response = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(response);
assertThat(response.getHits().getHits().length, greaterThan(0));
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testShapeFetchingPath.
public void testShapeFetchingPath() throws Exception {
createIndex("shapes");
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape").get();
String location = "\"geo\" : {\"type\":\"polygon\", \"coordinates\":[[[-10,-10],[10,-10],[10,10],[-10,10],[-10,-10]]]}";
client().prepareIndex("shapes").setId("1").setSource(String.format(Locale.ROOT, "{ %s, \"1\" : { %s, \"2\" : { %s, \"3\" : { %s } }} }", location, location, location, location), XContentType.JSON).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().startObject("geo").field("type", "polygon").startArray("coordinates").startArray().startArray().value(-20).value(-20).endArray().startArray().value(20).value(-20).endArray().startArray().value(20).value(20).endArray().startArray().value(-20).value(20).endArray().startArray().value(-20).value(-20).endArray().endArray().endArray().endObject().endObject()).setRefreshPolicy(IMMEDIATE).get();
GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("geo");
SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.geo");
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.geo");
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
filter = QueryBuilders.geoShapeQuery("geo", "1").relation(ShapeRelation.INTERSECTS).indexedShapeIndex("shapes").indexedShapePath("1.2.3.geo");
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
// now test the query variant
GeoShapeQueryBuilder query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("geo");
result = client().prepareSearch("test").setQuery(query).get();
assertSearchResponse(result);
assertHitCount(result, 1);
query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.geo");
result = client().prepareSearch("test").setQuery(query).get();
assertSearchResponse(result);
assertHitCount(result, 1);
query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.2.geo");
result = client().prepareSearch("test").setQuery(query).get();
assertSearchResponse(result);
assertHitCount(result, 1);
query = QueryBuilders.geoShapeQuery("geo", "1").indexedShapeIndex("shapes").indexedShapePath("1.2.3.geo");
result = client().prepareSearch("test").setQuery(query).get();
assertSearchResponse(result);
assertHitCount(result, 1);
}
Aggregations