Skip to content

Commit

Permalink
GlobalAggregation with profile option enabled returns incorrect result
Browse files Browse the repository at this point in the history
Signed-off-by: Sorabh Hamirwasia <sohami.apache@gmail.com>
  • Loading branch information
sohami committed Apr 11, 2023
1 parent a15475a commit f4ada0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void testWithStatsSubAggregator() throws Exception {
SearchResponse response = client().prepareSearch("idx")
.setQuery(QueryBuilders.termQuery("tag", "tag1"))
.addAggregation(global("global").subAggregation(stats("value_stats").field("value")))
.setProfile(randomBoolean())
.get();

assertSearchResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opensearch.common.lucene.search.Queries;
import org.opensearch.search.aggregations.bucket.global.GlobalAggregator;
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.profile.aggregation.ProfilingAggregator;
import org.opensearch.search.profile.query.CollectorResult;
import org.opensearch.search.profile.query.InternalProfileCollector;
import org.opensearch.search.query.QueryPhaseExecutionException;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void preProcess(SearchContext context) {
AggregatorFactories factories = context.aggregations().factories();
aggregators = factories.createTopLevelAggregators(context);
for (int i = 0; i < aggregators.length; i++) {
if (aggregators[i] instanceof GlobalAggregator == false) {
if (!isGlobalAggregator(context, aggregators[i])) {
collectors.add(aggregators[i]);
}
}
Expand Down Expand Up @@ -106,7 +107,7 @@ public void execute(SearchContext context) {
Aggregator[] aggregators = context.aggregations().aggregators();
List<Aggregator> globals = new ArrayList<>();
for (int i = 0; i < aggregators.length; i++) {
if (aggregators[i] instanceof GlobalAggregator) {
if (isGlobalAggregator(context, aggregators[i])) {
globals.add(aggregators[i]);
}
}
Expand Down Expand Up @@ -168,4 +169,16 @@ private Collector createCollector(SearchContext context, List<Aggregator> collec
}
return collector;
}

/**
* Checks if passed in aggregator is of type {@link GlobalAggregator}. This method takes care of Aggregator wrapped in
* {@link ProfilingAggregator} too
* @param context {@link SearchContext}
* @param aggregator input {@link Aggregator} instance to evaluate
* @return true input is {@link GlobalAggregator} instance or false otherwise
*/
private boolean isGlobalAggregator(SearchContext context, Aggregator aggregator) {
return (aggregator instanceof GlobalAggregator
|| (context.getProfilers() != null && ProfilingAggregator.unwrap(aggregator) instanceof GlobalAggregator));
}
}

0 comments on commit f4ada0c

Please sign in to comment.