Skip to content

Commit

Permalink
Fix profiling of ordered terms aggs (#31814)
Browse files Browse the repository at this point in the history
This change fixes the profiling of `terms` aggregation ordered
by a sub aggregation.

Closes #22123
  • Loading branch information
jimczi authored Jul 6, 2018
1 parent a81dfbc commit bf9ae5f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.search.aggregations.bucket.SingleBucketAggregator;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregator;
import org.elasticsearch.search.profile.aggregation.ProfilingAggregator;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -256,7 +257,7 @@ public Aggregator resolveAggregator(Aggregator root) {
Aggregator aggregator = root;
for (int i = 0; i < pathElements.size(); i++) {
AggregationPath.PathElement token = pathElements.get(i);
aggregator = aggregator.subAggregator(token.name);
aggregator = ProfilingAggregator.unwrap(aggregator.subAggregator(token.name));
assert (aggregator instanceof SingleBucketAggregator && i <= pathElements.size() - 1)
|| (aggregator instanceof NumericMetricsAggregator && i == pathElements.size() - 1) :
"this should be picked up before aggregation execution - on validate";
Expand All @@ -272,7 +273,7 @@ public Aggregator resolveAggregator(Aggregator root) {
*/
public Aggregator resolveTopmostAggregator(Aggregator root) {
AggregationPath.PathElement token = pathElements.get(0);
Aggregator aggregator = root.subAggregator(token.name);
Aggregator aggregator = ProfilingAggregator.unwrap(root.subAggregator(token.name));
assert (aggregator instanceof SingleBucketAggregator )
|| (aggregator instanceof NumericMetricsAggregator) : "this should be picked up before aggregation execution - on validate";
return aggregator;
Expand All @@ -287,7 +288,7 @@ public Aggregator resolveTopmostAggregator(Aggregator root) {
public void validate(Aggregator root) throws AggregationExecutionException {
Aggregator aggregator = root;
for (int i = 0; i < pathElements.size(); i++) {
aggregator = aggregator.subAggregator(pathElements.get(i).name);
aggregator = ProfilingAggregator.unwrap(aggregator.subAggregator(pathElements.get(i).name));
if (aggregator == null) {
throw new AggregationExecutionException("Invalid aggregator order path [" + this + "]. Unknown aggregation ["
+ pathElements.get(i).name + "]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,11 @@ public void postCollection() throws IOException {
public String toString() {
return delegate.toString();
}

public static Aggregator unwrap(Aggregator agg) {
if (agg instanceof ProfilingAggregator) {
return ((ProfilingAggregator) agg).delegate;
}
return agg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedOrdinalsSamplerAggregator;
import org.elasticsearch.search.aggregations.bucket.terms.GlobalOrdinalsStringTermsAggregator;
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregator;
Expand Down Expand Up @@ -120,9 +121,17 @@ public void testSimpleProfile() {

public void testMultiLevelProfile() {
SearchResponse response = client().prepareSearch("idx").setProfile(true)
.addAggregation(histogram("histo").field(NUMBER_FIELD).interval(1L)
.subAggregation(terms("terms").field(TAG_FIELD)
.subAggregation(avg("avg").field(NUMBER_FIELD)))).get();
.addAggregation(
histogram("histo")
.field(NUMBER_FIELD)
.interval(1L)
.subAggregation(
terms("terms")
.field(TAG_FIELD)
.order(BucketOrder.aggregation("avg", false))
.subAggregation(avg("avg").field(NUMBER_FIELD))
)
).get();
assertSearchResponse(response);
Map<String, ProfileShardResult> profileResults = response.getProfileResults();
assertThat(profileResults, notNullValue());
Expand Down

0 comments on commit bf9ae5f

Please sign in to comment.