use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket in project OpenSearch by opensearch-project.
the class GeoHashGridIT method testSimple.
public void testSimple() throws Exception {
for (int precision = 1; precision <= PRECISION; precision++) {
SearchResponse response = client().prepareSearch("idx").addAggregation(geohashGrid("geohashgrid").field("location").precision(precision)).get();
assertSearchResponse(response);
GeoGrid geoGrid = response.getAggregations().get("geohashgrid");
List<? extends Bucket> buckets = geoGrid.getBuckets();
Object[] propertiesKeys = (Object[]) ((InternalAggregation) geoGrid).getProperty("_key");
Object[] propertiesDocCounts = (Object[]) ((InternalAggregation) geoGrid).getProperty("_count");
for (int i = 0; i < buckets.size(); i++) {
GeoGrid.Bucket cell = buckets.get(i);
String geohash = cell.getKeyAsString();
long bucketCount = cell.getDocCount();
int expectedBucketCount = expectedDocCountsForGeoHash.get(geohash);
assertNotSame(bucketCount, 0);
assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
GeoPoint geoPoint = (GeoPoint) propertiesKeys[i];
assertThat(stringEncode(geoPoint.lon(), geoPoint.lat(), precision), equalTo(geohash));
assertThat((long) propertiesDocCounts[i], equalTo(bucketCount));
}
}
}
use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket in project OpenSearch by opensearch-project.
the class GeoHashGridIT method testTopMatch.
public void testTopMatch() throws Exception {
for (int precision = 1; precision <= PRECISION; precision++) {
SearchResponse response = client().prepareSearch("idx").addAggregation(geohashGrid("geohashgrid").field("location").size(1).shardSize(100).precision(precision)).get();
assertSearchResponse(response);
GeoGrid geoGrid = response.getAggregations().get("geohashgrid");
// Check we only have one bucket with the best match for that resolution
assertThat(geoGrid.getBuckets().size(), equalTo(1));
for (GeoGrid.Bucket cell : geoGrid.getBuckets()) {
String geohash = cell.getKeyAsString();
long bucketCount = cell.getDocCount();
int expectedBucketCount = 0;
for (ObjectIntCursor<String> cursor : expectedDocCountsForGeoHash) {
if (cursor.key.length() == precision) {
expectedBucketCount = Math.max(expectedBucketCount, cursor.value);
}
}
assertNotSame(bucketCount, 0);
assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
}
}
}
Aggregations