use of org.opensearch.common.geo.builders.CoordinatesBuilder 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.common.geo.builders.CoordinatesBuilder in project OpenSearch by opensearch-project.
the class GeoQueryTests method testIndexPointsMultiPolygon.
public void testIndexPointsMultiPolygon() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate(defaultIndexName).addMapping("_doc", xcb).get();
ensureGreen();
client().prepareIndex(defaultIndexName).setId("1").setSource(jsonBuilder().startObject().field("name", "Document 1").field(defaultGeoFieldName, "POINT(-30 -30)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("2").setSource(jsonBuilder().startObject().field("name", "Document 2").field(defaultGeoFieldName, "POINT(-40 -40)").endObject()).setRefreshPolicy(IMMEDIATE).get();
client().prepareIndex(defaultIndexName).setId("3").setSource(jsonBuilder().startObject().field("name", "Document 3").field(defaultGeoFieldName, "POINT(-50 -50)").endObject()).setRefreshPolicy(IMMEDIATE).get();
CoordinatesBuilder encloseDocument1Cb = new CoordinatesBuilder();
encloseDocument1Cb.coordinate(new Coordinate(-35, -35)).coordinate(new Coordinate(-35, -25)).coordinate(new Coordinate(-25, -25)).coordinate(new Coordinate(-25, -35)).coordinate(new Coordinate(-35, -35));
PolygonBuilder encloseDocument1Shape = new PolygonBuilder(encloseDocument1Cb);
CoordinatesBuilder encloseDocument2Cb = new CoordinatesBuilder();
encloseDocument2Cb.coordinate(new Coordinate(-55, -55)).coordinate(new Coordinate(-55, -45)).coordinate(new Coordinate(-45, -45)).coordinate(new Coordinate(-45, -55)).coordinate(new Coordinate(-55, -55));
PolygonBuilder encloseDocument2Shape = new PolygonBuilder(encloseDocument2Cb);
MultiPolygonBuilder mp = new MultiPolygonBuilder();
mp.polygon(encloseDocument1Shape).polygon(encloseDocument2Shape);
GeometryCollectionBuilder builder = new GeometryCollectionBuilder().shape(mp);
Geometry geometry = builder.buildGeometry();
SearchResponse searchResponse = client().prepareSearch(defaultIndexName).setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, geometry).relation(ShapeRelation.INTERSECTS)).get();
assertSearchResponse(searchResponse);
assertHitCount(searchResponse, 2);
assertThat(searchResponse.getHits().getAt(0).getId(), not(equalTo("2")));
assertThat(searchResponse.getHits().getAt(1).getId(), not(equalTo("2")));
}
use of org.opensearch.common.geo.builders.CoordinatesBuilder 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")));
}
use of org.opensearch.common.geo.builders.CoordinatesBuilder in project OpenSearch by opensearch-project.
the class GeoPointShapeQueryTests method testQueryMultiLine.
public void testQueryMultiLine() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
CoordinatesBuilder coords1 = new CoordinatesBuilder().coordinate(-35, -35).coordinate(-25, -25);
CoordinatesBuilder coords2 = new CoordinatesBuilder().coordinate(-15, -15).coordinate(-5, -5);
LineStringBuilder lsb1 = new LineStringBuilder(coords1);
LineStringBuilder lsb2 = new LineStringBuilder(coords2);
MultiLineStringBuilder mlb = new MultiLineStringBuilder().linestring(lsb1).linestring(lsb2);
MultiLine multiline = (MultiLine) mlb.buildGeometry();
try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, multiline)).get();
} catch (Exception e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.MULTILINESTRING + " queries"));
}
}
use of org.opensearch.common.geo.builders.CoordinatesBuilder 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