use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoQueryTests method testRectangleSpanningDateline.
public void testRectangleSpanningDateline() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 0)").endObject()).setRefreshPolicy(IMMEDIATE).get();
Rectangle rectangle = new Rectangle(169, -178, 1, -1);
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", rectangle);
SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertHitCount(response, 2);
SearchHits searchHits = response.getHits();
assertThat(searchHits.getAt(0).getId(), not(equalTo("1")));
assertThat(searchHits.getAt(1).getId(), not(equalTo("1")));
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testIndexRectangleSpanningDateLine.
public void testIndexRectangleSpanningDateLine() throws Exception {
String mapping = Strings.toString(createRandomMapping());
client().admin().indices().prepareCreate("test").addMapping("type1", mapping, XContentType.JSON).get();
ensureGreen();
EnvelopeBuilder envelopeBuilder = new EnvelopeBuilder(new Coordinate(178, 10), new Coordinate(-178, -10));
XContentBuilder docSource = envelopeBuilder.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
ShapeBuilder filterShape = new PointBuilder(179, 0);
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", filterShape);
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 testRandomGeoCollectionQuery.
public void testRandomGeoCollectionQuery() throws Exception {
// Create a random geometry collection to index.
GeometryCollectionBuilder gcb = RandomShapeGenerator.createGeometryCollection(random());
org.apache.lucene.geo.Polygon randomPoly = GeoTestUtil.nextPolygon();
assumeTrue("Skipping the check for the polygon with a degenerated dimension", randomPoly.maxLat - randomPoly.minLat > 8.4e-8 && randomPoly.maxLon - randomPoly.minLon > 8.4e-8);
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 mapping = createRandomMapping();
Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
client().admin().indices().prepareCreate("test").addMapping("_doc", mapping).setSettings(settings).get();
ensureGreen();
XContentBuilder docSource = gcb.toXContent(jsonBuilder().startObject().field("geo"), null).endObject();
client().prepareIndex("test").setId("1").setSource(docSource).setRefreshPolicy(IMMEDIATE).get();
// Create a random geometry collection to query
GeometryCollectionBuilder queryCollection = RandomShapeGenerator.createGeometryCollection(random());
queryCollection.shape(new PolygonBuilder(cb));
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", queryCollection);
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse result = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertSearchResponse(result);
assertThat(result.getHits().getHits().length, greaterThan(0));
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoQueryTests method testPolygonSpanningDateline.
public void testPolygonSpanningDateline() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("4").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
PolygonBuilder polygon = new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(177, 10).coordinate(177, 5).coordinate(-177, 5).coordinate(-177, 10));
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", polygon.buildGeometry());
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertHitCount(response, 2);
SearchHits searchHits = response.getHits();
assertThat(searchHits.getAt(0).getId(), not(equalTo("1")));
assertThat(searchHits.getAt(1).getId(), not(equalTo("1")));
assertThat(searchHits.getAt(0).getId(), not(equalTo("4")));
assertThat(searchHits.getAt(1).getId(), not(equalTo("4")));
}
use of org.opensearch.index.query.GeoShapeQueryBuilder in project OpenSearch by opensearch-project.
the class GeoQueryTests method testMultiPolygonSpanningDateline.
public void testMultiPolygonSpanningDateline() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-169 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(-179 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field(defaultGeoFieldName, "POINT(171 7)").endObject()).setRefreshPolicy(IMMEDIATE).get();
MultiPolygonBuilder multiPolygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-167, 10).coordinate(-171, 10).coordinate(171, 5).coordinate(-167, 5).coordinate(-167, 10))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-177, 10).coordinate(177, 10).coordinate(177, 5).coordinate(-177, 5).coordinate(-177, 10)));
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("geo", multiPolygon.buildGeometry());
geoShapeQueryBuilder.relation(ShapeRelation.INTERSECTS);
SearchResponse response = client().prepareSearch("test").setQuery(geoShapeQueryBuilder).get();
assertHitCount(response, 2);
SearchHits searchHits = response.getHits();
assertThat(searchHits.getAt(0).getId(), not(equalTo("3")));
assertThat(searchHits.getAt(1).getId(), not(equalTo("3")));
}
Aggregations