Search in sources :

Example 1 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.

the class GeoShapeIntegrationIT method testIndexPolygonDateLine.

public void testIndexPolygonDateLine() throws Exception {
    String mappingVector = "{\n" + "    \"properties\": {\n" + "      \"shape\": {\n" + "        \"type\": \"geo_shape\"\n" + "      }\n" + "    }\n" + "  }";
    String mappingQuad = "{\n" + "    \"properties\": {\n" + "      \"shape\": {\n" + "        \"type\": \"geo_shape\",\n" + "        \"tree\": \"quadtree\"\n" + "      }\n" + "    }\n" + "  }";
    // create index
    assertAcked(client().admin().indices().prepareCreate("vector").addMapping("doc", mappingVector, XContentType.JSON).get());
    ensureGreen();
    assertAcked(client().admin().indices().prepareCreate("quad").addMapping("doc", mappingQuad, XContentType.JSON).get());
    ensureGreen();
    String source = "{\n" + "    \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\"" + "}";
    indexRandom(true, client().prepareIndex("quad").setId("0").setSource(source, XContentType.JSON));
    indexRandom(true, client().prepareIndex("vector").setId("0").setSource(source, XContentType.JSON));
    try {
        ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
        updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true));
        assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
        SearchResponse searchResponse = client().prepareSearch("quad").setQuery(geoShapeQuery("shape", new PointBuilder(-179.75, 1))).get();
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
        searchResponse = client().prepareSearch("quad").setQuery(geoShapeQuery("shape", new PointBuilder(90, 1))).get();
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));
        searchResponse = client().prepareSearch("quad").setQuery(geoShapeQuery("shape", new PointBuilder(-180, 1))).get();
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
        searchResponse = client().prepareSearch("quad").setQuery(geoShapeQuery("shape", new PointBuilder(180, 1))).get();
        assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
    } finally {
        ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest();
        updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null));
        assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
    }
    SearchResponse searchResponse = client().prepareSearch("vector").setQuery(geoShapeQuery("shape", new PointBuilder(90, 1))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));
    searchResponse = client().prepareSearch("vector").setQuery(geoShapeQuery("shape", new PointBuilder(-179.75, 1))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
    searchResponse = client().prepareSearch("vector").setQuery(geoShapeQuery("shape", new PointBuilder(-180, 1))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
    searchResponse = client().prepareSearch("vector").setQuery(geoShapeQuery("shape", new PointBuilder(180, 1))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
}
Also used : PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterUpdateSettingsRequest(org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.

the class ExternalMapper method parse.

@Override
public void parse(ParseContext context) throws IOException {
    byte[] bytes = "Hello world".getBytes(Charset.defaultCharset());
    binMapper.parse(context.createExternalValueContext(bytes));
    boolMapper.parse(context.createExternalValueContext(true));
    // Let's add a Dummy Point
    double lat = 42.0;
    double lng = 51.0;
    ArrayList<GeoPoint> points = new ArrayList<>();
    points.add(new GeoPoint(lat, lng));
    pointMapper.parse(context.createExternalValueContext(points));
    // Let's add a Dummy Shape
    if (shapeMapper instanceof GeoShapeFieldMapper) {
        shapeMapper.parse(context.createExternalValueContext(new Point(-100, 45)));
    } else {
        PointBuilder pb = new PointBuilder(-100, 45);
        shapeMapper.parse(context.createExternalValueContext(pb.buildS4J()));
    }
    context = context.createExternalValueContext(generatedValue);
    // Let's add a Original String
    stringMapper.parse(context);
    multiFields.parse(this, context);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) ArrayList(java.util.ArrayList) Point(org.opensearch.geometry.Point) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 3 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.

the class ShapeBuilderTests method testNewPoint.

public void testNewPoint() {
    PointBuilder pb = new PointBuilder().coordinate(-100, 45);
    Point point = pb.buildS4J();
    assertEquals(-100D, point.getX(), 0.0d);
    assertEquals(45D, point.getY(), 0.0d);
    org.opensearch.geometry.Point geoPoint = pb.buildGeometry();
    assertEquals(-100D, geoPoint.getX(), 0.0d);
    assertEquals(45D, geoPoint.getY(), 0.0d);
}
Also used : PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Point(org.locationtech.spatial4j.shape.Point)

Example 4 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.

the class GeoWKTShapeParserTests method testUnexpectedShapeException.

public void testUnexpectedShapeException() throws IOException {
    XContentBuilder builder = toWKTContent(new PointBuilder(-1, 2), false);
    XContentParser parser = createParser(builder);
    parser.nextToken();
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> GeoWKTParser.parseExpectedType(parser, GeoShapeType.POLYGON));
    assertThat(e, hasToString(containsString("Expected geometry type [polygon] but found [point]")));
}
Also used : MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with PointBuilder

use of org.opensearch.common.geo.builders.PointBuilder in project OpenSearch by opensearch-project.

the class GeoWKTShapeParserTests method testParsePoint.

@Override
public void testParsePoint() throws IOException, ParseException {
    GeoPoint p = RandomShapeGenerator.randomPoint(random());
    Coordinate c = new Coordinate(p.lon(), p.lat());
    Point expected = GEOMETRY_FACTORY.createPoint(c);
    assertExpected(new JtsPoint(expected, SPATIAL_CONTEXT), new PointBuilder().coordinate(c), true);
    assertExpected(new org.opensearch.geometry.Point(p.lon(), p.lat()), new PointBuilder().coordinate(c), false);
    assertMalformed(new PointBuilder().coordinate(c));
}
Also used : MultiPointBuilder(org.opensearch.common.geo.builders.MultiPointBuilder) PointBuilder(org.opensearch.common.geo.builders.PointBuilder) Coordinate(org.locationtech.jts.geom.Coordinate) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) MultiPoint(org.opensearch.geometry.MultiPoint) JtsPoint(org.locationtech.spatial4j.shape.jts.JtsPoint) Point(org.locationtech.jts.geom.Point)

Aggregations

PointBuilder (org.opensearch.common.geo.builders.PointBuilder)13 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)9 SearchResponse (org.opensearch.action.search.SearchResponse)6 Coordinate (org.locationtech.jts.geom.Coordinate)5 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)5 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Point (org.locationtech.spatial4j.shape.Point)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)2 EnvelopeBuilder (org.opensearch.common.geo.builders.EnvelopeBuilder)2 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)2 LineStringBuilder (org.opensearch.common.geo.builders.LineStringBuilder)2 PolygonBuilder (org.opensearch.common.geo.builders.PolygonBuilder)2 ShapeBuilder (org.opensearch.common.geo.builders.ShapeBuilder)2 MultiPoint (org.opensearch.geometry.MultiPoint)2 Point (org.opensearch.geometry.Point)2 GeoShapeQueryBuilder (org.opensearch.index.query.GeoShapeQueryBuilder)2 ArrayList (java.util.ArrayList)1 ConvexHull (org.locationtech.jts.algorithm.ConvexHull)1