Search in sources :

Example 1 with GeoRect

use of org.vertexium.type.GeoRect in project vertexium by visallo.

the class ElasticsearchGraphQueryIterable method reduceGeohashResults.

private static GeohashResult reduceGeohashResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    Map<Object, List<MultiBucketsAggregation.Bucket>> bucketsByKey = new HashMap<>();
    for (Aggregation agg : aggs) {
        if (agg instanceof GeoHashGrid) {
            GeoHashGrid h = (GeoHashGrid) agg;
            for (GeoHashGrid.Bucket b : h.getBuckets()) {
                List<MultiBucketsAggregation.Bucket> existingBucket = bucketsByKey.computeIfAbsent(b.getKey(), k -> new ArrayList<>());
                existingBucket.add(b);
            }
        } else {
            throw new VertexiumException("Aggregation is not a geohash: " + agg.getClass().getName());
        }
    }
    return new MultiBucketsAggregationReducer<GeohashResult, GeohashBucket>() {

        @Override
        protected GeohashBucket createBucket(final Object key, long count, Map<String, AggregationResult> nestedResults, List<MultiBucketsAggregation.Bucket> buckets) {
            GeoPoint geoPoint = getAverageGeoPointFromBuckets(buckets);
            return new GeohashBucket(key.toString(), count, geoPoint, nestedResults) {

                @Override
                public GeoRect getGeoCell() {
                    org.elasticsearch.common.geo.GeoPoint northWest = new org.elasticsearch.common.geo.GeoPoint();
                    org.elasticsearch.common.geo.GeoPoint southEast = new org.elasticsearch.common.geo.GeoPoint();
                    GeohashUtils.decodeCell(key.toString(), northWest, southEast);
                    return new GeoRect(new GeoPoint(northWest.getLat(), northWest.getLon()), new GeoPoint(southEast.getLat(), southEast.getLon()));
                }
            };
        }

        @Override
        protected GeohashResult bucketsToResults(List<GeohashBucket> buckets) {
            return new GeohashResult(buckets);
        }
    }.reduce(query, bucketsByKey);
}
Also used : GeoRect(org.vertexium.type.GeoRect) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) Aggregation(org.elasticsearch.search.aggregations.Aggregation) GeoPoint(org.vertexium.type.GeoPoint) VertexiumException(org.vertexium.VertexiumException) MultiBucketsAggregation(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation) InternalGeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid)

Aggregations

Aggregation (org.elasticsearch.search.aggregations.Aggregation)1 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)1 GeoHashGrid (org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid)1 InternalGeoHashGrid (org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid)1 VertexiumException (org.vertexium.VertexiumException)1 GeoPoint (org.vertexium.type.GeoPoint)1 GeoRect (org.vertexium.type.GeoRect)1