Java Code Examples for org.elasticsearch.search.aggregations.InternalAggregations#reduce()

The following examples show how to use org.elasticsearch.search.aggregations.InternalAggregations#reduce() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalTerms.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Bucket reduce(List<? extends Bucket> buckets, ReduceContext context) {
    long docCount = 0;
    long docCountError = 0;
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    for (Bucket bucket : buckets) {
        docCount += bucket.docCount;
        if (docCountError != -1) {
            if (bucket.docCountError == -1) {
                docCountError = -1;
            } else {
                docCountError += bucket.docCountError;
            }
        }
        aggregationsList.add(bucket.aggregations);
    }
    InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
    return newBucket(docCount, aggs, docCountError);
}
 
Example 2
Source File: InternalPathHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
/**
 * Utility method of InternalPathHierarchy.doReduce()
 */
@Override
protected InternalBucket reduceBucket(List<InternalBucket> buckets, ReduceContext context) {
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    InternalBucket reduced = null;
    for (InternalBucket bucket : buckets) {
        if (reduced == null) {
            reduced = bucket;
        } else {
            reduced.docCount += bucket.docCount;
        }
        aggregationsList.add(bucket.aggregations);
    }
    reduced.aggregations = InternalAggregations.reduce(aggregationsList, context);
    return reduced;
}
 
Example 3
Source File: InternalGeoPointClustering.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 6 votes vote down vote up
@Override
public Bucket reduceBucket(List<Bucket> buckets, ReduceContext context) {
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    long docCount = 0;
    double centroidLat = 0;
    double centroidLon = 0;
    long geohashAsLong = 0;
    for (Bucket bucket : buckets) {
        docCount += bucket.docCount;
        centroidLat += bucket.centroid.getLat() * bucket.docCount;
        centroidLon += bucket.centroid.getLon() * bucket.docCount;
        aggregationsList.add(bucket.aggregations);
        geohashAsLong = bucket.geohashAsLong;
    }
    final InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
    return new Bucket(geohashAsLong, new GeoPoint(
            centroidLat / docCount, centroidLon / docCount), docCount, aggs);
}
 
Example 4
Source File: InternalSignificantTerms.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Bucket reduce(List<? extends Bucket> buckets, ReduceContext context) {
    long subsetDf = 0;
    long supersetDf = 0;
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    for (Bucket bucket : buckets) {
        subsetDf += bucket.subsetDf;
        supersetDf += bucket.supersetDf;
        aggregationsList.add(bucket.aggregations);
    }
    InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
    return newBucket(subsetDf, subsetSize, supersetDf, supersetSize, aggs);
}
 
Example 5
Source File: InternalRange.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
Bucket reduce(List<Bucket> ranges, ReduceContext context) {
    long docCount = 0;
    List<InternalAggregations> aggregationsList = new ArrayList<>(ranges.size());
    for (Bucket range : ranges) {
        docCount += range.docCount;
        aggregationsList.add(range.aggregations);
    }
    final InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
    return getFactory().createBucket(key, from, to, docCount, aggs, keyed, formatter);
}
 
Example 6
Source File: InternalGeoHashGrid.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Bucket reduce(List<? extends Bucket> buckets, ReduceContext context) {
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    long docCount = 0;
    for (Bucket bucket : buckets) {
        docCount += bucket.docCount;
        aggregationsList.add(bucket.aggregations);
    }
    final InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
    return new Bucket(geohashAsLong, docCount, aggs);
}
 
Example 7
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
<B extends Bucket> B reduce(List<B> buckets, ReduceContext context) {
    List<InternalAggregations> aggregations = new ArrayList<>(buckets.size());
    long docCount = 0;
    for (Bucket bucket : buckets) {
        docCount += bucket.docCount;
        aggregations.add((InternalAggregations) bucket.getAggregations());
    }
    InternalAggregations aggs = InternalAggregations.reduce(aggregations, context);
    return (B) getFactory().createBucket(key, docCount, aggs, keyed, formatter);
}
 
Example 8
Source File: InternalFilters.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
Bucket reduce(List<Bucket> buckets, ReduceContext context) {
    Bucket reduced = null;
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    for (Bucket bucket : buckets) {
        if (reduced == null) {
            reduced = new Bucket(bucket.key, bucket.docCount, bucket.aggregations, bucket.keyed);
        } else {
            reduced.docCount += bucket.docCount;
        }
        aggregationsList.add(bucket.aggregations);
    }
    reduced.aggregations = InternalAggregations.reduce(aggregationsList, context);
    return reduced;
}
 
Example 9
Source File: InternalDateHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
@Override
protected InternalBucket reduceBucket(List<InternalBucket> buckets, ReduceContext context) {
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    InternalBucket reduced = null;
    for (InternalBucket bucket : buckets) {
        if (reduced == null) {
            reduced = bucket;
        } else {
            reduced.docCount += bucket.docCount;
        }
        aggregationsList.add(bucket.aggregations);
    }
    reduced.aggregations = InternalAggregations.reduce(aggregationsList, context);
    return reduced;
}
 
Example 10
Source File: InternalGeoShape.java    From elasticsearch-plugin-geoshape with MIT License 5 votes vote down vote up
@Override
public InternalBucket reduceBucket(List<InternalBucket> buckets, ReduceContext context) {
    List<InternalAggregations> aggregationsList = new ArrayList<>(buckets.size());
    InternalBucket reduced = null;
    for (InternalBucket bucket : buckets) {
        if (reduced == null) {
            reduced = bucket;
        } else {
            reduced.docCount += bucket.docCount;
        }
        aggregationsList.add(bucket.aggregations);
    }
    reduced.aggregations = InternalAggregations.reduce(aggregationsList, context);
    return reduced;
}
 
Example 11
Source File: InternalGeoPointClustering.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 5 votes vote down vote up
private void computeDistance(Bucket bucket, Bucket potentialNeighbor, List<Bucket> revisit, ReduceContext reduceContext) {
    if (potentialNeighbor.visited) {
        return;
    }

    double neighborDistance = GeoUtils.arcDistance(
            bucket.centroid.lat(),
            bucket.centroid.lon(),
            potentialNeighbor.centroid.lat(),
            potentialNeighbor.centroid.lon()
    );

    double avgLat = (bucket.centroid.lat() + potentialNeighbor.centroid.lat()) / 2;

    double fixedRadius = radius * Math.cos(DistanceUtils.toRadians(avgLat));

    if (neighborDistance <= fixedRadius) {
        potentialNeighbor.visited = true;
        long mergedDocCount = bucket.docCount + potentialNeighbor.docCount;
        double newCentroidLat = (bucket.centroid.getLat() * bucket.docCount +
                potentialNeighbor.centroid.getLat() * potentialNeighbor.docCount) / mergedDocCount;
        double newCentroidLon = (bucket.centroid.getLon() * bucket.docCount +
                potentialNeighbor.centroid.getLon() * potentialNeighbor.docCount) / mergedDocCount;
        bucket.centroid = new GeoPoint(newCentroidLat, newCentroidLon);

        bucket.docCount = mergedDocCount;
        List<InternalAggregations> aggregationsList = new ArrayList<>();
        aggregationsList.add(bucket.aggregations);
        aggregationsList.add(potentialNeighbor.aggregations);
        bucket.aggregations = InternalAggregations.reduce(aggregationsList, reduceContext);
        bucket.geohashesList.add(potentialNeighbor.geohashAsLong);
    } else if (revisit != null && ratio > 0 && neighborDistance / fixedRadius < ratio ) {
        revisit.add(potentialNeighbor);
    }
}