Search in sources :

Example 1 with GlobalAggregator

use of org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator in project elasticsearch by elastic.

the class GlobalAggregatorTests method testCase.

// Note that `global`'s fancy support for ignoring the query comes from special code in AggregationPhase. We don't test that here.
private void testCase(CheckedConsumer<RandomIndexWriter, IOException> buildIndex, BiConsumer<InternalGlobal, InternalMin> verify) throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    buildIndex.accept(indexWriter);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    GlobalAggregationBuilder aggregationBuilder = new GlobalAggregationBuilder("_name");
    aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number"));
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number");
    try (GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        try {
            aggregator.preCollection();
            indexSearcher.search(new MatchAllDocsQuery(), aggregator);
            aggregator.postCollection();
            InternalGlobal result = (InternalGlobal) aggregator.buildAggregation(0L);
            verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global"));
        } finally {
            IOUtils.close(aggregator.subAggregators());
        }
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalGlobal(org.elasticsearch.search.aggregations.bucket.global.InternalGlobal) IndexReader(org.apache.lucene.index.IndexReader) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) GlobalAggregationBuilder(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder) GlobalAggregator(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator)

Example 2 with GlobalAggregator

use of org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator in project elasticsearch by elastic.

the class AggregationPhase method preProcess.

@Override
public void preProcess(SearchContext context) {
    if (context.aggregations() != null) {
        List<Aggregator> collectors = new ArrayList<>();
        Aggregator[] aggregators;
        try {
            AggregatorFactories factories = context.aggregations().factories();
            aggregators = factories.createTopLevelAggregators();
            for (int i = 0; i < aggregators.length; i++) {
                if (aggregators[i] instanceof GlobalAggregator == false) {
                    collectors.add(aggregators[i]);
                }
            }
            context.aggregations().aggregators(aggregators);
            if (!collectors.isEmpty()) {
                Collector collector = BucketCollector.wrap(collectors);
                ((BucketCollector) collector).preCollection();
                if (context.getProfilers() != null) {
                    collector = new InternalProfileCollector(collector, CollectorResult.REASON_AGGREGATION, // TODO: report on child aggs as well
                    Collections.emptyList());
                }
                context.queryCollectors().put(AggregationPhase.class, collector);
            }
        } catch (IOException e) {
            throw new AggregationInitializationException("Could not initialize aggregators", e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Collector(org.apache.lucene.search.Collector) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector) 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) InternalProfileCollector(org.elasticsearch.search.profile.query.InternalProfileCollector) GlobalAggregator(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator)

Example 3 with GlobalAggregator

use of org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator 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

GlobalAggregator (org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collector (org.apache.lucene.search.Collector)2 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)2 SiblingPipelineAggregator (org.elasticsearch.search.aggregations.pipeline.SiblingPipelineAggregator)2 InternalProfileCollector (org.elasticsearch.search.profile.query.InternalProfileCollector)2 IndexReader (org.apache.lucene.index.IndexReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 Query (org.apache.lucene.search.Query)1 Directory (org.apache.lucene.store.Directory)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1 GlobalAggregationBuilder (org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder)1 InternalGlobal (org.elasticsearch.search.aggregations.bucket.global.InternalGlobal)1 MinAggregationBuilder (org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder)1 QueryPhaseExecutionException (org.elasticsearch.search.query.QueryPhaseExecutionException)1