Skip to content

Commit

Permalink
ValuesSource refactor wire up missing agg (elastic#53511)
Browse files Browse the repository at this point in the history
Part of refactoring ValuesSource in elastic#42949
  • Loading branch information
andyb-elastic authored and not-napoleon committed Mar 26, 2020
1 parent a90c1de commit 151a347
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ private void registerAggregations(List<SearchPlugin> plugins) {
registerAggregation(new AggregationSpec(GlobalAggregationBuilder.NAME, GlobalAggregationBuilder::new,
GlobalAggregationBuilder::parse).addResultReader(InternalGlobal::new));
registerAggregation(new AggregationSpec(MissingAggregationBuilder.NAME, MissingAggregationBuilder::new,
MissingAggregationBuilder.PARSER).addResultReader(InternalMissing::new));
MissingAggregationBuilder.PARSER)
.addResultReader(InternalMissing::new)
.setAggregatorRegistrar(MissingAggregationBuilder::registerAggregators));
registerAggregation(new AggregationSpec(FilterAggregationBuilder.NAME, FilterAggregationBuilder::new,
FilterAggregationBuilder::parse).addResultReader(InternalFilter::new));
registerAggregation(new AggregationSpec(FiltersAggregationBuilder.NAME, FiltersAggregationBuilder::new,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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 All @@ -46,6 +47,10 @@ public class MissingAggregationBuilder extends ValuesSourceAggregationBuilder<Mi
ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false);
}

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

public MissingAggregationBuilder(String name) {
super(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
package org.elasticsearch.search.aggregations.bucket.missing;

import org.elasticsearch.index.query.QueryShardContext;
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 @@ -35,6 +39,22 @@

public class MissingAggregatorFactory extends ValuesSourceAggregatorFactory {

public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
valuesSourceRegistry.register(
MissingAggregationBuilder.NAME,
List.of(
CoreValuesSourceType.NUMERIC,
CoreValuesSourceType.BYTES,
CoreValuesSourceType.GEOPOINT,
CoreValuesSourceType.RANGE,
CoreValuesSourceType.IP,
CoreValuesSourceType.BOOLEAN,
CoreValuesSourceType.DATE
),
(MissingAggregatorSupplier) MissingAggregator::new
);
}

public MissingAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext,
AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder,
Map<String, Object> metaData) throws IOException {
Expand All @@ -50,13 +70,22 @@ protected MissingAggregator createUnmapped(SearchContext searchContext,
}

@Override
protected MissingAggregator doCreateInternal(ValuesSource valuesSource,
protected Aggregator doCreateInternal(ValuesSource valuesSource,
SearchContext searchContext,
Aggregator parent,
boolean collectsFromSingleBucket,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException {
return new MissingAggregator(name, factories, valuesSource, searchContext, parent, pipelineAggregators, metaData);

final AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry()
.getAggregator(config.valueSourceType(), MissingAggregationBuilder.NAME);
if (aggregatorSupplier instanceof MissingAggregatorSupplier == false) {
throw new AggregationExecutionException("Registry miss-match - expected MissingAggregatorSupplier, found [" +
aggregatorSupplier.getClass().toString() + "]");
}

return ((MissingAggregatorSupplier) aggregatorSupplier)
.build(name, factories, valuesSource, searchContext, parent, pipelineAggregators, metaData);
}

}
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.missing;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
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;

@FunctionalInterface
public interface MissingAggregatorSupplier extends AggregatorSupplier {

Aggregator build(String name,
AggregatorFactories factories,
@Nullable ValuesSource valuesSource,
SearchContext aggregationContext,
Aggregator parent,
List<PipelineAggregator> pipelineAggregators,
Map<String, Object> metaData) throws IOException;
}

0 comments on commit 151a347

Please sign in to comment.