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());
}
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));
}
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);
}
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));
}
}
}
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);
}
}
Aggregations