Skip to content

Commit 151a347

Browse files
andyb-elasticnot-napoleon
authored andcommitted
ValuesSource refactor wire up missing agg (elastic#53511)
Part of refactoring ValuesSource in elastic#42949
1 parent a90c1de commit 151a347

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
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
@@ -381,7 +381,9 @@ private void registerAggregations(List<SearchPlugin> plugins) {
381381
registerAggregation(new AggregationSpec(GlobalAggregationBuilder.NAME, GlobalAggregationBuilder::new,
382382
GlobalAggregationBuilder::parse).addResultReader(InternalGlobal::new));
383383
registerAggregation(new AggregationSpec(MissingAggregationBuilder.NAME, MissingAggregationBuilder::new,
384-
MissingAggregationBuilder.PARSER).addResultReader(InternalMissing::new));
384+
MissingAggregationBuilder.PARSER)
385+
.addResultReader(InternalMissing::new)
386+
.setAggregatorRegistrar(MissingAggregationBuilder::registerAggregators));
385387
registerAggregation(new AggregationSpec(FilterAggregationBuilder.NAME, FilterAggregationBuilder::new,
386388
FilterAggregationBuilder::parse).addResultReader(InternalFilter::new));
387389
registerAggregation(new AggregationSpec(FiltersAggregationBuilder.NAME, FiltersAggregationBuilder::new,

server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
3333
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
3434
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
35+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3536
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
3637

3738
import java.io.IOException;
@@ -46,6 +47,10 @@ public class MissingAggregationBuilder extends ValuesSourceAggregationBuilder<Mi
4647
ValuesSourceAggregationBuilder.declareFields(PARSER, true, true, false);
4748
}
4849

50+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
51+
MissingAggregatorFactory.registerAggregators(valuesSourceRegistry);
52+
}
53+
4954
public MissingAggregationBuilder(String name) {
5055
super(name);
5156
}

server/src/main/java/org/elasticsearch/search/aggregations/bucket/missing/MissingAggregatorFactory.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
package org.elasticsearch.search.aggregations.bucket.missing;
2121

2222
import org.elasticsearch.index.query.QueryShardContext;
23+
import org.elasticsearch.search.aggregations.AggregationExecutionException;
2324
import org.elasticsearch.search.aggregations.Aggregator;
2425
import org.elasticsearch.search.aggregations.AggregatorFactories;
2526
import org.elasticsearch.search.aggregations.AggregatorFactory;
2627
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
28+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
29+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
2730
import org.elasticsearch.search.aggregations.support.ValuesSource;
2831
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
2932
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
33+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3034
import org.elasticsearch.search.internal.SearchContext;
3135

3236
import java.io.IOException;
@@ -35,6 +39,22 @@
3539

3640
public class MissingAggregatorFactory extends ValuesSourceAggregatorFactory {
3741

42+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
43+
valuesSourceRegistry.register(
44+
MissingAggregationBuilder.NAME,
45+
List.of(
46+
CoreValuesSourceType.NUMERIC,
47+
CoreValuesSourceType.BYTES,
48+
CoreValuesSourceType.GEOPOINT,
49+
CoreValuesSourceType.RANGE,
50+
CoreValuesSourceType.IP,
51+
CoreValuesSourceType.BOOLEAN,
52+
CoreValuesSourceType.DATE
53+
),
54+
(MissingAggregatorSupplier) MissingAggregator::new
55+
);
56+
}
57+
3858
public MissingAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext,
3959
AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder,
4060
Map<String, Object> metaData) throws IOException {
@@ -50,13 +70,22 @@ protected MissingAggregator createUnmapped(SearchContext searchContext,
5070
}
5171

5272
@Override
53-
protected MissingAggregator doCreateInternal(ValuesSource valuesSource,
73+
protected Aggregator doCreateInternal(ValuesSource valuesSource,
5474
SearchContext searchContext,
5575
Aggregator parent,
5676
boolean collectsFromSingleBucket,
5777
List<PipelineAggregator> pipelineAggregators,
5878
Map<String, Object> metaData) throws IOException {
59-
return new MissingAggregator(name, factories, valuesSource, searchContext, parent, pipelineAggregators, metaData);
79+
80+
final AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry()
81+
.getAggregator(config.valueSourceType(), MissingAggregationBuilder.NAME);
82+
if (aggregatorSupplier instanceof MissingAggregatorSupplier == false) {
83+
throw new AggregationExecutionException("Registry miss-match - expected MissingAggregatorSupplier, found [" +
84+
aggregatorSupplier.getClass().toString() + "]");
85+
}
86+
87+
return ((MissingAggregatorSupplier) aggregatorSupplier)
88+
.build(name, factories, valuesSource, searchContext, parent, pipelineAggregators, metaData);
6089
}
6190

6291
}
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.missing;
21+
22+
import org.elasticsearch.common.Nullable;
23+
import org.elasticsearch.search.aggregations.Aggregator;
24+
import org.elasticsearch.search.aggregations.AggregatorFactories;
25+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
26+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
27+
import org.elasticsearch.search.aggregations.support.ValuesSource;
28+
import org.elasticsearch.search.internal.SearchContext;
29+
30+
import java.io.IOException;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
@FunctionalInterface
35+
public interface MissingAggregatorSupplier extends AggregatorSupplier {
36+
37+
Aggregator build(String name,
38+
AggregatorFactories factories,
39+
@Nullable ValuesSource valuesSource,
40+
SearchContext aggregationContext,
41+
Aggregator parent,
42+
List<PipelineAggregator> pipelineAggregators,
43+
Map<String, Object> metaData) throws IOException;
44+
}

0 commit comments

Comments
 (0)