Skip to content

Commit

Permalink
VS refactoring: Wire up median (MAD) aggregation (#52945)
Browse files Browse the repository at this point in the history
  • Loading branch information
csoulios authored Feb 28, 2020
1 parent 0e79fd5 commit d961faa
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,9 @@ private void registerAggregations(List<SearchPlugin> plugins) {
.addResultReader(InternalTDigestPercentileRanks.NAME, InternalTDigestPercentileRanks::new)
.addResultReader(InternalHDRPercentileRanks.NAME, InternalHDRPercentileRanks::new));
registerAggregation(new AggregationSpec(MedianAbsoluteDeviationAggregationBuilder.NAME,
MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder::parse)
.addResultReader(InternalMedianAbsoluteDeviation::new));
MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder::parse)
.addResultReader(InternalMedianAbsoluteDeviation::new)
.setAggregatorRegistrar(MedianAbsoluteDeviationAggregationBuilder::registerAggregators));
registerAggregation(new AggregationSpec(CardinalityAggregationBuilder.NAME, CardinalityAggregationBuilder::new,
CardinalityAggregationBuilder::parse).addResultReader(InternalCardinality::new)
.setAggregatorRegistrar(CardinalityAggregationBuilder::registerAggregators));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
import org.elasticsearch.search.aggregations.support.ValuesSourceType;

import java.io.IOException;
Expand All @@ -59,6 +60,10 @@ public static MedianAbsoluteDeviationAggregationBuilder parse(String aggregation
return PARSER.parse(parser, new MedianAbsoluteDeviationAggregationBuilder(aggregationName), null);
}

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
MedianAbsoluteDeviationAggregatorFactory.registerAggregators(valuesSourceRegistry);
}

private double compression = 1000d;

public MedianAbsoluteDeviationAggregationBuilder(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
package org.elasticsearch.search.aggregations.metrics;

import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
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;
Expand All @@ -39,22 +43,49 @@ public class MedianAbsoluteDeviationAggregatorFactory extends ValuesSourceAggreg
private final double compression;

MedianAbsoluteDeviationAggregatorFactory(String name,
ValuesSourceConfig config,
QueryShardContext queryShardContext,
AggregatorFactory parent,
AggregatorFactories.Builder subFactoriesBuilder,
Map<String, Object> metaData,
double compression) throws IOException {
ValuesSourceConfig config,
QueryShardContext queryShardContext,
AggregatorFactory parent,
AggregatorFactories.Builder subFactoriesBuilder,
Map<String, Object> metaData,
double compression) throws IOException {

super(name, config, queryShardContext, parent, subFactoriesBuilder, metaData);
this.compression = compression;
}

static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(MedianAbsoluteDeviationAggregationBuilder.NAME,
CoreValuesSourceType.NUMERIC,
new MedianAbsoluteDeviationAggregatorSupplier() {
@Override
public Aggregator build(String name,
ValuesSource valuesSource,
DocValueFormat format,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData,
double compression) throws IOException {
return new MedianAbsoluteDeviationAggregator(
name,
context,
parent,
pipelineAggregators,
metaData,
(ValuesSource.Numeric) valuesSource,
format,
compression
);
}
});
}

@Override
protected Aggregator createUnmapped(SearchContext searchContext,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {

return new MedianAbsoluteDeviationAggregator(
name,
Expand All @@ -75,20 +106,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
boolean collectsFromSingleBucket,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
MedianAbsoluteDeviationAggregationBuilder.NAME);

if (valuesSource instanceof ValuesSource.Numeric == false) {
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
this.name());
if (aggregatorSupplier instanceof MedianAbsoluteDeviationAggregatorSupplier == false) {
throw new AggregationExecutionException("Registry miss-match - expected MedianAbsoluteDeviationAggregatorSupplier, found [" +
aggregatorSupplier.getClass().toString() + "]");
}
return new MedianAbsoluteDeviationAggregator(
name,
searchContext,
parent,
pipelineAggregators,
metaData,
(ValuesSource.Numeric) valuesSource,
config.format(),
compression
);
return ((MedianAbsoluteDeviationAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(),
searchContext, parent, pipelineAggregators, metaData, compression);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.metrics;

import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
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 MedianAbsoluteDeviationAggregatorSupplier extends AggregatorSupplier {
Aggregator build(String name,
ValuesSource valuesSource,
DocValueFormat format,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData,
double compression) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
import java.util.Map;

public interface MetricAggregatorSupplier extends AggregatorSupplier {

Aggregator build(String name,
ValuesSource valuesSource,
DocValueFormat formatter,
SearchContext searchContext,
DocValueFormat format,
SearchContext context,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException;
Expand Down

0 comments on commit d961faa

Please sign in to comment.