Skip to content

Commit b58bbb9

Browse files
Allow setting aggs after parsing them elsewhere (#22238)
This commit exposes public getters for the aggregations in AggregatorFactories.Builder. The reason is that it allows to parse the aggregation object from elsewhere (e.g. a plugin) and then be able to get the aggregation builders in order to set them in a SearchSourceBuilder. The alternative would have been to expose a setter for the AggregatorFactories.Builder object. But that would be making the API a bit trappy.
1 parent ce5c094 commit b58bbb9

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.util.ArrayList;
35+
import java.util.Collections;
3536
import java.util.HashMap;
3637
import java.util.HashSet;
3738
import java.util.LinkedList;
@@ -255,7 +256,7 @@ private void resolvePipelineAggregatorOrder(Map<String, AggregationBuilder> aggB
255256
} else {
256257
// Check the non-pipeline sub-aggregator
257258
// factories
258-
AggregationBuilder[] subBuilders = aggBuilder.factoriesBuilder.getAggregatorFactories();
259+
List<AggregationBuilder> subBuilders = aggBuilder.factoriesBuilder.aggregationBuilders;
259260
boolean foundSubBuilder = false;
260261
for (AggregationBuilder subBuilder : subBuilders) {
261262
if (aggName.equals(subBuilder.name)) {
@@ -297,12 +298,12 @@ private void resolvePipelineAggregatorOrder(Map<String, AggregationBuilder> aggB
297298
}
298299
}
299300

300-
AggregationBuilder[] getAggregatorFactories() {
301-
return this.aggregationBuilders.toArray(new AggregationBuilder[this.aggregationBuilders.size()]);
301+
public List<AggregationBuilder> getAggregatorFactories() {
302+
return Collections.unmodifiableList(aggregationBuilders);
302303
}
303304

304-
List<PipelineAggregationBuilder> getPipelineAggregatorFactories() {
305-
return this.pipelineAggregatorBuilders;
305+
public List<PipelineAggregationBuilder> getPipelineAggregatorFactories() {
306+
return Collections.unmodifiableList(pipelineAggregatorBuilders);
306307
}
307308

308309
public int count() {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.search.aggregations;
20+
21+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders;
22+
import org.elasticsearch.test.ESTestCase;
23+
24+
import java.util.List;
25+
26+
import static org.hamcrest.Matchers.equalTo;
27+
28+
public class AggregatorFactoriesTests extends ESTestCase {
29+
30+
public void testGetAggregatorFactories_returnsUnmodifiableList() {
31+
AggregatorFactories.Builder builder = new AggregatorFactories.Builder().addAggregator(AggregationBuilders.avg("foo"));
32+
List<AggregationBuilder> aggregatorFactories = builder.getAggregatorFactories();
33+
assertThat(aggregatorFactories.size(), equalTo(1));
34+
expectThrows(UnsupportedOperationException.class, () -> aggregatorFactories.add(AggregationBuilders.avg("bar")));
35+
}
36+
37+
public void testGetPipelineAggregatorFactories_returnsUnmodifiableList() {
38+
AggregatorFactories.Builder builder = new AggregatorFactories.Builder().addPipelineAggregator(
39+
PipelineAggregatorBuilders.avgBucket("foo", "path1"));
40+
List<PipelineAggregationBuilder> pipelineAggregatorFactories = builder.getPipelineAggregatorFactories();
41+
assertThat(pipelineAggregatorFactories.size(), equalTo(1));
42+
expectThrows(UnsupportedOperationException.class,
43+
() -> pipelineAggregatorFactories.add(PipelineAggregatorBuilders.avgBucket("bar", "path2")));
44+
}
45+
}

0 commit comments

Comments
 (0)