org.elasticsearch.search.aggregations.InternalAggregations Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.InternalAggregations. 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: 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 #2
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 #3
Source File: CumulativeSumPipelineAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    InternalHistogram histo = (InternalHistogram) aggregation;
    List<? extends InternalHistogram.Bucket> buckets = histo.getBuckets();
    InternalHistogram.Factory<? extends InternalHistogram.Bucket> factory = histo.getFactory();

    List newBuckets = new ArrayList<>();
    double sum = 0;
    for (InternalHistogram.Bucket bucket : buckets) {
        Double thisBucketValue = resolveBucketValue(histo, bucket, bucketsPaths()[0], GapPolicy.INSERT_ZEROS);
        sum += thisBucketValue;
        List<InternalAggregation> aggs = new ArrayList<>(eagerTransform(bucket.getAggregations().asList(),
                AGGREGATION_TRANFORM_FUNCTION));
        aggs.add(new InternalSimpleValue(name(), sum, formatter, new ArrayList<PipelineAggregator>(), metaData()));
        InternalHistogram.Bucket newBucket = factory.createBucket(bucket.getKey(), bucket.getDocCount(),
                new InternalAggregations(aggs), bucket.getKeyed(), bucket.getFormatter());
        newBuckets.add(newBucket);
    }
    return factory.create(newBuckets, histo);
}
 
Example #4
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 #5
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Bucket(long key, long docCount, boolean keyed, ValueFormatter formatter, Factory factory,
        InternalAggregations aggregations) {
    this(keyed, formatter, factory);
    this.key = key;
    this.docCount = docCount;
    this.aggregations = aggregations;
}
 
Example #6
Source File: SignificantStringTerms.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    termBytes = in.readBytesRef();
    subsetDf = in.readVLong();
    supersetDf = in.readVLong();
    score = in.readDouble();
    aggregations = InternalAggregations.readAggregations(in);
}
 
Example #7
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 #8
Source File: StringTerms.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    termBytes = in.readBytesRef();
    docCount = in.readVLong();
    docCountError = -1;
    if (showDocCountError) {
        docCountError = in.readLong();
    }
    aggregations = InternalAggregations.readAggregations(in);
}
 
Example #9
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
PercolateResponse(int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures,
                         Match[] matches, long count, long tookInMillis, InternalAggregations aggregations) {
    super(totalShards, successfulShards, failedShards, shardFailures);
    if (tookInMillis < 0) {
        throw new IllegalArgumentException("tookInMillis must be positive but was: " + tookInMillis);
    }
    this.tookInMillis = tookInMillis;
    this.matches = matches;
    this.count = count;
    this.aggregations = aggregations;
}
 
Example #10
Source File: InternalDateHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
/**
 * Read from a stream.
 */
public InternalBucket(StreamInput in) throws IOException {
    key = in.readBytesRef();
    name = in.readString();
    docCount = in.readLong();
    aggregations = new InternalAggregations(in);
    level = in.readInt();
    int pathsSize = in.readInt();
    paths = new String[pathsSize];
    for (int i=0; i < pathsSize; i++) {
        paths[i] = in.readString();
    }
}
 
Example #11
Source File: InternalPathHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
public InternalBucket(long docCount, InternalAggregations aggregations, String basename,
                      BytesRef term, int level, int minDepth, String[] paths) {
    termBytes = term;
    this.docCount = docCount;
    this.aggregations = aggregations;
    this.level = level;
    this.minDepth = minDepth;
    this.basename = basename;
    this.paths = paths;
}
 
Example #12
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 #13
Source File: BucketsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Required method to build the child aggregations of the given bucket (identified by the bucket ordinal).
 */
protected final InternalAggregations bucketAggregations(long bucket) throws IOException {
    final InternalAggregation[] aggregations = new InternalAggregation[subAggregators.length];
    for (int i = 0; i < subAggregators.length; i++) {
        aggregations[i] = subAggregators[i].buildAggregation(bucket);
    }
    return new InternalAggregations(Arrays.asList(aggregations));
}
 
Example #14
Source File: InternalPathHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
/**
 * Read from a stream.
 */
public InternalBucket(StreamInput in) throws IOException {
    termBytes = in.readBytesRef();
    docCount = in.readLong();
    aggregations = new InternalAggregations(in);
    level = in.readInt();
    minDepth = in.readInt();
    basename = in.readString();
    int pathsSize = in.readInt();
    paths = new String[pathsSize];
    for (int i=0; i < pathsSize; i++) {
        paths[i] = in.readString();
    }
}
 
Example #15
Source File: InternalSearchResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public InternalSearchResponse(InternalSearchHits hits, InternalAggregations aggregations, Suggest suggest,
                              InternalProfileShardResults profileResults, boolean timedOut, Boolean terminatedEarly) {
    this.hits = hits;
    this.aggregations = aggregations;
    this.suggest = suggest;
    this.profileResults = profileResults;
    this.timedOut = timedOut;
    this.terminatedEarly = terminatedEarly;
}
 
Example #16
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);
    }
}
 
Example #17
Source File: InternalGeoPointClustering.java    From elasticsearch-aggregation-geoclustering with Apache License 2.0 5 votes vote down vote up
Bucket(long geohashAsLong, GeoPoint centroid, long docCount, InternalAggregations aggregations) {
    this.docCount = docCount;
    this.centroid = centroid;
    this.aggregations = aggregations;
    this.geohashAsLong = geohashAsLong;
    this.geohashesList = new HashSet<>();
    this.geohashesList.add(geohashAsLong);
}
 
Example #18
Source File: PercolateShardResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    percolatorTypeId = in.readByte();
    requestedSize = in.readVInt();
    count = in.readVLong();
    matches = new BytesRef[in.readVInt()];
    for (int i = 0; i < matches.length; i++) {
        matches[i] = in.readBytesRef();
    }
    scores = new float[in.readVInt()];
    for (int i = 0; i < scores.length; i++) {
        scores[i] = in.readFloat();
    }
    int size = in.readVInt();
    for (int i = 0; i < size; i++) {
        int mSize = in.readVInt();
        Map<String, HighlightField> fields = new HashMap<>();
        for (int j = 0; j < mSize; j++) {
            fields.put(in.readString(), HighlightField.readHighlightField(in));
        }
        hls.add(fields);
    }
    aggregations = InternalAggregations.readOptionalAggregations(in);
    if (in.readBoolean()) {
        int pipelineAggregatorsSize = in.readVInt();
        List<SiblingPipelineAggregator> pipelineAggregators = new ArrayList<>(pipelineAggregatorsSize);
        for (int i = 0; i < pipelineAggregatorsSize; i++) {
            BytesReference type = in.readBytesReference();
            PipelineAggregator pipelineAggregator = PipelineAggregatorStreams.stream(type).readResult(in);
            pipelineAggregators.add((SiblingPipelineAggregator) pipelineAggregator);
        }
        this.pipelineAggregators = pipelineAggregators;
    }
}
 
Example #19
Source File: DoubleTerms.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    term = in.readDouble();
    docCount = in.readVLong();
    docCountError = -1;
    if (showDocCountError) {
        docCountError = in.readLong();
    }
    aggregations = InternalAggregations.readAggregations(in);
}
 
Example #20
Source File: BucketsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to build empty aggregations of the sub aggregators.
 */
protected final InternalAggregations bucketEmptyAggregations() {
    final InternalAggregation[] aggregations = new InternalAggregation[subAggregators.length];
    for (int i = 0; i < subAggregators.length; i++) {
        aggregations[i] = subAggregators[i].buildEmptyAggregation();
    }
    return new InternalAggregations(Arrays.asList(aggregations));
}
 
Example #21
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    tookInMillis = in.readVLong();
    count = in.readVLong();
    int size = in.readVInt();
    if (size != -1) {
        matches = new Match[size];
        for (int i = 0; i < size; i++) {
            matches[i] = new Match();
            matches[i].readFrom(in);
        }
    }
    aggregations = InternalAggregations.readOptionalAggregations(in);
}
 
Example #22
Source File: InternalRange.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doReadFrom(StreamInput in) throws IOException {
    formatter = ValueFormatterStreams.readOptional(in);
    keyed = in.readBoolean();
    int size = in.readVInt();
    List<B> ranges = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        String key = in.readOptionalString();
        ranges.add(getFactory().createBucket(key, in.readDouble(), in.readDouble(), in.readVLong(), InternalAggregations.readAggregations(in), keyed, formatter));
    }
    this.ranges = ranges;
    this.rangeMap = null;
}
 
Example #23
Source File: InternalDateHierarchy.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
public InternalBucket(long docCount, InternalAggregations aggregations, BytesRef key, String name, int level, String[] paths) {
    this.key = key;
    this.name = name;
    this.docCount = docCount;
    this.aggregations = aggregations;
    this.level = level;
    this.paths = paths;
}
 
Example #24
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public B createBucket(Object key, long docCount, InternalAggregations aggregations, boolean keyed, ValueFormatter formatter) {
    if (key instanceof Number) {
        return (B) new Bucket(((Number) key).longValue(), docCount, keyed, formatter, this, aggregations);
    } else {
        throw new AggregationExecutionException("Expected key of type Number but got [" + key + "]");
    }
}
 
Example #25
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 #26
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private InternalSearchResponse parseInternalSearchResponse(Map<String,?> map) {
    InternalSearchHits internalSearchHits = parseInternalSearchHits(map);
    InternalAggregations internalAggregations = parseInternalAggregations(map);
    Suggest suggest = parseSuggest(map);
    InternalProfileShardResults internalProfileShardResults = null;
    Boolean timeout = false;
    Boolean terminatedEarly = false;
    return new InternalSearchResponse(internalSearchHits,
            internalAggregations,
            suggest,
            internalProfileShardResults,
            timeout,
            terminatedEarly);
}
 
Example #27
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static EmptyBucketInfo readFrom(StreamInput in) throws IOException {
    Rounding rounding = Rounding.Streams.read(in);
    InternalAggregations aggs = InternalAggregations.readAggregations(in);
    if (in.readBoolean()) {
        return new EmptyBucketInfo(rounding, aggs, ExtendedBounds.readFrom(in));
    }
    return new EmptyBucketInfo(rounding, aggs);
}
 
Example #28
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 #29
Source File: InternalTerms.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected Bucket(long docCount, InternalAggregations aggregations, boolean showDocCountError, long docCountError,
        ValueFormatter formatter) {
    this(formatter, showDocCountError);
    this.docCount = docCount;
    this.aggregations = aggregations;
    this.docCountError = docCountError;
}
 
Example #30
Source File: InternalRange.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Bucket(String key, double from, double to, long docCount, InternalAggregations aggregations, boolean keyed,
        ValueFormatter formatter) {
    this(keyed, formatter);
    this.key = key != null ? key : generateKey(from, to, formatter);
    this.from = from;
    this.to = to;
    this.docCount = docCount;
    this.aggregations = aggregations;
}