Search in sources :

Example 46 with GeoPoint

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

the class AbstractGeoFieldDataTestCase method assertValues.

private void assertValues(MultiGeoPointValues values, int docId, boolean missing) throws IOException {
    assertEquals(missing == false, values.advanceExact(docId));
    if (missing == false) {
        final int docCount = values.docValueCount();
        for (int i = 0; i < docCount; ++i) {
            final GeoPoint point = values.nextValue();
            assertThat(point.lat(), allOf(greaterThanOrEqualTo(GeoUtils.MIN_LAT), lessThanOrEqualTo(GeoUtils.MAX_LAT)));
            assertThat(point.lon(), allOf(greaterThanOrEqualTo(GeoUtils.MIN_LON), lessThanOrEqualTo(GeoUtils.MAX_LON)));
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) RandomShapeGenerator.randomPoint(org.opensearch.test.geo.RandomShapeGenerator.randomPoint)

Example 47 with GeoPoint

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

the class ScriptDocValuesGeoPointsTests method testGeoGetLatLon.

public void testGeoGetLatLon() throws IOException {
    final double lat1 = randomLat();
    final double lat2 = randomLat();
    final double lon1 = randomLon();
    final double lon2 = randomLon();
    GeoPoint[][] points = { { new GeoPoint(lat1, lon1), new GeoPoint(lat2, lon2) } };
    final MultiGeoPointValues values = wrap(points);
    final ScriptDocValues.GeoPoints script = new ScriptDocValues.GeoPoints(values);
    script.setNextDocId(1);
    assertEquals(true, script.isEmpty());
    script.setNextDocId(0);
    assertEquals(false, script.isEmpty());
    assertEquals(new GeoPoint(lat1, lon1), script.getValue());
    assertEquals(lat1, script.getLat(), 0);
    assertEquals(lon1, script.getLon(), 0);
    assertTrue(Arrays.equals(new double[] { lat1, lat2 }, script.getLats()));
    assertTrue(Arrays.equals(new double[] { lon1, lon2 }, script.getLons()));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoints(org.opensearch.index.fielddata.ScriptDocValues.GeoPoints) GeoPoints(org.opensearch.index.fielddata.ScriptDocValues.GeoPoints)

Example 48 with GeoPoint

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

the class GeoPointFieldMapperTests method testNullValue.

public void testNullValue() throws Exception {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "geo_point")));
    Mapper fieldMapper = mapper.mappers().getMapper("field");
    assertThat(fieldMapper, instanceOf(GeoPointFieldMapper.class));
    ParsedDocument doc = mapper.parse(source(b -> b.nullField("field")));
    assertThat(doc.rootDoc().getField("field"), nullValue());
    assertThat(doc.rootDoc().getFields(FieldNamesFieldMapper.NAME).length, equalTo(0));
    mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "geo_point").field("doc_values", false)));
    fieldMapper = mapper.mappers().getMapper("field");
    assertThat(fieldMapper, instanceOf(GeoPointFieldMapper.class));
    doc = mapper.parse(source(b -> b.nullField("field")));
    assertThat(doc.rootDoc().getField("field"), nullValue());
    assertThat(doc.rootDoc().getFields(FieldNamesFieldMapper.NAME).length, equalTo(0));
    mapper = createDocumentMapper(fieldMapping(b -> b.field("type", "geo_point").field("null_value", "1,2")));
    fieldMapper = mapper.mappers().getMapper("field");
    assertThat(fieldMapper, instanceOf(GeoPointFieldMapper.class));
    AbstractPointGeometryFieldMapper.ParsedPoint nullValue = ((GeoPointFieldMapper) fieldMapper).nullValue;
    assertThat(nullValue, equalTo(new GeoPoint(1, 2)));
    doc = mapper.parse(source(b -> b.nullField("field")));
    assertThat(doc.rootDoc().getField("field"), notNullValue());
    BytesRef defaultValue = doc.rootDoc().getBinaryValue("field");
    // Shouldn't matter if we specify the value explicitly or use null value
    doc = mapper.parse(source(b -> b.field("field", "1, 2")));
    assertThat(defaultValue, equalTo(doc.rootDoc().getBinaryValue("field")));
    doc = mapper.parse(source(b -> b.field("field", "3, 4")));
    assertThat(defaultValue, not(equalTo(doc.rootDoc().getBinaryValue("field"))));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) Matchers.not(org.hamcrest.Matchers.not) Set(java.util.Set) IOException(java.io.IOException) Matchers.both(org.hamcrest.Matchers.both) GeoUtils(org.opensearch.common.geo.GeoUtils) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) GeoPoint(org.opensearch.common.geo.GeoPoint) XContentFactory(org.opensearch.common.xcontent.XContentFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) Geohash.stringEncode(org.opensearch.geometry.utils.Geohash.stringEncode) GeoPoint(org.opensearch.common.geo.GeoPoint) BytesRef(org.apache.lucene.util.BytesRef)

Example 49 with GeoPoint

use of org.opensearch.common.geo.GeoPoint 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 50 with GeoPoint

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

the class DistanceFeatureQueryBuilderTests method doCreateTestQueryBuilder.

@Override
protected DistanceFeatureQueryBuilder doCreateTestQueryBuilder() {
    String field = randomFrom(DATE_FIELD_NAME, DATE_NANOS_FIELD_NAME, GEO_POINT_FIELD_NAME);
    Origin origin;
    String pivot;
    switch(field) {
        case GEO_POINT_FIELD_NAME:
            GeoPoint point = new GeoPoint(randomDouble(), randomDouble());
            origin = randomBoolean() ? new Origin(point) : new Origin(point.geohash());
            pivot = randomFrom(DistanceUnit.values()).toString(randomDouble());
            break;
        case DATE_FIELD_NAME:
            long randomDateMills = randomLongBetween(0, 2_000_000_000_000L);
            origin = randomBoolean() ? new Origin(randomDateMills) : new Origin(new DateTime(randomDateMills).toString());
            pivot = randomTimeValue(1, 1000, "d", "h", "ms", "s", "m");
            break;
        default:
            // DATE_NANOS_FIELD_NAME
            randomDateMills = randomLongBetween(0, 2_000_000_000_000L);
            if (randomBoolean()) {
                // nano_dates long accept milliseconds since epoch
                origin = new Origin(randomDateMills);
            } else {
                long randomNanos = randomLongBetween(0, 1_000_000L);
                Instant randomDateNanos = Instant.ofEpochMilli(randomDateMills).plusNanos(randomNanos);
                origin = new Origin(randomDateNanos.toString());
            }
            pivot = randomTimeValue(1, 100_000_000, "nanos");
            break;
    }
    return new DistanceFeatureQueryBuilder(field, origin, pivot);
}
Also used : Origin(org.opensearch.index.query.DistanceFeatureQueryBuilder.Origin) GeoPoint(org.opensearch.common.geo.GeoPoint) Instant(java.time.Instant) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime)

Aggregations

GeoPoint (org.opensearch.common.geo.GeoPoint)150 SearchResponse (org.opensearch.action.search.SearchResponse)41 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)6 Bucket (org.opensearch.search.aggregations.bucket.range.Range.Bucket)6 Map (java.util.Map)5 Document (org.apache.lucene.document.Document)5 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)5 Query (org.apache.lucene.search.Query)5