Skip to content

Commit 0331312

Browse files
authored
ValuesSourceRegistry Prototype (#48758)
1 parent fc3509c commit 0331312

File tree

16 files changed

+361
-93
lines changed

16 files changed

+361
-93
lines changed

modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class ChildrenAggregationBuilder
6262
*/
6363
public ChildrenAggregationBuilder(String name, String childType) {
6464
super(name, ValueType.STRING);
65-
f (childType == null) {
65+
if (childType == null) {
6666
throw new IllegalArgumentException("[childType] must not be null: [" + name + "]");
6767
}
6868
this.childType = childType;

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeValuesSourceBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ protected abstract CompositeValuesSourceConfig innerBuild(QueryShardContext quer
276276

277277
public final CompositeValuesSourceConfig build(QueryShardContext queryShardContext) throws IOException {
278278
ValuesSourceConfig<?> config = ValuesSourceConfig.resolve(queryShardContext,
279-
valueType, field, script, null,null, format);
279+
valueType, field, script, null,null, format, name());
280280
return innerBuild(queryShardContext, config);
281281
}
282282
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/HistogramAggregatorFactory.java

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@
1919

2020
package org.elasticsearch.search.aggregations.bucket.histogram;
2121

22+
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
23+
import org.elasticsearch.index.mapper.RangeFieldMapper;
2224
import org.elasticsearch.index.query.QueryShardContext;
25+
import org.elasticsearch.search.DocValueFormat;
26+
import org.elasticsearch.search.aggregations.AggregationExecutionException;
2327
import org.elasticsearch.search.aggregations.Aggregator;
2428
import org.elasticsearch.search.aggregations.AggregatorFactories;
2529
import org.elasticsearch.search.aggregations.AggregatorFactory;
2630
import org.elasticsearch.search.aggregations.BucketOrder;
2731
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
32+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
33+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
34+
import org.elasticsearch.search.aggregations.support.HistogramAggregatorSupplier;
2835
import org.elasticsearch.search.aggregations.support.ValuesSource;
2936
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
3037
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
38+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3139
import org.elasticsearch.search.internal.SearchContext;
3240

3341
import java.io.IOException;
@@ -46,6 +54,45 @@ public final class HistogramAggregatorFactory extends ValuesSourceAggregatorFact
4654
private final long minDocCount;
4755
private final double minBound, maxBound;
4856

57+
// TODO: Registration should happen on the actual aggregator classes, but I don't want to set up the whole dynamic loading thing yet
58+
static {
59+
ValuesSourceRegistry.getInstance().register(HistogramAggregationBuilder.NAME, CoreValuesSourceType.RANGE,
60+
new HistogramAggregatorSupplier() {
61+
@Override
62+
public Aggregator build(String name, AggregatorFactories factories, double interval, double offset,
63+
BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound,
64+
ValuesSource valuesSource, DocValueFormat formatter, SearchContext context,
65+
Aggregator parent, List<PipelineAggregator> pipelineAggregators,
66+
Map<String, Object> metaData) throws IOException {
67+
ValuesSource.Range rangeValueSource = (ValuesSource.Range) valuesSource;
68+
if (rangeValueSource.rangeType().isNumeric() == false) {
69+
throw new IllegalArgumentException("Expected numeric range type but found non-numeric range ["
70+
+ rangeValueSource.rangeType().name + "]");
71+
}
72+
return new RangeHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound,
73+
maxBound, rangeValueSource, formatter, context, parent, pipelineAggregators, metaData);
74+
}
75+
},
76+
(fieldType, indexFieldData) -> fieldType instanceof RangeFieldMapper.RangeFieldType
77+
);
78+
79+
ValuesSourceRegistry.getInstance().register(HistogramAggregationBuilder.NAME, CoreValuesSourceType.NUMERIC,
80+
new HistogramAggregatorSupplier() {
81+
@Override
82+
public Aggregator build(String name, AggregatorFactories factories, double interval, double offset,
83+
BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound,
84+
ValuesSource valuesSource, DocValueFormat formatter, SearchContext context,
85+
Aggregator parent, List<PipelineAggregator> pipelineAggregators,
86+
Map<String, Object> metaData) throws IOException {
87+
return new NumericHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound,
88+
maxBound, (ValuesSource.Numeric) valuesSource, formatter, context, parent, pipelineAggregators, metaData);
89+
}
90+
},
91+
(fieldType, indexFieldData) -> indexFieldData instanceof IndexNumericFieldData
92+
);
93+
}
94+
95+
4996
@Override
5097
protected ValuesSource resolveMissingAny(Object missing) {
5198
if (missing instanceof Number) {
@@ -92,23 +139,16 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
92139
if (collectsFromSingleBucket == false) {
93140
return asMultiBucketAggregator(this, searchContext, parent);
94141
}
95-
if (valuesSource instanceof ValuesSource.Numeric) {
96-
return new NumericHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound, maxBound,
97-
(ValuesSource.Numeric) valuesSource, config.format(), searchContext, parent, pipelineAggregators, metaData);
98-
} else if (valuesSource instanceof ValuesSource.Range) {
99-
ValuesSource.Range rangeValueSource = (ValuesSource.Range) valuesSource;
100-
if (rangeValueSource.rangeType().isNumeric() == false) {
101-
throw new IllegalArgumentException("Expected numeric range type but found non-numeric range ["
102-
+ rangeValueSource.rangeType().name + "]");
103-
}
104-
return new RangeHistogramAggregator(name, factories, interval, offset, order, keyed, minDocCount, minBound, maxBound,
105-
(ValuesSource.Range) valuesSource, config.format(), searchContext, parent, pipelineAggregators,
106-
metaData);
107-
}
108-
else {
109-
throw new IllegalArgumentException("Expected one of [Numeric, Range] values source, found ["
110-
+ valuesSource.toString() + "]");
142+
143+
AggregatorSupplier aggregatorSupplier = ValuesSourceRegistry.getInstance().getAggregator(config.valueSourceType(),
144+
HistogramAggregationBuilder.NAME);
145+
if (aggregatorSupplier instanceof HistogramAggregatorSupplier == false) {
146+
throw new AggregationExecutionException("Registry miss-match - expected HistogramAggregatorSupplier, found [" +
147+
aggregatorSupplier.getClass().toString() + "]");
111148
}
149+
HistogramAggregatorSupplier histogramAggregatorSupplier = (HistogramAggregatorSupplier) aggregatorSupplier;
150+
return histogramAggregatorSupplier.build(name, factories, interval, offset, order, keyed, minDocCount, minBound, maxBound,
151+
valuesSource, config.format(), searchContext, parent, pipelineAggregators, metaData);
112152
}
113153

114154
@Override

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/NumericHistogramAggregator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
import org.elasticsearch.search.DocValueFormat;
3030
import org.elasticsearch.search.aggregations.Aggregator;
3131
import org.elasticsearch.search.aggregations.AggregatorFactories;
32+
import org.elasticsearch.search.aggregations.BucketOrder;
3233
import org.elasticsearch.search.aggregations.InternalAggregation;
34+
import org.elasticsearch.search.aggregations.InternalOrder;
3335
import org.elasticsearch.search.aggregations.LeafBucketCollector;
3436
import org.elasticsearch.search.aggregations.LeafBucketCollectorBase;
3537
import org.elasticsearch.search.aggregations.bucket.BucketsAggregator;
3638
import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram.EmptyBucketInfo;
3739
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
38-
import org.elasticsearch.search.aggregations.BucketOrder;
39-
import org.elasticsearch.search.aggregations.InternalOrder;
4040
import org.elasticsearch.search.aggregations.support.ValuesSource;
4141
import org.elasticsearch.search.internal.SearchContext;
4242

@@ -52,7 +52,7 @@
5252
* written as {@code interval * x + offset} and yet is less than or equal to
5353
* {@code value}.
5454
*/
55-
class NumericHistogramAggregator extends BucketsAggregator {
55+
public class NumericHistogramAggregator extends BucketsAggregator {
5656

5757
private final ValuesSource.Numeric valuesSource;
5858
private final DocValueFormat formatter;
@@ -64,7 +64,7 @@ class NumericHistogramAggregator extends BucketsAggregator {
6464

6565
private final LongHash bucketOrds;
6666

67-
NumericHistogramAggregator(String name, AggregatorFactories factories, double interval, double offset,
67+
public NumericHistogramAggregator(String name, AggregatorFactories factories, double interval, double offset,
6868
BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound,
6969
@Nullable ValuesSource.Numeric valuesSource, DocValueFormat formatter,
7070
SearchContext context, Aggregator parent,

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/RangeHistogramAggregator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class RangeHistogramAggregator extends BucketsAggregator {
5858

5959
private final LongHash bucketOrds;
6060

61-
RangeHistogramAggregator(String name, AggregatorFactories factories, double interval, double offset,
61+
public RangeHistogramAggregator(String name, AggregatorFactories factories, double interval, double offset,
6262
BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound,
6363
@Nullable ValuesSource.Range valuesSource, DocValueFormat formatter,
6464
SearchContext context, Aggregator parent,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.support;
20+
21+
public interface AggregatorSupplier {
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.support;
20+
21+
import org.elasticsearch.common.Nullable;
22+
import org.elasticsearch.search.DocValueFormat;
23+
import org.elasticsearch.search.aggregations.Aggregator;
24+
import org.elasticsearch.search.aggregations.AggregatorFactories;
25+
import org.elasticsearch.search.aggregations.BucketOrder;
26+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
27+
import org.elasticsearch.search.internal.SearchContext;
28+
29+
import java.io.IOException;
30+
import java.util.List;
31+
import java.util.Map;
32+
33+
public interface HistogramAggregatorSupplier extends AggregatorSupplier {
34+
Aggregator build(String name, AggregatorFactories factories, double interval, double offset,
35+
BucketOrder order, boolean keyed, long minDocCount, double minBound, double maxBound,
36+
@Nullable ValuesSource valuesSource, DocValueFormat formatter,
37+
SearchContext context, Aggregator parent,
38+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException;
39+
}

server/src/main/java/org/elasticsearch/search/aggregations/support/MultiValuesSourceAggregationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected final MultiValuesSourceAggregatorFactory<VS> doBuild(QueryShardContext
170170
Map<String, ValuesSourceConfig<VS>> configs = new HashMap<>(fields.size());
171171
fields.forEach((key, value) -> {
172172
ValuesSourceConfig<VS> config = ValuesSourceConfig.resolve(queryShardContext, finalValueType,
173-
value.getFieldName(), value.getScript(), value.getMissing(), value.getTimeZone(), format);
173+
value.getFieldName(), value.getScript(), value.getMissing(), value.getTimeZone(), format, getType());
174174
configs.put(key, config);
175175
});
176176
DocValueFormat docValueFormat = resolveFormat(format, finalValueType);

server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceAggregationBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ protected ValueType defaultValueType(Script script) {
330330
protected ValuesSourceConfig<VS> resolveConfig(QueryShardContext queryShardContext) {
331331
ValueType valueType = this.valueType != null ? this.valueType : targetValueType;
332332
return ValuesSourceConfig.resolve(queryShardContext,
333-
valueType, field, script, missing, timeZone, format, this::resolveScriptAny);
333+
valueType, field, script, missing, timeZone, format, this::resolveScriptAny, this.getType());
334334
}
335335

336336
protected abstract ValuesSourceAggregatorFactory<VS> innerBuild(QueryShardContext queryShardContext,

0 commit comments

Comments
 (0)