Search in sources :

Example 56 with GeoPoint

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

the class GeoPolygonQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(GeoPolygonQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
    MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
    if (fieldType == null) {
        assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery);
    } else {
        // TODO: Test case when there are no docValues
        Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery();
        String expectedFieldName = expectedFieldName(queryBuilder.fieldName());
        List<GeoPoint> points = queryBuilder.points();
        double[] lats = new double[points.size()];
        double[] lons = new double[points.size()];
        for (int i = 0; i < points.size(); i++) {
            lats[i] = points.get(i).getLat();
            lons[i] = points.get(i).getLon();
        }
        org.apache.lucene.geo.Polygon polygon = new org.apache.lucene.geo.Polygon(lats, lons);
        assertEquals(LatLonPoint.newPolygonQuery(expectedFieldName, polygon), indexQuery);
        Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery();
        assertEquals(LatLonDocValuesField.newSlowPolygonQuery(expectedFieldName, polygon), dvQuery);
    }
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LatLonPoint(org.apache.lucene.document.LatLonPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery)

Example 57 with GeoPoint

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

the class GeoUtilsTests method testParseGeoPoint.

public void testParseGeoPoint() throws IOException {
    for (int i = 0; i < 100; i++) {
        double lat = randomDouble() * 180 - 90 + randomIntBetween(-1000, 1000) * 180;
        double lon = randomDouble() * 360 - 180 + randomIntBetween(-1000, 1000) * 360;
        XContentBuilder json = jsonBuilder().startObject().field("lat", lat).field("lon", lon).endObject();
        try (XContentParser parser = createParser(json)) {
            parser.nextToken();
            GeoPoint point = GeoUtils.parseGeoPoint(parser);
            assertThat(point, equalTo(new GeoPoint(lat, lon)));
            assertThat(parser.currentToken(), is(Token.END_OBJECT));
            assertNull(parser.nextToken());
        }
        json = jsonBuilder().startObject().field("lat", String.valueOf(lat)).field("lon", String.valueOf(lon)).endObject();
        try (XContentParser parser = createParser(json)) {
            parser.nextToken();
            GeoPoint point = GeoUtils.parseGeoPoint(parser);
            assertThat(point, equalTo(new GeoPoint(lat, lon)));
        }
        json = jsonBuilder().startObject().startArray("foo").value(lon).value(lat).endArray().endObject();
        try (XContentParser parser = createParser(json)) {
            while (parser.currentToken() != Token.START_ARRAY) {
                parser.nextToken();
            }
            GeoPoint point = GeoUtils.parseGeoPoint(parser);
            assertThat(point, equalTo(new GeoPoint(lat, lon)));
        }
        json = jsonBuilder().startObject().field("foo", lat + "," + lon).endObject();
        try (XContentParser parser = createParser(json)) {
            while (parser.currentToken() != Token.VALUE_STRING) {
                parser.nextToken();
            }
            GeoPoint point = GeoUtils.parseGeoPoint(parser);
            assertThat(point, equalTo(new GeoPoint(lat, lon)));
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 58 with GeoPoint

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

the class GeoUtilsTests method testParseGeoPointStringZValueError.

public void testParseGeoPointStringZValueError() throws IOException {
    double lat = randomDouble() * 180 - 90 + randomIntBetween(-1000, 1000) * 180;
    double lon = randomDouble() * 360 - 180 + randomIntBetween(-1000, 1000) * 360;
    double alt = randomDouble() * 1000;
    XContentBuilder json = jsonBuilder().startObject().field("foo", lat + "," + lon + "," + alt).endObject();
    try (XContentParser parser = createParser(json)) {
        while (parser.currentToken() != Token.VALUE_STRING) {
            parser.nextToken();
        }
        Exception e = expectThrows(OpenSearchParseException.class, () -> GeoUtils.parseGeoPoint(parser, new GeoPoint(), false));
        assertThat(e.getMessage(), containsString("but [ignore_z_value] parameter is [false]"));
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchParseException(org.opensearch.OpenSearchParseException) IOException(java.io.IOException)

Example 59 with GeoPoint

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

the class GeoUtilsTests method testNormalizePointInNormalRange.

public void testNormalizePointInNormalRange() {
    for (int i = 0; i < 100; i++) {
        double testLat = (randomDouble() * 180.0) - 90.0;
        double testLon = (randomDouble() * 360.0) - 180.0;
        GeoPoint testPoint = new GeoPoint(testLat, testLon);
        assertNormalizedPoint(testPoint, testPoint);
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 60 with GeoPoint

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

the class GeoUtilsTests method testParseGeoPointGeohashPositions.

public void testParseGeoPointGeohashPositions() throws IOException {
    assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.TOP_LEFT), new GeoPoint(42.890625, -71.71875));
    assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.TOP_RIGHT), new GeoPoint(42.890625, -71.3671875));
    assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.BOTTOM_LEFT), new GeoPoint(42.71484375, -71.71875));
    assertNormalizedPoint(parseGeohash("drt5", GeoUtils.EffectivePoint.BOTTOM_RIGHT), new GeoPoint(42.71484375, -71.3671875));
    assertNormalizedPoint(parseGeohash("drtk", GeoUtils.EffectivePoint.BOTTOM_LEFT), new GeoPoint(42.890625, -71.3671875));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

Aggregations

GeoPoint (org.opensearch.common.geo.GeoPoint)151 SearchResponse (org.opensearch.action.search.SearchResponse)42 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)27 ArrayList (java.util.ArrayList)23 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)19 Matchers.containsString (org.hamcrest.Matchers.containsString)11 XContentParser (org.opensearch.common.xcontent.XContentParser)10 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)10 LatLonPoint (org.apache.lucene.document.LatLonPoint)8 Version (org.opensearch.Version)8 IOException (java.io.IOException)7 LatLonDocValuesField (org.apache.lucene.document.LatLonDocValuesField)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 OpenSearchParseException (org.opensearch.OpenSearchParseException)7 Range (org.opensearch.search.aggregations.bucket.range.Range)7 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)6 Map (java.util.Map)5 Document (org.apache.lucene.document.Document)5 Directory (org.apache.lucene.store.Directory)5 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)5