{"record":{"id":"8b95ccbda3720ce6","repo":"elastic/elasticsearch","slug":"aggregator-name-of-type-gettype-cannot-a","errorCode":null,"errorMessage":"Aggregator [{name}] of type [{getType()}] cannot accept sub-aggregations","messagePattern":"Aggregator \\[(.+?)\\] of type \\[(.+?)\\] cannot accept sub-aggregations","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/aggregations/src/main/java/org/elasticsearch/aggregations/metric/ArrayValuesSourceAggregationBuilder.java","lineNumber":48,"sourceCode":"import java.util.Objects;\n\npublic abstract class ArrayValuesSourceAggregationBuilder<AB extends ArrayValuesSourceAggregationBuilder<AB>> extends\n    AbstractAggregationBuilder<AB> {\n\n    public static final ParseField MULTIVALUE_MODE_FIELD = new ParseField(\"mode\");\n\n    public abstract static class LeafOnly<AB extends ArrayValuesSourceAggregationBuilder<AB>> extends ArrayValuesSourceAggregationBuilder<\n        AB> {\n\n        protected LeafOnly(String name) {\n            super(name);\n        }\n\n        @SuppressWarnings(\"this-escape\")\n        protected LeafOnly(LeafOnly<AB> clone, Builder factoriesBuilder, Map<String, Object> metadata) {\n            super(clone, factoriesBuilder, metadata);\n            if (factoriesBuilder.count() > 0) {\n                throw new IllegalArgumentException(\"Aggregator [\" + name + \"] of type [\" + getType() + \"] cannot accept sub-aggregations\");\n            }\n        }\n\n        /**\n         * Read from a stream\n         */\n        protected LeafOnly(StreamInput in) throws IOException {\n            super(in);\n        }\n\n        @Override\n        public AB subAggregations(Builder subFactories) {\n            throw new IllegalArgumentException(\"Aggregator [\" + name + \"] of type [\" + getType() + \"] cannot accept sub-aggregations\");\n        }\n\n        @Override\n        public final BucketCardinality bucketCardinality() {\n            return BucketCardinality.NONE;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/aggregations/src/main/java/org/elasticsearch/aggregations/metric/ArrayValuesSourceAggregationBuilder.java#L30-L66","documentation":"Thrown by the LeafOnly clone constructor of ArrayValuesSourceAggregationBuilder when factoriesBuilder.count() > 0 — i.e., during a rewrite/clone the builder discovers it has sub-aggregations attached. LeafOnly aggregations (multi-field variants of metrics like matrix_stats) produce a single metric value and cannot have sub-aggregations nested under them, so attaching any is a structural error caught at rewrite time.","triggerScenarios":"Calling .subAggregations(...)/.addAggregation(...) on a LeafOnly aggregation (or constructing one with a non-empty factories builder) and then triggering rewrite (e.g. via build() or the search execution path). The error surfaces during the clone-with-rewrite step rather than at the moment of attachment because the constructor path is the one that validates.","commonSituations":"Users nesting a sub-aggregation under a metric that does not support it (e.g. under matrix_stats). Programmatic builders that generically attach sub-aggregations to any aggregation type. REST requests with a `aggs` block nested inside a leaf-only metric.","solutions":["Remove the sub-aggregations from the LeafOnly metric — move them to a parent bucket aggregation if needed.","Check the aggregation type's documentation: metric aggregations like matrix_stats are leaf-only and cannot nest sub-aggregations.","Restructure so the metric is a sibling, not a parent, of the sub-aggregation."],"exampleFix":"// before: sub-aggregation nested under a leaf-only metric\n{\"matrix_stats\":{\"fields\":[\"a\",\"b\"],\"aggs\":{\"sub\":{\"avg\":{\"field\":\"c\"}}}}}\n// after: make them siblings\n{\"aggs\":{\"m\":{\"matrix_stats\":{\"fields\":[\"a\",\"b\"]}},\"sub\":{\"avg\":{\"field\":\"c\"}}}}","handlingStrategy":"type-guard","validationCode":"if (agg instanceof ArrayValuesSourceAggregationBuilder.LeafOnly<?> && subFactories.count() > 0) {\n  throw new IllegalArgumentException(\"leaf-only aggregation cannot have sub-aggregations\");\n}","typeGuard":"boolean isLeafOnly(org.elasticsearch.search.aggregations.AggregationBuilder b) {\n  return b instanceof ArrayValuesSourceAggregationBuilder.LeafOnly<?>;\n}","tryCatchPattern":null,"preventionTips":["Never nest sub-aggregations under leaf-only metrics (matrix_stats, etc.).","In generic builders, check BucketCardinality.NONE before attaching children.","Make sub-aggregations siblings of leaf metrics."],"tags":["aggregations","leaf-only","sub-aggregations","validation","structure"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}