Search in sources :

Example 11 with InternalAggregation

use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.

the class InternalSingleBucketAggregation method reduce.

@Override
public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    long docCount = 0L;
    List<InternalAggregations> subAggregationsList = new ArrayList<>(aggregations.size());
    for (InternalAggregation aggregation : aggregations) {
        assert aggregation.getName().equals(getName());
        docCount += ((InternalSingleBucketAggregation) aggregation).docCount;
        subAggregationsList.add(((InternalSingleBucketAggregation) aggregation).aggregations);
    }
    final InternalAggregations aggs = InternalAggregations.reduce(subAggregationsList, reduceContext);
    return newAggregation(getName(), docCount, aggs);
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ArrayList(java.util.ArrayList)

Example 12 with InternalAggregation

use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.

the class InternalSingleBucketAggregation method getProperty.

@Override
public Object getProperty(List<String> path) {
    if (path.isEmpty()) {
        return this;
    } else {
        String aggName = path.get(0);
        if (aggName.equals("_count")) {
            if (path.size() > 1) {
                throw new IllegalArgumentException("_count must be the last element in the path");
            }
            return getDocCount();
        }
        InternalAggregation aggregation = aggregations.get(aggName);
        if (aggregation == null) {
            throw new IllegalArgumentException("Cannot find an aggregation named [" + aggName + "] in [" + getName() + "]");
        }
        return aggregation.getProperty(path.subList(1, path.size()));
    }
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation)

Example 13 with InternalAggregation

use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.

the class InternalSingleBucketAggregation method reducePipelines.

/**
 * Amulti-bucket agg needs to first reduce the buckets and *their* pipelines
 * before allowing sibling pipelines to materialize.
 */
@Override
public final InternalAggregation reducePipelines(InternalAggregation reducedAggs, ReduceContext reduceContext, PipelineTree pipelineTree) {
    assert reduceContext.isFinalReduce();
    InternalAggregation reduced = this;
    if (pipelineTree.hasSubTrees()) {
        List<InternalAggregation> aggs = new ArrayList<>();
        for (Aggregation agg : getAggregations().asList()) {
            PipelineTree subTree = pipelineTree.subTree(agg.getName());
            aggs.add(((InternalAggregation) agg).reducePipelines((InternalAggregation) agg, reduceContext, subTree));
        }
        InternalAggregations reducedSubAggs = InternalAggregations.from(aggs);
        reduced = create(reducedSubAggs);
    }
    return super.reducePipelines(reduced, reduceContext, pipelineTree);
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ArrayList(java.util.ArrayList) PipelineTree(org.opensearch.search.aggregations.pipeline.PipelineAggregator.PipelineTree)

Example 14 with InternalAggregation

use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.

the class InternalComposite method reduce.

@Override
public InternalAggregation reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    PriorityQueue<BucketIterator> pq = new PriorityQueue<>(aggregations.size());
    boolean earlyTerminated = false;
    for (InternalAggregation agg : aggregations) {
        InternalComposite sortedAgg = (InternalComposite) agg;
        earlyTerminated |= sortedAgg.earlyTerminated;
        BucketIterator it = new BucketIterator(sortedAgg.buckets);
        if (it.next() != null) {
            pq.add(it);
        }
    }
    InternalBucket lastBucket = null;
    List<InternalBucket> buckets = new ArrayList<>();
    List<InternalBucket> result = new ArrayList<>();
    while (pq.size() > 0) {
        BucketIterator bucketIt = pq.poll();
        if (lastBucket != null && bucketIt.current.compareKey(lastBucket) != 0) {
            InternalBucket reduceBucket = reduceBucket(buckets, reduceContext);
            buckets.clear();
            result.add(reduceBucket);
            if (result.size() >= size) {
                break;
            }
        }
        lastBucket = bucketIt.current;
        buckets.add(bucketIt.current);
        if (bucketIt.next() != null) {
            pq.add(bucketIt);
        }
    }
    if (buckets.size() > 0) {
        InternalBucket reduceBucket = reduceBucket(buckets, reduceContext);
        result.add(reduceBucket);
    }
    List<DocValueFormat> reducedFormats = formats;
    CompositeKey lastKey = null;
    if (result.size() > 0) {
        lastBucket = result.get(result.size() - 1);
        /* Attach the formats from the last bucket to the reduced composite
             * so that we can properly format the after key. */
        reducedFormats = lastBucket.formats;
        lastKey = lastBucket.getRawKey();
    }
    reduceContext.consumeBucketsAndMaybeBreak(result.size());
    return new InternalComposite(name, size, sourceNames, reducedFormats, result, lastKey, reverseMuls, missingOrders, earlyTerminated, metadata);
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) DocValueFormat(org.opensearch.search.DocValueFormat) ArrayList(java.util.ArrayList) PriorityQueue(java.util.PriorityQueue)

Example 15 with InternalAggregation

use of org.opensearch.search.aggregations.InternalAggregation in project OpenSearch by opensearch-project.

the class InternalGeoGrid method reduce.

@Override
public InternalGeoGrid reduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    LongObjectPagedHashMap<List<InternalGeoGridBucket>> buckets = null;
    for (InternalAggregation aggregation : aggregations) {
        InternalGeoGrid grid = (InternalGeoGrid) aggregation;
        if (buckets == null) {
            buckets = new LongObjectPagedHashMap<>(grid.buckets.size(), reduceContext.bigArrays());
        }
        for (Object obj : grid.buckets) {
            InternalGeoGridBucket bucket = (InternalGeoGridBucket) obj;
            List<InternalGeoGridBucket> existingBuckets = buckets.get(bucket.hashAsLong());
            if (existingBuckets == null) {
                existingBuckets = new ArrayList<>(aggregations.size());
                buckets.put(bucket.hashAsLong(), existingBuckets);
            }
            existingBuckets.add(bucket);
        }
    }
    final int size = Math.toIntExact(reduceContext.isFinalReduce() == false ? buckets.size() : Math.min(requiredSize, buckets.size()));
    BucketPriorityQueue<InternalGeoGridBucket> ordered = new BucketPriorityQueue<>(size);
    for (LongObjectPagedHashMap.Cursor<List<InternalGeoGridBucket>> cursor : buckets) {
        List<InternalGeoGridBucket> sameCellBuckets = cursor.value;
        ordered.insertWithOverflow(reduceBucket(sameCellBuckets, reduceContext));
    }
    buckets.close();
    InternalGeoGridBucket[] list = new InternalGeoGridBucket[ordered.size()];
    for (int i = ordered.size() - 1; i >= 0; i--) {
        list[i] = ordered.pop();
    }
    reduceContext.consumeBucketsAndMaybeBreak(list.length);
    return create(getName(), requiredSize, Arrays.asList(list), getMetadata());
}
Also used : LongObjectPagedHashMap(org.opensearch.common.util.LongObjectPagedHashMap) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)75 ArrayList (java.util.ArrayList)40 List (java.util.List)17 SearchResponse (org.opensearch.action.search.SearchResponse)11 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)11 Global (org.opensearch.search.aggregations.bucket.global.Global)11 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)11 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 MockBigArrays (org.opensearch.common.util.MockBigArrays)9 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)9 IOException (java.io.IOException)8 ScriptService (org.opensearch.script.ScriptService)8 DocValueFormat (org.opensearch.search.DocValueFormat)8 ReduceContext (org.opensearch.search.aggregations.InternalAggregation.ReduceContext)8 Collectors (java.util.stream.Collectors)7 MultiBucketConsumerService (org.opensearch.search.aggregations.MultiBucketConsumerService)7 IndexReader (org.apache.lucene.index.IndexReader)6 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)6