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);
}
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);
}
}
}
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();
}
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();
}
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();
}
Aggregations