Skip to content

Commit bebbc37

Browse files
authored
Wire up IpRangeAggregation to ValuesSourceRegistry (#55831) (#55859)
1 parent f38385e commit bebbc37

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,9 @@ private ValuesSourceRegistry registerAggregations(List<SearchPlugin> plugins) {
460460
.addResultReader(InternalDateRange::new)
461461
.setAggregatorRegistrar(DateRangeAggregationBuilder::registerAggregators), builder);
462462
registerAggregation(new AggregationSpec(IpRangeAggregationBuilder.NAME, IpRangeAggregationBuilder::new,
463-
IpRangeAggregationBuilder.PARSER).addResultReader(InternalBinaryRange::new), builder);
463+
IpRangeAggregationBuilder.PARSER)
464+
.addResultReader(InternalBinaryRange::new)
465+
.setAggregatorRegistrar(IpRangeAggregationBuilder::registerAggregators), builder);
464466
registerAggregation(new AggregationSpec(HistogramAggregationBuilder.NAME, HistogramAggregationBuilder::new,
465467
HistogramAggregationBuilder.PARSER)
466468
.addResultReader(InternalHistogram::new)

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/BinaryRangeAggregator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ private static int compare(BytesRef a, BytesRef b, int m) {
7777
final Range[] ranges;
7878

7979
public BinaryRangeAggregator(String name, AggregatorFactories factories,
80-
ValuesSource.Bytes valuesSource, DocValueFormat format,
80+
ValuesSource valuesSource, DocValueFormat format,
8181
List<Range> ranges, boolean keyed, SearchContext context,
8282
Aggregator parent, Map<String, Object> metadata) throws IOException {
8383
super(name, factories, context, parent, metadata);
84-
this.valuesSource = valuesSource;
84+
this.valuesSource = (ValuesSource.Bytes) valuesSource;
8585
this.format = format;
8686
this.keyed = keyed;
8787
this.ranges = ranges.toArray(new Range[0]);

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/BinaryRangeAggregatorFactory.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@
2323
import org.elasticsearch.search.aggregations.Aggregator;
2424
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
2525
import org.elasticsearch.search.aggregations.AggregatorFactory;
26+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
27+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
2628
import org.elasticsearch.search.aggregations.support.ValuesSource;
2729
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
2830
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
31+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
2932
import org.elasticsearch.search.internal.SearchContext;
3033

3134
import java.io.IOException;
3235
import java.util.List;
3336
import java.util.Map;
3437

35-
public class BinaryRangeAggregatorFactory
36-
extends ValuesSourceAggregatorFactory {
38+
public class BinaryRangeAggregatorFactory extends ValuesSourceAggregatorFactory {
39+
40+
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
41+
builder.register(IpRangeAggregationBuilder.NAME, CoreValuesSourceType.IP, (IpRangeAggregatorSupplier) BinaryRangeAggregator::new);
42+
}
3743

3844
private final List<BinaryRangeAggregator.Range> ranges;
3945
private final boolean keyed;
@@ -60,11 +66,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
6066
SearchContext searchContext, Aggregator parent,
6167
boolean collectsFromSingleBucket,
6268
Map<String, Object> metadata) throws IOException {
63-
if (valuesSource instanceof ValuesSource.Bytes == false) {
64-
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
65-
this.name());
69+
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
70+
IpRangeAggregationBuilder.NAME);
71+
72+
if (aggregatorSupplier instanceof IpRangeAggregatorSupplier == false) {
73+
throw new AggregationExecutionException("Registry miss-match - expected IpRangeAggregatorSupplier, found [" +
74+
aggregatorSupplier.getClass().toString() + "]");
6675
}
67-
return new BinaryRangeAggregator(name, factories, (ValuesSource.Bytes) valuesSource, config.format(),
76+
return ((IpRangeAggregatorSupplier) aggregatorSupplier).build(name, factories, valuesSource, config.format(),
6877
ranges, keyed, searchContext, parent, metadata);
6978
}
7079

server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/IpRangeAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
4141
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
4242
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
43+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
4344
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
4445

4546
import java.io.IOException;
@@ -210,6 +211,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
210211
}
211212
}
212213

214+
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
215+
BinaryRangeAggregatorFactory.registerAggregators(builder);
216+
}
217+
213218
private boolean keyed = false;
214219
private List<Range> ranges = new ArrayList<>();
215220

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
20+
package org.elasticsearch.search.aggregations.bucket.range;
21+
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.support.AggregatorSupplier;
26+
import org.elasticsearch.search.aggregations.support.ValuesSource;
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 IpRangeAggregatorSupplier extends AggregatorSupplier {
34+
35+
Aggregator build(String name,
36+
AggregatorFactories factories,
37+
ValuesSource valuesSource,
38+
DocValueFormat format,
39+
List<BinaryRangeAggregator.Range> ranges,
40+
boolean keyed,
41+
SearchContext context,
42+
Aggregator parent,
43+
Map<String, Object> metadata) throws IOException;
44+
}

0 commit comments

Comments
 (0)