Skip to content

Better error message when buckets_path refers to multi-bucket agg #30152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,16 @@ public boolean isMapped() {
*/
public Object getProperty(String path) {
AggregationPath aggPath = AggregationPath.parse(path);
return getProperty(aggPath.getPathElementsAsStringList());
return getProperty(aggPath.getPathElementsAsStringList(), true);
}

public abstract Object getProperty(List<String> path);
/**
*
* @param path the path to the property in the aggregation tree
* @param allowMultiBucket true if multi-bucket aggregations are allowed to be processed, false if exception should be thrown
* @return the value of the property
*/
public abstract Object getProperty(List<String> path, boolean allowMultiBucket);

/**
* Read a size under the assumption that a value of 0 means unlimited.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ protected InternalMultiBucketAggregation(StreamInput in) throws IOException {
public abstract List<? extends InternalBucket> getBuckets();

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
boolean needsBucketCount = path.isEmpty() == false && path.get(0).equals("_bucket_count") ? true : false;
if (allowMultiBucket == false && needsBucketCount == false) {
throw new AggregationExecutionException("[" + getName() + "] is a [" + getType()
+ "], but only number value or a single value numeric metric aggregations are allowed.");
}

if (path.isEmpty()) {
return this;
} else if (path.get(0).equals("_bucket_count")) {
Expand Down Expand Up @@ -119,6 +125,10 @@ public static int countInnerBucket(Aggregation agg) {
public abstract static class InternalBucket implements Bucket, Writeable {

public Object getProperty(String containingAggName, List<String> path) {
return getProperty(containingAggName, path, true);
}

public Object getProperty(String containingAggName, List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
}
Expand All @@ -140,7 +150,8 @@ public Object getProperty(String containingAggName, List<String> path) {
throw new InvalidAggregationPathException("Cannot find an aggregation named [" + aggName + "] in [" + containingAggName
+ "]");
}
return aggregation.getProperty(path.subList(1, path.size()));

return aggregation.getProperty(path.subList(1, path.size()), allowMultiBucket);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.InternalAggregations;
import org.elasticsearch.search.aggregations.InternalMultiBucketAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

import java.io.IOException;
Expand Down Expand Up @@ -110,7 +112,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else {
Expand All @@ -125,7 +127,11 @@ public Object getProperty(List<String> path) {
if (aggregation == null) {
throw new IllegalArgumentException("Cannot find an aggregation named [" + aggName + "] in [" + getName() + "]");
}
return aggregation.getProperty(path.subList(1, path.size()));
if (allowMultiBucket == false && aggregation instanceof InternalMultiBucketAggregation) {
throw new AggregationExecutionException("[" + aggName + "] is a [" + aggregation.getType()
+ "], but only number value or a single value numeric metric aggregations are allowed.");
}
return aggregation.getProperty(path.subList(1, path.size()), allowMultiBucket);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public InternalGeoCentroid doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

Expand Down Expand Up @@ -52,7 +53,7 @@ public String getValueAsString() {
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1 && "value".equals(path.get(0))) {
Expand Down Expand Up @@ -83,8 +84,13 @@ public String valueAsString(String name) {
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
if (allowMultiBucket == false) {
throw new AggregationExecutionException("[" + getName() + "] is a [" + getType()
+ "] which contains multiple values, but only number value or a single value numeric metric aggregation. Please " +
"specify which value to return.");
}
return this;
} else if (path.size() == 1) {
return value(path.get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1 && "value".equals(path.get(0))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static Double resolveBucketValue(MultiBucketsAggregation agg,
public static Double resolveBucketValue(MultiBucketsAggregation agg,
InternalMultiBucketAggregation.InternalBucket bucket, List<String> aggPathAsList, GapPolicy gapPolicy) {
try {
Object propertyValue = bucket.getProperty(agg.getName(), aggPathAsList);
Object propertyValue = bucket.getProperty(agg.getName(), aggPathAsList, false);
if (propertyValue == null) {
throw new AggregationExecutionException(AbstractPipelineAggregationBuilder.BUCKETS_PATH_FIELD.getPreferredName()
+ " must reference either a number value or a single value numeric metric aggregation");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1 && "value".equals(path.get(0))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ DocValueFormat formatter() {
}

@Override
public Object getProperty(List<String> path) {
public Object getProperty(List<String> path, boolean allowMultiBucket) {
if (path.isEmpty()) {
return this;
} else if (path.size() == 1 && "value".equals(path.get(0))) {
Expand Down
Loading