Skip to content

Commit

Permalink
Wire up IpRangeAggregation to ValuesSourceRegistry (elastic#55831)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Apr 28, 2020
1 parent ddc7305 commit 9723550
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ private ValuesSourceRegistry registerAggregations(List<SearchPlugin> plugins) {
.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ private static int compare(BytesRef a, BytesRef b, int m) {
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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,23 @@
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;
Expand All @@ -60,11 +66,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -210,6 +211,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
}

public static void registerAggregators(ValuesSourceRegistry.Builder builder) {
BinaryRangeAggregatorFactory.registerAggregators(builder);
}

private boolean keyed = false;
private List<Range> ranges = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 9723550

Please sign in to comment.