Search in sources :

Example 11 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class SiblingPipelineAggregator method reduce.

@SuppressWarnings("unchecked")
@Override
public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext reduceContext) {
    if (aggregation instanceof InternalMultiBucketAggregation) {
        @SuppressWarnings("rawtypes") InternalMultiBucketAggregation multiBucketsAgg = (InternalMultiBucketAggregation) aggregation;
        List<? extends Bucket> buckets = multiBucketsAgg.getBuckets();
        List<Bucket> newBuckets = new ArrayList<>();
        for (int i = 0; i < buckets.size(); i++) {
            InternalMultiBucketAggregation.InternalBucket bucket = (InternalMultiBucketAggregation.InternalBucket) buckets.get(i);
            InternalAggregation aggToAdd = doReduce(bucket.getAggregations(), reduceContext);
            List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map((p) -> {
                return (InternalAggregation) p;
            }).collect(Collectors.toList());
            aggs.add(aggToAdd);
            InternalMultiBucketAggregation.InternalBucket newBucket = multiBucketsAgg.createBucket(new InternalAggregations(aggs), bucket);
            newBuckets.add(newBucket);
        }
        return multiBucketsAgg.create(newBuckets);
    } else if (aggregation instanceof InternalSingleBucketAggregation) {
        InternalSingleBucketAggregation singleBucketAgg = (InternalSingleBucketAggregation) aggregation;
        InternalAggregation aggToAdd = doReduce(singleBucketAgg.getAggregations(), reduceContext);
        List<InternalAggregation> aggs = StreamSupport.stream(singleBucketAgg.getAggregations().spliterator(), false).map((p) -> {
            return (InternalAggregation) p;
        }).collect(Collectors.toList());
        aggs.add(aggToAdd);
        return singleBucketAgg.create(new InternalAggregations(aggs));
    } else {
        throw new IllegalStateException("Aggregation [" + aggregation.getName() + "] must be a bucket aggregation [" + aggregation.getWriteableName() + "]");
    }
}
Also used : InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) Aggregations(org.elasticsearch.search.aggregations.Aggregations) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation) List(java.util.List) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) ArrayList(java.util.ArrayList) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) InternalSingleBucketAggregation(org.elasticsearch.search.aggregations.bucket.InternalSingleBucketAggregation) ArrayList(java.util.ArrayList) List(java.util.List) InternalMultiBucketAggregation(org.elasticsearch.search.aggregations.InternalMultiBucketAggregation)

Example 12 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class SearchPhaseControllerTests method testConsumer.

public void testConsumer() {
    int bufferSize = randomIntBetween(2, 3);
    SearchRequest request = new SearchRequest();
    request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
    request.setBatchedReduceSize(bufferSize);
    InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer = searchPhaseController.newSearchPhaseResults(request, 3);
    QuerySearchResult result = new QuerySearchResult(0, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    InternalAggregations aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 1.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(0, result);
    result = new QuerySearchResult(1, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 3.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(2, result);
    result = new QuerySearchResult(1, new SearchShardTarget("node", new Index("a", "b"), 0));
    result.topDocs(new TopDocs(0, new ScoreDoc[0], 0.0F), new DocValueFormat[0]);
    aggs = new InternalAggregations(Arrays.asList(new InternalMax("test", 2.0D, DocValueFormat.RAW, Collections.emptyList(), Collections.emptyMap())));
    result.aggregations(aggs);
    consumer.consumeResult(1, result);
    int numTotalReducePhases = 1;
    if (bufferSize == 2) {
        assertThat(consumer, instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class));
        assertEquals(1, ((SearchPhaseController.QueryPhaseResultConsumer) consumer).getNumReducePhases());
        assertEquals(2, ((SearchPhaseController.QueryPhaseResultConsumer) consumer).getNumBuffered());
        numTotalReducePhases++;
    } else {
        assertThat(consumer, not(instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class)));
    }
    SearchPhaseController.ReducedQueryPhase reduce = consumer.reduce();
    assertEquals(numTotalReducePhases, reduce.numReducePhases);
    InternalMax max = (InternalMax) reduce.aggregations.asList().get(0);
    assertEquals(3.0D, max.getValue(), 0.0D);
}
Also used : QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) InternalMax(org.elasticsearch.search.aggregations.metrics.max.InternalMax) Index(org.elasticsearch.index.Index) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SearchShardTarget(org.elasticsearch.search.SearchShardTarget)

Example 13 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class InternalHistogram method addEmptyBuckets.

private void addEmptyBuckets(List<Bucket> list, ReduceContext reduceContext) {
    ListIterator<Bucket> iter = list.listIterator();
    // first adding all the empty buckets *before* the actual data (based on th extended_bounds.min the user requested)
    InternalAggregations reducedEmptySubAggs = InternalAggregations.reduce(Collections.singletonList(emptyBucketInfo.subAggregations), reduceContext);
    if (iter.hasNext() == false) {
        // fill with empty buckets
        for (double key = round(emptyBucketInfo.minBound); key <= emptyBucketInfo.maxBound; key = nextKey(key)) {
            iter.add(new Bucket(key, 0, keyed, format, reducedEmptySubAggs));
        }
    } else {
        Bucket first = list.get(iter.nextIndex());
        if (Double.isFinite(emptyBucketInfo.minBound)) {
            // fill with empty buckets until the first key
            for (double key = round(emptyBucketInfo.minBound); key < first.key; key = nextKey(key)) {
                iter.add(new Bucket(key, 0, keyed, format, reducedEmptySubAggs));
            }
        }
        // now adding the empty buckets within the actual data,
        // e.g. if the data series is [1,2,3,7] there're 3 empty buckets that will be created for 4,5,6
        Bucket lastBucket = null;
        do {
            Bucket nextBucket = list.get(iter.nextIndex());
            if (lastBucket != null) {
                double key = nextKey(lastBucket.key);
                while (key < nextBucket.key) {
                    iter.add(new Bucket(key, 0, keyed, format, reducedEmptySubAggs));
                    key = nextKey(key);
                }
                assert key == nextBucket.key;
            }
            lastBucket = iter.next();
        } while (iter.hasNext());
        // finally, adding the empty buckets *after* the actual data (based on the extended_bounds.max requested by the user)
        for (double key = nextKey(lastBucket.key); key <= emptyBucketInfo.maxBound; key = nextKey(key)) {
            iter.add(new Bucket(key, 0, keyed, format, reducedEmptySubAggs));
        }
    }
}
Also used : InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations)

Example 14 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class RangeAggregator method buildEmptyAggregation.

@Override
public InternalAggregation buildEmptyAggregation() {
    InternalAggregations subAggs = buildEmptySubAggregations();
    List<org.elasticsearch.search.aggregations.bucket.range.Range.Bucket> buckets = new ArrayList<>(ranges.length);
    for (int i = 0; i < ranges.length; i++) {
        Range range = ranges[i];
        org.elasticsearch.search.aggregations.bucket.range.Range.Bucket bucket = rangeFactory.createBucket(range.key, range.from, range.to, 0, subAggs, keyed, format);
        buckets.add(bucket);
    }
    // value source can be null in the case of unmapped fields
    return rangeFactory.create(name, buckets, format, keyed, pipelineAggregators(), metaData());
}
Also used : ArrayList(java.util.ArrayList) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations)

Example 15 with InternalAggregations

use of org.elasticsearch.search.aggregations.InternalAggregations in project elasticsearch by elastic.

the class FiltersAggregator method buildEmptyAggregation.

@Override
public InternalAggregation buildEmptyAggregation() {
    InternalAggregations subAggs = buildEmptySubAggregations();
    List<InternalFilters.InternalBucket> buckets = new ArrayList<>(filters.length);
    for (int i = 0; i < keys.length; i++) {
        InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(keys[i], 0, subAggs, keyed);
        buckets.add(bucket);
    }
    if (showOtherBucket) {
        InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(otherBucketKey, 0, subAggs, keyed);
        buckets.add(bucket);
    }
    return new InternalFilters(name, buckets, keyed, pipelineAggregators(), metaData());
}
Also used : InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) ArrayList(java.util.ArrayList)

Aggregations

InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)16 ArrayList (java.util.ArrayList)12 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)9 List (java.util.List)8 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)8 IOException (java.io.IOException)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 StreamSupport (java.util.stream.StreamSupport)7 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)6 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)4 DocValueFormat (org.elasticsearch.search.DocValueFormat)4 GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)4 BucketHelpers.resolveBucketValue (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue)4 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)4 QuerySearchResult (org.elasticsearch.search.query.QuerySearchResult)4 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 MultiBucketsAggregation (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation)3