use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testEnvelopeSpanningDateline.
// Test for issue #34418
public void testEnvelopeSpanningDateline() throws Exception {
XContentBuilder mapping = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).get();
ensureGreen();
String doc1 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-33.918711,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
client().index(new IndexRequest("test").id("1").source(doc1, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
String doc2 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "-49.0,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
client().index(new IndexRequest("test").id("2").source(doc2, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
String doc3 = "{\"geo\": {\r\n" + "\"coordinates\": [\r\n" + "49.0,\r\n" + "18.847685\r\n" + "],\r\n" + "\"type\": \"Point\"\r\n" + "}}";
client().index(new IndexRequest("test").id("3").source(doc3, XContentType.JSON).setRefreshPolicy(IMMEDIATE)).actionGet();
@SuppressWarnings("unchecked") CheckedSupplier<GeoShapeQueryBuilder, IOException> querySupplier = randomFrom(() -> QueryBuilders.geoShapeQuery("geo", new EnvelopeBuilder(new Coordinate(-21, 44), new Coordinate(-39, 9))).relation(ShapeRelation.WITHIN), () -> {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("geo").startObject("shape").field("type", "envelope").startArray("coordinates").startArray().value(-21).value(44).endArray().startArray().value(-39).value(9).endArray().endArray().endObject().field("relation", "within").endObject().endObject();
try (XContentParser parser = createParser(builder)) {
parser.nextToken();
return GeoShapeQueryBuilder.fromXContent(parser);
}
}, () -> {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("geo").field("shape", "BBOX (-21, -39, 44, 9)").field("relation", "within").endObject().endObject();
try (XContentParser parser = createParser(builder)) {
parser.nextToken();
return GeoShapeQueryBuilder.fromXContent(parser);
}
});
SearchResponse response = client().prepareSearch("test").setQuery(querySupplier.get()).get();
assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), not(equalTo("1")));
assertThat(response.getHits().getAt(1).getId(), not(equalTo("1")));
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testShapeFilterWithDefinedGeoCollection.
public void testShapeFilterWithDefinedGeoCollection() throws Exception {
createIndex("shapes");
client().admin().indices().prepareCreate("test").addMapping("type", "geo", "type=geo_shape,tree=quadtree").get();
XContentBuilder docSource = jsonBuilder().startObject().startObject("geo").field("type", "geometrycollection").startArray("geometries").startObject().field("type", "point").startArray("coordinates").value(100.0).value(0.0).endArray().endObject().startObject().field("type", "linestring").startArray("coordinates").startArray().value(101.0).value(0.0).endArray().startArray().value(102.0).value(1.0).endArray().endArray().endObject().endArray().endObject().endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
GeoShapeQueryBuilder filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0)))).relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 0);
filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(99.0, -1.0).coordinate(99.0, 3.0).coordinate(103.0, 3.0).coordinate(103.0, -1.0).coordinate(99.0, -1.0))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(199.0, -11.0).coordinate(199.0, 13.0).coordinate(193.0, 13.0).coordinate(193.0, -11.0).coordinate(199.0, -11.0)))).relation(ShapeRelation.INTERSECTS);
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 1);
// no shape
filter = QueryBuilders.geoShapeQuery("geo", new GeometryCollectionBuilder());
result = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(filter).get();
assertSearchResponse(result);
assertHitCount(result, 0);
}
Aggregations