Search in sources :

Example 1 with GlobalAggregator

use of org.opensearch.search.aggregations.bucket.global.GlobalAggregator in project OpenSearch by opensearch-project.

the class AggregationPhase method execute.

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 = MultiBucketCollector.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.shardTarget(), "Failed to execute global aggregators", e);
        }
    }
    List<InternalAggregation> aggregations = new ArrayList<>(aggregators.length);
    context.aggregations().resetBucketMultiConsumer();
    for (Aggregator aggregator : context.aggregations().aggregators()) {
        try {
            aggregator.postCollection();
            aggregations.add(aggregator.buildTopLevel());
        } catch (IOException e) {
            throw new AggregationExecutionException("Failed to build aggregation [" + aggregator.name() + "]", e);
        }
    }
    context.queryResult().aggregations(new InternalAggregations(aggregations, context.request().source().aggregations()::buildPipelineTree));
    // 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.opensearch.search.query.QueryPhaseExecutionException) ArrayList(java.util.ArrayList) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) IOException(java.io.IOException) QueryPhaseExecutionException(org.opensearch.search.query.QueryPhaseExecutionException) IOException(java.io.IOException) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) Collector(org.apache.lucene.search.Collector) InternalProfileCollector(org.opensearch.search.profile.query.InternalProfileCollector) InternalProfileCollector(org.opensearch.search.profile.query.InternalProfileCollector)

Example 2 with GlobalAggregator

use of org.opensearch.search.aggregations.bucket.global.GlobalAggregator in project OpenSearch by opensearch-project.

the class AggregationPhase method preProcess.

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(context);
            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 = MultiBucketCollector.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.opensearch.search.profile.query.InternalProfileCollector) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) IOException(java.io.IOException) InternalProfileCollector(org.opensearch.search.profile.query.InternalProfileCollector) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator)

Example 3 with GlobalAggregator

use of org.opensearch.search.aggregations.bucket.global.GlobalAggregator in project OpenSearch by opensearch-project.

the class MaxAggregatorTests method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER);
    AggregationBuilder aggregationBuilder = AggregationBuilders.global("global").subAggregation(AggregationBuilders.max("max").field("value"));
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    // Do not add any documents
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
    aggregator.preCollection();
    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
    aggregator.postCollection();
    Global global = (Global) aggregator.buildTopLevel();
    assertNotNull(global);
    assertEquals("global", global.getName());
    assertEquals(0L, global.getDocCount());
    assertNotNull(global.getAggregations());
    assertEquals(1, global.getAggregations().asMap().size());
    Max max = global.getAggregations().get("max");
    assertNotNull(max);
    assertEquals("max", max.getName());
    assertEquals(Double.NEGATIVE_INFINITY, max.getValue(), 0);
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Global(org.opensearch.search.aggregations.bucket.global.Global) Directory(org.apache.lucene.store.Directory) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator)

Example 4 with GlobalAggregator

use of org.opensearch.search.aggregations.bucket.global.GlobalAggregator in project OpenSearch by opensearch-project.

the class MaxAggregatorTests method testSingleValuedFieldGetProperty.

public void testSingleValuedFieldGetProperty() throws IOException {
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("value", NumberFieldMapper.NumberType.INTEGER);
    AggregationBuilder aggregationBuilder = AggregationBuilders.global("global").subAggregation(AggregationBuilders.max("max").field("value"));
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    final int numDocs = 10;
    for (int i = 0; i < numDocs; i++) {
        indexWriter.addDocument(singleton(new NumericDocValuesField("value", i + 1)));
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
    aggregator.preCollection();
    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
    aggregator.postCollection();
    Global global = (Global) aggregator.buildTopLevel();
    assertNotNull(global);
    assertEquals("global", global.getName());
    assertEquals(10L, global.getDocCount());
    assertNotNull(global.getAggregations());
    assertEquals(1, global.getAggregations().asMap().size());
    Max max = global.getAggregations().get("max");
    assertNotNull(max);
    assertEquals("max", max.getName());
    assertEquals(10.0, max.getValue(), 0);
    assertEquals(max, ((InternalAggregation) global).getProperty("max"));
    assertEquals(10.0, (double) ((InternalAggregation) global).getProperty("max.value"), 0);
    assertEquals(10.0, (double) ((InternalAggregation) max).getProperty("value"), 0);
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) Global(org.opensearch.search.aggregations.bucket.global.Global) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 5 with GlobalAggregator

use of org.opensearch.search.aggregations.bucket.global.GlobalAggregator in project OpenSearch by opensearch-project.

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("number", NumberFieldMapper.NumberType.LONG);
    GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType);
    aggregator.preCollection();
    indexSearcher.search(new MatchAllDocsQuery(), aggregator);
    aggregator.postCollection();
    InternalGlobal result = (InternalGlobal) aggregator.buildTopLevel();
    verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global"));
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalGlobal(org.opensearch.search.aggregations.bucket.global.InternalGlobal) IndexReader(org.apache.lucene.index.IndexReader) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) GlobalAggregationBuilder(org.opensearch.search.aggregations.bucket.global.GlobalAggregationBuilder) GlobalAggregator(org.opensearch.search.aggregations.bucket.global.GlobalAggregator)

Aggregations

GlobalAggregator (org.opensearch.search.aggregations.bucket.global.GlobalAggregator)5 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Directory (org.apache.lucene.store.Directory)3 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collector (org.apache.lucene.search.Collector)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 Global (org.opensearch.search.aggregations.bucket.global.Global)2 TermsAggregationBuilder (org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder)2 InternalProfileCollector (org.opensearch.search.profile.query.InternalProfileCollector)2 DoublePoint (org.apache.lucene.document.DoublePoint)1 FloatPoint (org.apache.lucene.document.FloatPoint)1 IntPoint (org.apache.lucene.document.IntPoint)1 LongPoint (org.apache.lucene.document.LongPoint)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1