Search in sources :

Example 1 with GeoGrid

use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid 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));
        }
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) Bucket(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with GeoGrid

use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid in project OpenSearch by opensearch-project.

the class GeoHashGridIT method testFiltered.

public void testFiltered() throws Exception {
    GeoBoundingBoxQueryBuilder bbox = new GeoBoundingBoxQueryBuilder("location");
    bbox.setCorners(smallestGeoHash).queryName("bbox");
    for (int precision = 1; precision <= PRECISION; precision++) {
        SearchResponse response = client().prepareSearch("idx").addAggregation(AggregationBuilders.filter("filtered", bbox).subAggregation(geohashGrid("geohashgrid").field("location").precision(precision))).get();
        assertSearchResponse(response);
        Filter filter = response.getAggregations().get("filtered");
        GeoGrid geoGrid = filter.getAggregations().get("geohashgrid");
        for (GeoGrid.Bucket cell : geoGrid.getBuckets()) {
            String geohash = cell.getKeyAsString();
            long bucketCount = cell.getDocCount();
            int expectedBucketCount = expectedDocCountsForGeoHash.get(geohash);
            assertNotSame(bucketCount, 0);
            assertTrue("Buckets must be filtered", geohash.startsWith(smallestGeoHash));
            assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
        }
    }
}
Also used : GeoBoundingBoxQueryBuilder(org.opensearch.index.query.GeoBoundingBoxQueryBuilder) Bucket(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket) Filter(org.opensearch.search.aggregations.bucket.filter.Filter) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 3 with GeoGrid

use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid in project OpenSearch by opensearch-project.

the class ShardReduceIT method testGeoHashGrid.

public void testGeoHashGrid() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(geohashGrid("grid").field("location").subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).get();
    assertSearchResponse(response);
    GeoGrid grid = response.getAggregations().get("grid");
    Histogram histo = grid.getBuckets().iterator().next().getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : AggregationBuilders.dateHistogram(org.opensearch.search.aggregations.AggregationBuilders.dateHistogram) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with GeoGrid

use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid 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);
        }
    }
}
Also used : Bucket(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.opensearch.common.geo.GeoPoint) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with GeoGrid

use of org.opensearch.search.aggregations.bucket.geogrid.GeoGrid in project OpenSearch by opensearch-project.

the class GeoCentroidIT method testSingleValueFieldAsSubAggToGeohashGrid.

public void testSingleValueFieldAsSubAggToGeohashGrid() throws Exception {
    SearchResponse response = client().prepareSearch(HIGH_CARD_IDX_NAME).addAggregation(geohashGrid("geoGrid").field(SINGLE_VALUED_FIELD_NAME).subAggregation(geoCentroid(aggName).field(SINGLE_VALUED_FIELD_NAME))).get();
    assertSearchResponse(response);
    GeoGrid grid = response.getAggregations().get("geoGrid");
    assertThat(grid, notNullValue());
    assertThat(grid.getName(), equalTo("geoGrid"));
    List<? extends GeoGrid.Bucket> buckets = grid.getBuckets();
    for (GeoGrid.Bucket cell : buckets) {
        String geohash = cell.getKeyAsString();
        GeoPoint expectedCentroid = expectedCentroidsForGeoHash.get(geohash);
        GeoCentroid centroidAgg = cell.getAggregations().get(aggName);
        assertThat("Geohash " + geohash + " has wrong centroid latitude ", expectedCentroid.lat(), closeTo(centroidAgg.centroid().lat(), GEOHASH_TOLERANCE));
        assertThat("Geohash " + geohash + " has wrong centroid longitude", expectedCentroid.lon(), closeTo(centroidAgg.centroid().lon(), GEOHASH_TOLERANCE));
    }
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) GeoGrid(org.opensearch.search.aggregations.bucket.geogrid.GeoGrid) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.opensearch.action.search.SearchResponse)9 GeoGrid (org.opensearch.search.aggregations.bucket.geogrid.GeoGrid)9 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)9 GeoPoint (org.opensearch.common.geo.GeoPoint)7 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Bucket (org.opensearch.search.aggregations.bucket.geogrid.GeoGrid.Bucket)5 AggregationBuilders.dateHistogram (org.opensearch.search.aggregations.AggregationBuilders.dateHistogram)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 GeoBoundingBoxQueryBuilder (org.opensearch.index.query.GeoBoundingBoxQueryBuilder)1 Filter (org.opensearch.search.aggregations.bucket.filter.Filter)1