Skip to content

Commit e567ecb

Browse files
authored
Wire up Value Count (#52225)
1 parent d6caf2d commit e567ecb

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
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
@@ -353,7 +353,9 @@ private void registerAggregations(List<SearchPlugin> plugins) {
353353
registerAggregation(new AggregationSpec(ExtendedStatsAggregationBuilder.NAME, ExtendedStatsAggregationBuilder::new,
354354
ExtendedStatsAggregationBuilder::parse).addResultReader(InternalExtendedStats::new));
355355
registerAggregation(new AggregationSpec(ValueCountAggregationBuilder.NAME, ValueCountAggregationBuilder::new,
356-
ValueCountAggregationBuilder::parse).addResultReader(InternalValueCount::new));
356+
ValueCountAggregationBuilder::parse)
357+
.addResultReader(InternalValueCount::new)
358+
.setAggregatorRegistrar(ValueCountAggregationBuilder::registerAggregators));
357359
registerAggregation(new AggregationSpec(PercentilesAggregationBuilder.NAME, PercentilesAggregationBuilder::new,
358360
PercentilesAggregationBuilder::parse)
359361
.addResultReader(InternalTDigestPercentiles.NAME, InternalTDigestPercentiles::new)

server/src/main/java/org/elasticsearch/search/aggregations/metrics/CardinalityAggregatorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CardinalityAggregatorFactory extends ValuesSourceAggregatorFactory {
5151
}
5252

5353
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
54-
valuesSourceRegistry.register(CardinalityAggregationBuilder.NAME, (ignored) -> true, cardinalityAggregatorSupplier());
54+
valuesSourceRegistry.registerAny(CardinalityAggregationBuilder.NAME, cardinalityAggregatorSupplier());
5555
}
5656

5757
private static CardinalityAggregatorSupplier cardinalityAggregatorSupplier(){

server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
3535
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3636
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
37+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3738
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
3839

3940
import java.io.IOException;
@@ -52,6 +53,10 @@ public static AggregationBuilder parse(String aggregationName, XContentParser pa
5253
return PARSER.parse(parser, new ValueCountAggregationBuilder(aggregationName), null);
5354
}
5455

56+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
57+
ValueCountAggregatorFactory.registerAggregators(valuesSourceRegistry);
58+
}
59+
5560
public ValueCountAggregationBuilder(String name) {
5661
super(name);
5762
}

server/src/main/java/org/elasticsearch/search/aggregations/metrics/ValueCountAggregatorFactory.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
package org.elasticsearch.search.aggregations.metrics;
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;
2729
import org.elasticsearch.search.aggregations.support.ValuesSource;
2830
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
2931
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
32+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
3033
import org.elasticsearch.search.internal.SearchContext;
3134

3235
import java.io.IOException;
@@ -35,6 +38,21 @@
3538

3639
class ValueCountAggregatorFactory extends ValuesSourceAggregatorFactory {
3740

41+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
42+
valuesSourceRegistry.registerAny(ValueCountAggregationBuilder.NAME,
43+
new ValueCountAggregatorSupplier() {
44+
@Override
45+
public Aggregator build(String name,
46+
ValuesSource valuesSource,
47+
SearchContext aggregationContext,
48+
Aggregator parent,
49+
List<PipelineAggregator> pipelineAggregators,
50+
Map<String, Object> metaData) throws IOException {
51+
return new ValueCountAggregator(name, valuesSource, aggregationContext, parent, pipelineAggregators, metaData);
52+
}
53+
});
54+
}
55+
3856
ValueCountAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext,
3957
AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder,
4058
Map<String, Object> metaData) throws IOException {
@@ -56,6 +74,13 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
5674
boolean collectsFromSingleBucket,
5775
List<PipelineAggregator> pipelineAggregators,
5876
Map<String, Object> metaData) throws IOException {
59-
return new ValueCountAggregator(name, valuesSource, searchContext, parent, pipelineAggregators, metaData);
77+
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
78+
ValueCountAggregationBuilder.NAME);
79+
if (aggregatorSupplier instanceof ValueCountAggregatorSupplier == false) {
80+
throw new AggregationExecutionException("Registry miss-match - expected ValueCountAggregatorSupplier, found [" +
81+
aggregatorSupplier.getClass().toString() + "]");
82+
}
83+
return ((ValueCountAggregatorSupplier) aggregatorSupplier)
84+
.build(name, valuesSource, searchContext, parent, pipelineAggregators,metaData);
6085
}
6186
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
package org.elasticsearch.search.aggregations.metrics;
20+
21+
import org.elasticsearch.search.aggregations.Aggregator;
22+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
23+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
24+
import org.elasticsearch.search.aggregations.support.ValuesSource;
25+
import org.elasticsearch.search.internal.SearchContext;
26+
27+
import java.io.IOException;
28+
import java.util.List;
29+
import java.util.Map;
30+
31+
public interface ValueCountAggregatorSupplier extends AggregatorSupplier {
32+
Aggregator build(String name,
33+
ValuesSource valuesSource,
34+
SearchContext aggregationContext,
35+
Aggregator parent,
36+
List<PipelineAggregator> pipelineAggregators,
37+
Map<String, Object> metaData) throws IOException;
38+
}

server/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceRegistry.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ValuesSourceRegistry {
5454
* different worker threads. Thus we want to optimize the read case to be thread safe and fast, which the immutable
5555
* collections do well. Using immutable collections requires a copy on write mechanic, thus the somewhat non-intuitive
5656
* implementation of this method.
57-
* @param aggregationName The name of the family of aggregations, typically found via ValuesSourceAggregationBuilder.getType()
57+
* @param aggregationName The name of the family of aggregations, typically found via {@link ValuesSourceAggregationBuilder#getType()}
5858
* @param appliesTo A predicate which accepts the resolved {@link ValuesSourceType} and decides if the given aggregator can be applied
5959
* to that type.
6060
* @param aggregatorSupplier An Aggregation-specific specialization of AggregatorSupplier which will construct the mapped aggregator
@@ -75,7 +75,7 @@ public synchronized void register(String aggregationName, Predicate<ValuesSource
7575
/**
7676
* Register a ValuesSource to Aggregator mapping. This version provides a convenience method for mappings that only apply to a single
7777
* {@link ValuesSourceType}, to allow passing in the type and auto-wrapping it in a predicate
78-
* @param aggregationName The name of the family of aggregations, typically found via ValuesSourceAggregationBuilder.getType()
78+
* @param aggregationName The name of the family of aggregations, typically found via {@link ValuesSourceAggregationBuilder#getType()}
7979
* @param valuesSourceType The ValuesSourceType this mapping applies to.
8080
* @param aggregatorSupplier An Aggregation-specific specialization of AggregatorSupplier which will construct the mapped aggregator
8181
* from the aggregation standard set of parameters
@@ -87,7 +87,7 @@ public void register(String aggregationName, ValuesSourceType valuesSourceType,
8787
/**
8888
* Register a ValuesSource to Aggregator mapping. This version provides a convenience method for mappings that only apply to a known
8989
* list of {@link ValuesSourceType}, to allow passing in the type and auto-wrapping it in a predicate
90-
* @param aggregationName The name of the family of aggregations, typically found via ValuesSourceAggregationBuilder.getType()
90+
* @param aggregationName The name of the family of aggregations, typically found via {@link ValuesSourceAggregationBuilder#getType()}
9191
* @param valuesSourceTypes The ValuesSourceTypes this mapping applies to.
9292
* @param aggregatorSupplier An Aggregation-specific specialization of AggregatorSupplier which will construct the mapped aggregator
9393
* from the aggregation standard set of parameters
@@ -103,6 +103,19 @@ public void register(String aggregationName, List<ValuesSourceType> valuesSource
103103
}, aggregatorSupplier);
104104
}
105105

106+
/**
107+
* Register an aggregator that applies to any values source type. This is a convenience method for aggregations that do not care at all
108+
* about the types of their inputs. Aggregations using this version of registration should not make any other registrations, as the
109+
* aggregator registered using this function will be applied in all cases.
110+
*
111+
* @param aggregationName The name of the family of aggregations, typically found via {@link ValuesSourceAggregationBuilder#getType()}
112+
* @param aggregatorSupplier An Aggregation-specific specialization of AggregatorSupplier which will construct the mapped aggregator
113+
* from the aggregation standard set of parameters.
114+
*/
115+
public void registerAny(String aggregationName, AggregatorSupplier aggregatorSupplier) {
116+
register(aggregationName, (ignored) -> true, aggregatorSupplier);
117+
}
118+
106119
private AggregatorSupplier findMatchingSuppier(ValuesSourceType valuesSourceType,
107120
List<Map.Entry<Predicate<ValuesSourceType>, AggregatorSupplier>> supportedTypes) {
108121
for (Map.Entry<Predicate<ValuesSourceType>, AggregatorSupplier> candidate : supportedTypes) {

0 commit comments

Comments
 (0)