|
18 | 18 | */
|
19 | 19 | package org.elasticsearch.search.aggregations;
|
20 | 20 |
|
| 21 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 22 | +import org.elasticsearch.action.ValidateActions; |
21 | 23 | import org.elasticsearch.common.Strings;
|
22 | 24 | import org.elasticsearch.common.io.stream.NamedWriteable;
|
23 | 25 | import org.elasticsearch.common.xcontent.ToXContentFragment;
|
24 | 26 | import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
|
| 27 | +import org.elasticsearch.search.aggregations.bucket.histogram.AutoDateHistogramAggregationBuilder; |
| 28 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; |
| 29 | +import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder; |
25 | 30 | import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
|
26 | 31 |
|
27 | 32 | import java.util.Collection;
|
28 | 33 | import java.util.Map;
|
| 34 | +import java.util.Objects; |
29 | 35 |
|
30 | 36 | /**
|
31 | 37 | * A factory that knows how to create an {@link PipelineAggregator} of a
|
@@ -64,11 +70,145 @@ public final String[] getBucketsPaths() {
|
64 | 70 | }
|
65 | 71 |
|
66 | 72 | /**
|
67 |
| - * Internal: Validates the state of this factory (makes sure the factory is properly |
68 |
| - * configured) |
| 73 | + * Makes sure this builder is properly configured. |
69 | 74 | */
|
70 |
| - protected abstract void validate(AggregatorFactory parent, Collection<AggregationBuilder> aggregationBuilders, |
71 |
| - Collection<PipelineAggregationBuilder> pipelineAggregatorBuilders); |
| 75 | + protected abstract void validate(ValidationContext context); |
| 76 | + public abstract static class ValidationContext { |
| 77 | + /** |
| 78 | + * Build the context for the root of the aggregation tree. |
| 79 | + */ |
| 80 | + public static ValidationContext forTreeRoot(Collection<AggregationBuilder> siblingAggregations, |
| 81 | + Collection<PipelineAggregationBuilder> siblingPipelineAggregations, |
| 82 | + ActionRequestValidationException validationFailuresSoFar) { |
| 83 | + return new ForTreeRoot(siblingAggregations, siblingPipelineAggregations, validationFailuresSoFar); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Build the context for a node inside the aggregation tree. |
| 88 | + */ |
| 89 | + public static ValidationContext forInsideTree(AggregationBuilder parent, |
| 90 | + ActionRequestValidationException validationFailuresSoFar) { |
| 91 | + return new ForInsideTree(parent, validationFailuresSoFar); |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + private ActionRequestValidationException e; |
| 96 | + |
| 97 | + private ValidationContext(ActionRequestValidationException validationFailuresSoFar) { |
| 98 | + this.e = validationFailuresSoFar; |
| 99 | + } |
| 100 | + |
| 101 | + private static class ForTreeRoot extends ValidationContext { |
| 102 | + private final Collection<AggregationBuilder> siblingAggregations; |
| 103 | + private final Collection<PipelineAggregationBuilder> siblingPipelineAggregations; |
| 104 | + |
| 105 | + ForTreeRoot(Collection<AggregationBuilder> siblingAggregations, |
| 106 | + Collection<PipelineAggregationBuilder> siblingPipelineAggregations, |
| 107 | + ActionRequestValidationException validationFailuresSoFar) { |
| 108 | + super(validationFailuresSoFar); |
| 109 | + this.siblingAggregations = Objects.requireNonNull(siblingAggregations); |
| 110 | + this.siblingPipelineAggregations = Objects.requireNonNull(siblingPipelineAggregations); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Collection<AggregationBuilder> getSiblingAggregations() { |
| 115 | + return siblingAggregations; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Collection<PipelineAggregationBuilder> getSiblingPipelineAggregations() { |
| 120 | + return siblingPipelineAggregations; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void validateParentAggSequentiallyOrdered(String type, String name) { |
| 125 | + addValidationError(type + " aggregation [" + name |
| 126 | + + "] must have a histogram, date_histogram or auto_date_histogram as parent but doesn't have a parent"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static class ForInsideTree extends ValidationContext { |
| 131 | + private final AggregationBuilder parent; |
| 132 | + |
| 133 | + ForInsideTree(AggregationBuilder parent, ActionRequestValidationException validationFailuresSoFar) { |
| 134 | + super(validationFailuresSoFar); |
| 135 | + this.parent = Objects.requireNonNull(parent); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public Collection<AggregationBuilder> getSiblingAggregations() { |
| 140 | + return parent.getSubAggregations(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public Collection<PipelineAggregationBuilder> getSiblingPipelineAggregations() { |
| 145 | + return parent.getPipelineAggregations(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void validateParentAggSequentiallyOrdered(String type, String name) { |
| 150 | + if (parent instanceof HistogramAggregationBuilder) { |
| 151 | + HistogramAggregationBuilder histoParent = (HistogramAggregationBuilder) parent; |
| 152 | + if (histoParent.minDocCount() != 0) { |
| 153 | + addValidationError( |
| 154 | + "parent histogram of " + type + " aggregation [" + name + "] must have min_doc_count of 0"); |
| 155 | + } |
| 156 | + } else if (parent instanceof DateHistogramAggregationBuilder) { |
| 157 | + DateHistogramAggregationBuilder histoParent = (DateHistogramAggregationBuilder) parent; |
| 158 | + if (histoParent.minDocCount() != 0) { |
| 159 | + addValidationError( |
| 160 | + "parent histogram of " + type + " aggregation [" + name + "] must have min_doc_count of 0"); |
| 161 | + } |
| 162 | + } else if (parent instanceof AutoDateHistogramAggregationBuilder) { |
| 163 | + // Nothing to check |
| 164 | + } else { |
| 165 | + addValidationError( |
| 166 | + type + " aggregation [" + name + "] must have a histogram, date_histogram or auto_date_histogram as parent"); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Aggregations that are siblings to the aggregation being validated. |
| 173 | + */ |
| 174 | + public abstract Collection<AggregationBuilder> getSiblingAggregations(); |
| 175 | + |
| 176 | + /** |
| 177 | + * Pipeline aggregations that are siblings to the aggregation being validated. |
| 178 | + */ |
| 179 | + public abstract Collection<PipelineAggregationBuilder> getSiblingPipelineAggregations(); |
| 180 | + |
| 181 | + /** |
| 182 | + * Add a validation error to this context. All validation errors |
| 183 | + * are accumulated in a list and, if there are any, the request |
| 184 | + * is not executed and the entire list is returned as the error |
| 185 | + * response. |
| 186 | + */ |
| 187 | + public void addValidationError(String error) { |
| 188 | + e = ValidateActions.addValidationError(error, e); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Add a validation error about the {@code buckets_path}. |
| 193 | + */ |
| 194 | + public void addBucketPathValidationError(String error) { |
| 195 | + addValidationError(PipelineAggregator.Parser.BUCKETS_PATH.getPreferredName() + ' ' + error); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Validates that the parent is sequentially ordered. |
| 200 | + */ |
| 201 | + public abstract void validateParentAggSequentiallyOrdered(String type, String name); |
| 202 | + |
| 203 | + /** |
| 204 | + * The validation exception, if there is one. It'll be {@code null} |
| 205 | + * if the context wasn't provided with any exception on creation |
| 206 | + * and none were added. |
| 207 | + */ |
| 208 | + public ActionRequestValidationException getValidationException() { |
| 209 | + return e; |
| 210 | + } |
| 211 | + } |
72 | 212 |
|
73 | 213 | /**
|
74 | 214 | * Creates the pipeline aggregator
|
|
0 commit comments