Wire up IpRangeAggregation to ValuesSourceRegistry (#55831) (#55859)

This commit is contained in:
Mark Tozzi 2020-04-28 12:10:21 -04:00 committed by GitHub
parent f38385ee25
commit bebbc375ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 9 deletions

View File

@ -460,7 +460,9 @@ public class SearchModule {
.addResultReader(InternalDateRange::new)
.setAggregatorRegistrar(DateRangeAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(IpRangeAggregationBuilder.NAME, IpRangeAggregationBuilder::new,
IpRangeAggregationBuilder.PARSER).addResultReader(InternalBinaryRange::new), builder);
IpRangeAggregationBuilder.PARSER)
.addResultReader(InternalBinaryRange::new)
.setAggregatorRegistrar(IpRangeAggregationBuilder::registerAggregators), builder);
registerAggregation(new AggregationSpec(HistogramAggregationBuilder.NAME, HistogramAggregationBuilder::new,
HistogramAggregationBuilder.PARSER)
.addResultReader(InternalHistogram::new)

View File

@ -77,11 +77,11 @@ public final class BinaryRangeAggregator extends BucketsAggregator {
final Range[] ranges;
public BinaryRangeAggregator(String name, AggregatorFactories factories,
ValuesSource.Bytes valuesSource, DocValueFormat format,
ValuesSource valuesSource, DocValueFormat format,
List<Range> ranges, boolean keyed, SearchContext context,
Aggregator parent, Map<String, Object> metadata) throws IOException {
super(name, factories, context, parent, metadata);
this.valuesSource = valuesSource;
this.valuesSource = (ValuesSource.Bytes) valuesSource;
this.format = format;
this.keyed = keyed;
this.ranges = ranges.toArray(new Range[0]);

View File

@ -23,17 +23,23 @@ import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public class BinaryRangeAggregatorFactory
extends ValuesSourceAggregatorFactory {
public class BinaryRangeAggregatorFactory extends ValuesSourceAggregatorFactory {
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
builder.register(IpRangeAggregationBuilder.NAME, CoreValuesSourceType.IP, (IpRangeAggregatorSupplier) BinaryRangeAggregator::new);
}
private final List<BinaryRangeAggregator.Range> ranges;
private final boolean keyed;
@ -60,11 +66,14 @@ public class BinaryRangeAggregatorFactory
SearchContext searchContext, Aggregator parent,
boolean collectsFromSingleBucket,
Map<String, Object> metadata) throws IOException {
if (valuesSource instanceof ValuesSource.Bytes == false) {
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
this.name());
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
IpRangeAggregationBuilder.NAME);
if (aggregatorSupplier instanceof IpRangeAggregatorSupplier == false) {
throw new AggregationExecutionException("Registry miss-match - expected IpRangeAggregatorSupplier, found [" +
aggregatorSupplier.getClass().toString() + "]");
}
return new BinaryRangeAggregator(name, factories, (ValuesSource.Bytes) valuesSource, config.format(),
return ((IpRangeAggregatorSupplier) aggregatorSupplier).build(name, factories, valuesSource, config.format(),
ranges, keyed, searchContext, parent, metadata);
}

View File

@ -40,6 +40,7 @@ import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
import java.io.IOException;
@ -210,6 +211,10 @@ public final class IpRangeAggregationBuilder
}
}
public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
BinaryRangeAggregatorFactory.registerAggregators(builder);
}
private boolean keyed = false;
private List<Range> ranges = new ArrayList<>();

View File

@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.bucket.range;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
import org.elasticsearch.search.aggregations.support.ValuesSource;
import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public interface IpRangeAggregatorSupplier extends AggregatorSupplier {
Aggregator build(String name,
AggregatorFactories factories,
ValuesSource valuesSource,
DocValueFormat format,
List<BinaryRangeAggregator.Range> ranges,
boolean keyed,
SearchContext context,
Aggregator parent,
Map<String, Object> metadata) throws IOException;
}