Search in sources :

Example 1 with SiblingPipelineAggregator

use of org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator in project elasticsearch by elastic.

the class SearchPhaseController method reduceAggs.

private InternalAggregations reduceAggs(List<InternalAggregations> aggregationsList, List<SiblingPipelineAggregator> pipelineAggregators, ReduceContext reduceContext) {
    InternalAggregations aggregations = InternalAggregations.reduce(aggregationsList, reduceContext);
    if (pipelineAggregators != null) {
        List<InternalAggregation> newAggs = StreamSupport.stream(aggregations.spliterator(), false).map((p) -> (InternalAggregation) p).collect(Collectors.toList());
        for (SiblingPipelineAggregator pipelineAggregator : pipelineAggregators) {
            InternalAggregation newAgg = pipelineAggregator.doReduce(new InternalAggregations(newAggs), reduceContext);
            newAggs.add(newAgg);
        }
        return new InternalAggregations(newAggs);
    }
    return aggregations;
}
Also used : Arrays(java.util.Arrays) HppcMaps(org.elasticsearch.common.collect.HppcMaps) ScoreDoc(org.apache.lucene.search.ScoreDoc) Suggest(org.elasticsearch.search.suggest.Suggest) SearchHits(org.elasticsearch.search.SearchHits) BigArrays(org.elasticsearch.common.util.BigArrays) Term(org.apache.lucene.index.Term) ObjectObjectHashMap(com.carrotsearch.hppc.ObjectObjectHashMap) HashMap(java.util.HashMap) FieldDoc(org.apache.lucene.search.FieldDoc) Lucene(org.elasticsearch.common.lucene.Lucene) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) ArrayList(java.util.ArrayList) Settings(org.elasticsearch.common.settings.Settings) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) IntArrayList(com.carrotsearch.hppc.IntArrayList) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) Map(java.util.Map) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) AggregatedDfs(org.elasticsearch.search.dfs.AggregatedDfs) StreamSupport(java.util.stream.StreamSupport) SortField(org.apache.lucene.search.SortField) Entry(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry) TermStatistics(org.apache.lucene.search.TermStatistics) SearchHit(org.elasticsearch.search.SearchHit) CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) TopDocs(org.apache.lucene.search.TopDocs) InternalSearchResponse(org.elasticsearch.search.internal.InternalSearchResponse) Suggestion(org.elasticsearch.search.suggest.Suggest.Suggestion) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Sort(org.apache.lucene.search.Sort) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) ProfileShardResult(org.elasticsearch.search.profile.ProfileShardResult) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DfsSearchResult(org.elasticsearch.search.dfs.DfsSearchResult) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) ReduceContext(org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext) CollectionStatistics(org.apache.lucene.search.CollectionStatistics) List(java.util.List) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) SiblingPipelineAggregator(org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SearchProfileShardResults(org.elasticsearch.search.profile.SearchProfileShardResults) ScriptService(org.elasticsearch.script.ScriptService) Collections(java.util.Collections) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) InternalAggregations(org.elasticsearch.search.aggregations.InternalAggregations) SiblingPipelineAggregator(org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator)

Example 2 with SiblingPipelineAggregator

use of org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator in project elasticsearch by elastic.

the class AggregationPhase method execute.

@Override
public void execute(SearchContext context) {
    if (context.aggregations() == null) {
        context.queryResult().aggregations(null);
        return;
    }
    if (context.queryResult().hasAggs()) {
        // no need to compute the aggs twice, they should be computed on a per context basis
        return;
    }
    Aggregator[] aggregators = context.aggregations().aggregators();
    List<Aggregator> globals = new ArrayList<>();
    for (int i = 0; i < aggregators.length; i++) {
        if (aggregators[i] instanceof GlobalAggregator) {
            globals.add(aggregators[i]);
        }
    }
    // optimize the global collector based execution
    if (!globals.isEmpty()) {
        BucketCollector globalsCollector = BucketCollector.wrap(globals);
        Query query = context.buildFilteredQuery(Queries.newMatchAllQuery());
        try {
            final Collector collector;
            if (context.getProfilers() == null) {
                collector = globalsCollector;
            } else {
                InternalProfileCollector profileCollector = new InternalProfileCollector(globalsCollector, CollectorResult.REASON_AGGREGATION_GLOBAL, // TODO: report on sub collectors
                Collections.emptyList());
                collector = profileCollector;
                // start a new profile with this collector
                context.getProfilers().addQueryProfiler().setCollector(profileCollector);
            }
            globalsCollector.preCollection();
            context.searcher().search(query, collector);
        } catch (Exception e) {
            throw new QueryPhaseExecutionException(context, "Failed to execute global aggregators", e);
        } finally {
            context.clearReleasables(SearchContext.Lifetime.COLLECTION);
        }
    }
    List<InternalAggregation> aggregations = new ArrayList<>(aggregators.length);
    for (Aggregator aggregator : context.aggregations().aggregators()) {
        try {
            aggregator.postCollection();
            aggregations.add(aggregator.buildAggregation(0));
        } catch (IOException e) {
            throw new AggregationExecutionException("Failed to build aggregation [" + aggregator.name() + "]", e);
        }
    }
    context.queryResult().aggregations(new InternalAggregations(aggregations));
    try {
        List<PipelineAggregator> pipelineAggregators = context.aggregations().factories().createPipelineAggregators();
        List<SiblingPipelineAggregator> siblingPipelineAggregators = new ArrayList<>(pipelineAggregators.size());
        for (PipelineAggregator pipelineAggregator : pipelineAggregators) {
            if (pipelineAggregator instanceof SiblingPipelineAggregator) {
                siblingPipelineAggregators.add((SiblingPipelineAggregator) pipelineAggregator);
            } else {
                throw new AggregationExecutionException("Invalid pipeline aggregation named [" + pipelineAggregator.name() + "] of type [" + pipelineAggregator.getWriteableName() + "]. Only sibling pipeline aggregations are " + "allowed at the top level");
            }
        }
        context.queryResult().pipelineAggregators(siblingPipelineAggregators);
    } catch (IOException e) {
        throw new AggregationExecutionException("Failed to build top level pipeline aggregators", e);
    }
    // disable aggregations so that they don't run on next pages in case of scrolling
    context.aggregations(null);
    context.queryCollectors().remove(AggregationPhase.class);
}
Also used : Query(org.apache.lucene.search.Query) QueryPhaseExecutionException(org.elasticsearch.search.query.QueryPhaseExecutionException) SiblingPipelineAggregator(org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator) ArrayList(java.util.ArrayList) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) SiblingPipelineAggregator(org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator) PipelineAggregator(org.elasticsearch.search.aggregations.pipeline.PipelineAggregator) GlobalAggregator(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator) SiblingPipelineAggregator(org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator) IOException(java.io.IOException) IOException(java.io.IOException) QueryPhaseExecutionException(org.elasticsearch.search.query.QueryPhaseExecutionException) GlobalAggregator(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator) Collector(org.apache.lucene.search.Collector) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SiblingPipelineAggregator (org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 ObjectObjectHashMap (com.carrotsearch.hppc.ObjectObjectHashMap)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Term (org.apache.lucene.index.Term)1 CollectionStatistics (org.apache.lucene.search.CollectionStatistics)1 Collector (org.apache.lucene.search.Collector)1 FieldDoc (org.apache.lucene.search.FieldDoc)1 Query (org.apache.lucene.search.Query)1 ScoreDoc (org.apache.lucene.search.ScoreDoc)1 Sort (org.apache.lucene.search.Sort)1 SortField (org.apache.lucene.search.SortField)1