use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.
the class GeoPointShapeQueryTests method testQueryPoint.
public void testQueryPoint() throws Exception {
XContentBuilder xcb = createDefaultMapping();
client().admin().indices().prepareCreate("test").addMapping("_doc", xcb).get();
ensureGreen();
PointBuilder pb = new PointBuilder().coordinate(-35, -25);
Point point = pb.buildGeometry();
try {
client().prepareSearch("test").setQuery(QueryBuilders.geoShapeQuery(defaultGeoFieldName, point)).get();
} catch (Exception e) {
assertThat(e.getCause().getMessage(), containsString("does not support " + GeoShapeType.POINT + " queries"));
}
}
use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.
the class GeoShapeQueryTests method testDistanceQuery.
public void testDistanceQuery() throws Exception {
String mapping = Strings.toString(createRandomMapping());
client().admin().indices().prepareCreate("test_distance").addMapping("type1", mapping, XContentType.JSON).get();
ensureGreen();
CircleBuilder circleBuilder = new CircleBuilder().center(new Coordinate(1, 0)).radius(350, DistanceUnit.KILOMETERS);
client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(2, 2)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(3, 1)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(-20, -30)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
client().index(new IndexRequest("test_distance").source(jsonBuilder().startObject().field("geo", new PointBuilder(20, 30)).endObject()).setRefreshPolicy(IMMEDIATE)).actionGet();
SearchResponse response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.WITHIN)).get();
assertEquals(2, response.getHits().getTotalHits().value);
response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.INTERSECTS)).get();
assertEquals(2, response.getHits().getTotalHits().value);
response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.DISJOINT)).get();
assertEquals(2, response.getHits().getTotalHits().value);
response = client().prepareSearch("test_distance").setQuery(QueryBuilders.geoShapeQuery("geo", circleBuilder.buildGeometry()).relation(ShapeRelation.CONTAINS)).get();
assertEquals(0, response.getHits().getTotalHits().value);
}
use of org.opensearch.common.geo.builders.PointBuilder 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.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.
the class GeoFilterIT method testShapeRelations.
public void testShapeRelations() throws Exception {
assertTrue("Intersect relation is not supported", intersectSupport);
assertTrue("Disjoint relation is not supported", disjointSupport);
assertTrue("within relation is not supported", withinSupport);
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("polygon").startObject("properties").startObject("area").field("type", "geo_shape").field("tree", "geohash").endObject().endObject().endObject().endObject());
CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("shapes").addMapping("polygon", mapping, XContentType.JSON);
mappingRequest.get();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().get();
// Create a multipolygon with two polygons. The first is an rectangle of size 10x10
// with a hole of size 5x5 equidistant from all sides. This hole in turn contains
// the second polygon of size 4x4 equidistant from all sites
MultiPolygonBuilder polygon = new MultiPolygonBuilder().polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()))).polygon(new PolygonBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
BytesReference data = BytesReference.bytes(jsonBuilder().startObject().field("area", polygon).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// Point in polygon
SearchResponse result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(3, 3))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
// Point in polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 0);
// by definition the border of a polygon belongs to the inner
// so the border of a polygons hole also belongs to the inner
// of the polygon NOT the hole
// Point on polygon border
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(10.0, 5.0))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
// Point on hole border
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(5.0, 2.0))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
if (disjointSupport) {
// Point not in polygon
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(3, 3))).get();
assertHitCount(result, 0);
// Point in polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoDisjointQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("1"));
}
// Create a polygon that fills the empty area of the polygon defined above
PolygonBuilder inverse = new PolygonBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(5, 5).coordinate(5, -5).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-4, -4).coordinate(-4, 4).coordinate(4, 4).coordinate(4, -4).close()));
data = BytesReference.bytes(jsonBuilder().startObject().field("area", inverse).endObject());
client().prepareIndex("shapes").setId("2").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// re-check point on polygon hole
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(4.5, 4.5))).get();
assertHitCount(result, 1);
assertFirstHit(result, hasId("2"));
// Create Polygon with hole and common edge
PolygonBuilder builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-10, -10).coordinate(-10, 10).coordinate(10, 10).coordinate(10, -10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(-5, -5).coordinate(-5, 5).coordinate(10, 5).coordinate(10, -5).close()));
if (withinSupport) {
// Polygon WithIn Polygon
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(-30, -30).coordinate(-30, 30).coordinate(30, 30).coordinate(30, -30).close());
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoWithinQuery("area", builder.buildGeometry())).get();
assertHitCount(result, 2);
}
// Create a polygon crossing longitude 180.
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close());
data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
// Create a polygon crossing longitude 180 with hole.
builder = new PolygonBuilder(new CoordinatesBuilder().coordinate(170, -10).coordinate(190, -10).coordinate(190, 10).coordinate(170, 10).close()).hole(new LineStringBuilder(new CoordinatesBuilder().coordinate(175, -5).coordinate(185, -5).coordinate(185, 5).coordinate(175, 5).close()));
data = BytesReference.bytes(jsonBuilder().startObject().field("area", builder).endObject());
client().prepareIndex("shapes").setId("1").setSource(data, XContentType.JSON).get();
client().admin().indices().prepareRefresh().get();
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(174, -4).buildGeometry())).get();
assertHitCount(result, 1);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(-174, -4).buildGeometry())).get();
assertHitCount(result, 1);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -4).buildGeometry())).get();
assertHitCount(result, 0);
result = client().prepareSearch().setQuery(matchAllQuery()).setPostFilter(QueryBuilders.geoIntersectionQuery("area", new PointBuilder(180, -6).buildGeometry())).get();
assertHitCount(result, 1);
}
use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.
the class GeoWKTParser method parsePoint.
private static PointBuilder parsePoint(StreamTokenizer stream, final boolean ignoreZValue, final boolean coerce) throws IOException, OpenSearchParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return null;
}
PointBuilder pt = new PointBuilder(nextNumber(stream), nextNumber(stream));
if (isNumberNext(stream)) {
GeoPoint.assertZValue(ignoreZValue, nextNumber(stream));
}
nextCloser(stream);
return pt;
}
Aggregations