Skip to content

Commit d434a9d

Browse files
authored
ValuesSource refactoring: Wire up string_stats aggregation (#52875)
1 parent 3dea0e6 commit d434a9d

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public abstract class AggregatorTestCase extends ESTestCase {
135135
private static final String NESTEDFIELD_PREFIX = "nested_";
136136
private List<Releasable> releasables = new ArrayList<>();
137137
private static final String TYPE_NAME = "type";
138-
private static ValuesSourceRegistry valuesSourceRegistry;
138+
protected static ValuesSourceRegistry valuesSourceRegistry;
139139

140140
// A list of field types that should not be tested, or are not currently supported
141141
private static List<String> TYPE_TEST_BLACKLIST = List.of(
@@ -683,7 +683,7 @@ private void writeTestDoc(MappedFieldType fieldType, String fieldName, RandomInd
683683

684684
String typeName = fieldType.typeName();
685685
ValuesSourceType vst = fieldType.getValuesSourceType();
686-
686+
687687
if (vst.equals(CoreValuesSourceType.NUMERIC)) {
688688
if (typeName.equals(NumberFieldMapper.NumberType.DOUBLE.typeName())) {
689689
long encoded = NumericUtils.doubleToSortableLong(Math.abs(randomDouble()));

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/AnalyticsPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public List<AggregationSpec> getAggregations() {
6666
new AggregationSpec(
6767
StringStatsAggregationBuilder.NAME,
6868
StringStatsAggregationBuilder::new,
69-
StringStatsAggregationBuilder.PARSER).addResultReader(InternalStringStats::new),
69+
StringStatsAggregationBuilder.PARSER)
70+
.addResultReader(InternalStringStats::new)
71+
.setAggregatorRegistrar(StringStatsAggregationBuilder::registerAggregators),
7072
new AggregationSpec(
7173
BoxplotAggregationBuilder.NAME,
7274
BoxplotAggregationBuilder::new,

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregationBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
1919
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
2020
import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper;
21+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
2122
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
2223

2324
import java.io.IOException;
@@ -108,6 +109,10 @@ public StringStatsAggregationBuilder showDistribution(boolean showDistribution)
108109
return this;
109110
}
110111

112+
public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
113+
StringStatsAggregatorFactory.registerAggregators(valuesSourceRegistry);
114+
}
115+
111116
@Override
112117
public int hashCode() {
113118
return Objects.hash(super.hashCode(), showDistribution);

x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorFactory.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
package org.elasticsearch.xpack.analytics.stringstats;
88

99
import org.elasticsearch.index.query.QueryShardContext;
10+
import org.elasticsearch.search.DocValueFormat;
1011
import org.elasticsearch.search.aggregations.AggregationExecutionException;
1112
import org.elasticsearch.search.aggregations.Aggregator;
1213
import org.elasticsearch.search.aggregations.AggregatorFactories;
1314
import org.elasticsearch.search.aggregations.AggregatorFactory;
1415
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
16+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
17+
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
1518
import org.elasticsearch.search.aggregations.support.ValuesSource;
1619
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
1720
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
21+
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
1822
import org.elasticsearch.search.internal.SearchContext;
1923

2024
import java.io.IOException;
@@ -34,6 +38,24 @@ class StringStatsAggregatorFactory extends ValuesSourceAggregatorFactory {
3438
this.showDistribution = showDistribution;
3539
}
3640

41+
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
42+
valuesSourceRegistry.register(StringStatsAggregationBuilder.NAME,
43+
CoreValuesSourceType.BYTES, new StringStatsAggregatorSupplier() {
44+
@Override
45+
public Aggregator build(String name,
46+
ValuesSource valuesSource,
47+
boolean showDistribution,
48+
DocValueFormat format,
49+
SearchContext context,
50+
Aggregator parent,
51+
List<PipelineAggregator> pipelineAggregators,
52+
Map<String, Object> metaData) throws IOException {
53+
return new StringStatsAggregator(name, showDistribution, (ValuesSource.Bytes) valuesSource,
54+
format, context, parent, pipelineAggregators, metaData);
55+
}
56+
});
57+
}
58+
3759
@Override
3860
protected Aggregator createUnmapped(SearchContext searchContext,
3961
Aggregator parent,
@@ -50,12 +72,15 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource,
5072
boolean collectsFromSingleBucket,
5173
List<PipelineAggregator> pipelineAggregators,
5274
Map<String, Object> metaData) throws IOException {
53-
if (valuesSource instanceof ValuesSource.Bytes == false) {
54-
throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " +
55-
this.name());
75+
AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(),
76+
StringStatsAggregationBuilder.NAME);
77+
78+
if (aggregatorSupplier instanceof StringStatsAggregatorSupplier == false) {
79+
throw new AggregationExecutionException("Registry miss-match - expected StringStatsAggregatorSupplier, found [" +
80+
aggregatorSupplier.getClass().toString() + "]");
5681
}
57-
return new StringStatsAggregator(name, showDistribution, (ValuesSource.Bytes) valuesSource, config.format(), searchContext, parent,
58-
pipelineAggregators, metaData);
82+
return ((StringStatsAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, showDistribution, config.format(),
83+
searchContext, parent, pipelineAggregators, metaData);
5984
}
6085

6186
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.analytics.stringstats;
7+
8+
import org.elasticsearch.search.DocValueFormat;
9+
import org.elasticsearch.search.aggregations.Aggregator;
10+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
11+
import org.elasticsearch.search.aggregations.support.AggregatorSupplier;
12+
import org.elasticsearch.search.aggregations.support.ValuesSource;
13+
import org.elasticsearch.search.internal.SearchContext;
14+
15+
import java.io.IOException;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
public interface StringStatsAggregatorSupplier extends AggregatorSupplier {
20+
21+
Aggregator build(String name,
22+
ValuesSource valuesSource,
23+
boolean showDistribution,
24+
DocValueFormat format,
25+
SearchContext context,
26+
Aggregator parent,
27+
List<PipelineAggregator> pipelineAggregators,
28+
Map<String, Object> metaData) throws IOException;
29+
}

x-pack/plugin/analytics/src/test/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
2828
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator;
2929
import org.elasticsearch.search.aggregations.support.ValueType;
30+
import org.junit.BeforeClass;
3031

3132
import java.io.IOException;
3233
import java.util.List;
@@ -36,6 +37,11 @@
3637

3738
public class StringStatsAggregatorTests extends AggregatorTestCase {
3839

40+
@BeforeClass()
41+
public static void registerBuilder() {
42+
StringStatsAggregationBuilder.registerAggregators(valuesSourceRegistry);
43+
}
44+
3945
private void testCase(Query query,
4046
CheckedConsumer<RandomIndexWriter, IOException> buildIndex,
4147
Consumer<InternalStringStats> verify) throws IOException {
@@ -258,5 +264,4 @@ public void testNestedAggregation() throws IOException {
258264
indexReader.close();
259265
directory.close();
260266
}
261-
262267
}

0 commit comments

Comments
 (0)