Search in sources :

Example 6 with GeoHashGrid

use of org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid in project elasticsearch by elastic.

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)).execute().actionGet();
        assertSearchResponse(response);
        GeoHashGrid geoGrid = response.getAggregations().get("geohashgrid");
        List<Bucket> buckets = geoGrid.getBuckets();
        Object[] propertiesKeys = (Object[]) geoGrid.getProperty("_key");
        Object[] propertiesDocCounts = (Object[]) geoGrid.getProperty("_count");
        for (int i = 0; i < buckets.size(); i++) {
            GeoHashGrid.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.elasticsearch.common.geo.GeoPoint) Bucket(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with GeoHashGrid

use of org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid in project elasticsearch by elastic.

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)).execute().actionGet();
        assertSearchResponse(response);
        GeoHashGrid 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 (GeoHashGrid.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.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with GeoHashGrid

use of org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid in project elasticsearch by elastic.

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))).execute().actionGet();
        assertSearchResponse(response);
        Filter filter = response.getAggregations().get("filtered");
        GeoHashGrid geoGrid = filter.getAggregations().get("geohashgrid");
        for (GeoHashGrid.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.elasticsearch.index.query.GeoBoundingBoxQueryBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Bucket(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)8 GeoHashGrid (org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)8 GeoPoint (org.elasticsearch.common.geo.GeoPoint)7 Bucket (org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 GeoBoundingBoxQueryBuilder (org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder)1 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)1 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)1 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)1 GeoCentroid (org.elasticsearch.search.aggregations.metrics.geocentroid.GeoCentroid)1