Search in sources :

Example 36 with GeoPoint

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

the class InternalGeoCentroidTests method createTestInstance.

@Override
protected InternalGeoCentroid createTestInstance(String name, Map<String, Object> metadata) {
    GeoPoint centroid = RandomGeoGenerator.randomPoint(random());
    // Re-encode lat/longs to avoid rounding issue when testing InternalGeoCentroid#hashCode() and
    // InternalGeoCentroid#equals()
    int encodedLon = GeoEncodingUtils.encodeLongitude(centroid.lon());
    centroid.resetLon(GeoEncodingUtils.decodeLongitude(encodedLon));
    int encodedLat = GeoEncodingUtils.encodeLatitude(centroid.lat());
    centroid.resetLat(GeoEncodingUtils.decodeLatitude(encodedLat));
    long count = randomIntBetween(0, 1000);
    if (count == 0) {
        centroid = null;
    }
    return new InternalGeoCentroid(name, centroid, count, Collections.emptyMap());
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 37 with GeoPoint

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

the class InternalGeoCentroidTests method testReduceMaxCount.

public void testReduceMaxCount() {
    InternalGeoCentroid maxValueGeoCentroid = new InternalGeoCentroid("agg", new GeoPoint(10, 0), Long.MAX_VALUE, Collections.emptyMap());
    InternalGeoCentroid reducedGeoCentroid = maxValueGeoCentroid.reduce(Collections.singletonList(maxValueGeoCentroid), null);
    assertThat(reducedGeoCentroid.count(), equalTo(Long.MAX_VALUE));
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 38 with GeoPoint

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

the class InternalGeoCentroidTests method mutateInstance.

@Override
protected InternalGeoCentroid mutateInstance(InternalGeoCentroid instance) {
    String name = instance.getName();
    GeoPoint centroid = instance.centroid();
    long count = instance.count();
    Map<String, Object> metadata = instance.getMetadata();
    switch(between(0, 2)) {
        case 0:
            name += randomAlphaOfLength(5);
            break;
        case 1:
            count += between(1, 100);
            if (centroid == null) {
                // if the new count is > 0 then we need to make sure there is a
                // centroid or the constructor will throw an exception
                centroid = new GeoPoint(randomDoubleBetween(-90, 90, false), randomDoubleBetween(-180, 180, false));
            }
            break;
        case 2:
            if (centroid == null) {
                centroid = new GeoPoint(randomDoubleBetween(-90, 90, false), randomDoubleBetween(-180, 180, false));
                count = between(1, 100);
            } else {
                GeoPoint newCentroid = new GeoPoint(centroid);
                if (randomBoolean()) {
                    newCentroid.resetLat(centroid.getLat() / 2.0);
                } else {
                    newCentroid.resetLon(centroid.getLon() / 2.0);
                }
                centroid = newCentroid;
            }
            break;
        case 3:
            if (metadata == null) {
                metadata = new HashMap<>(1);
            } else {
                metadata = new HashMap<>(instance.getMetadata());
            }
            metadata.put(randomAlphaOfLength(15), randomInt());
            break;
        default:
            throw new AssertionError("Illegal randomisation branch");
    }
    return new InternalGeoCentroid(name, centroid, count, metadata);
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 39 with GeoPoint

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

the class GeoBoundsAggregatorTests method testRandom.

public void testRandom() throws Exception {
    double top = Double.NEGATIVE_INFINITY;
    double bottom = Double.POSITIVE_INFINITY;
    double posLeft = Double.POSITIVE_INFINITY;
    double posRight = Double.NEGATIVE_INFINITY;
    double negLeft = Double.POSITIVE_INFINITY;
    double negRight = Double.NEGATIVE_INFINITY;
    int numDocs = randomIntBetween(50, 100);
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            int numValues = randomIntBetween(1, 5);
            for (int j = 0; j < numValues; j++) {
                GeoPoint point = RandomGeoGenerator.randomPoint(random());
                if (point.getLat() > top) {
                    top = point.getLat();
                }
                if (point.getLat() < bottom) {
                    bottom = point.getLat();
                }
                if (point.getLon() >= 0 && point.getLon() < posLeft) {
                    posLeft = point.getLon();
                }
                if (point.getLon() >= 0 && point.getLon() > posRight) {
                    posRight = point.getLon();
                }
                if (point.getLon() < 0 && point.getLon() < negLeft) {
                    negLeft = point.getLon();
                }
                if (point.getLon() < 0 && point.getLon() > negRight) {
                    negRight = point.getLon();
                }
                doc.add(new LatLonDocValuesField("field", point.getLat(), point.getLon()));
            }
            w.addDocument(doc);
        }
        GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder("my_agg").field("field").wrapLongitude(false);
        MappedFieldType fieldType = new GeoPointFieldMapper.GeoPointFieldType("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            InternalGeoBounds bounds = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            assertThat(bounds.top, closeTo(top, GEOHASH_TOLERANCE));
            assertThat(bounds.bottom, closeTo(bottom, GEOHASH_TOLERANCE));
            assertThat(bounds.posLeft, closeTo(posLeft, GEOHASH_TOLERANCE));
            assertThat(bounds.posRight, closeTo(posRight, GEOHASH_TOLERANCE));
            assertThat(bounds.negRight, closeTo(negRight, GEOHASH_TOLERANCE));
            assertThat(bounds.negLeft, closeTo(negLeft, GEOHASH_TOLERANCE));
            assertTrue(AggregationInspectionHelper.hasValue(bounds));
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) LatLonDocValuesField(org.apache.lucene.document.LatLonDocValuesField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) GeoPoint(org.opensearch.common.geo.GeoPoint) GeoPoint(org.opensearch.common.geo.GeoPoint) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 40 with GeoPoint

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

the class InternalScriptedMetricTests method assertValues.

private static void assertValues(Object expected, Object actual) {
    if (expected instanceof Long) {
        // longs that fit into the integer range are parsed back as integer
        if (actual instanceof Integer) {
            assertEquals(((Long) expected).intValue(), actual);
        } else {
            assertEquals(expected, actual);
        }
    } else if (expected instanceof Float) {
        // based on the xContent type, floats are sometimes parsed back as doubles
        if (actual instanceof Double) {
            assertEquals(expected, ((Double) actual).floatValue());
        } else {
            assertEquals(expected, actual);
        }
    } else if (expected instanceof GeoPoint) {
        assertTrue(actual instanceof Map);
        GeoPoint point = (GeoPoint) expected;
        Map<String, Object> pointMap = (Map<String, Object>) actual;
        assertEquals(point.getLat(), pointMap.get("lat"));
        assertEquals(point.getLon(), pointMap.get("lon"));
    } else if (expected instanceof Map) {
        Map<String, Object> expectedMap = (Map<String, Object>) expected;
        Map<String, Object> actualMap = (Map<String, Object>) actual;
        assertEquals(expectedMap.size(), actualMap.size());
        for (String key : expectedMap.keySet()) {
            assertValues(expectedMap.get(key), actualMap.get(key));
        }
    } else if (expected instanceof List) {
        List<Object> expectedList = (List<Object>) expected;
        List<Object> actualList = (List<Object>) actual;
        assertEquals(expectedList.size(), actualList.size());
        Iterator<Object> actualIterator = actualList.iterator();
        for (Object element : expectedList) {
            assertValues(element, actualIterator.next());
        }
    } else {
        assertEquals(expected, actual);
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

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