Skip to content

Commit ba2210e

Browse files
authored
ValuesSource Refactor: move histo VSType into XPack module (#53298)
- Introduces a new API (`getBareAggregatorRegistrar()`) which allows plugins to register aggregations against existing agg definitions defined in Core. - This moves the histogram VSType over to XPack where it belongs. `getHistogramValues()` still remains as a Core concept - Moves the histo-specific bits over to xpack (e.g. the actual aggregator logic). This requires extra boilerplate since we need to create a new "Analytics" Percentile/Rank aggregators to deal with the histo field. Doubly-so since percentiles/ranks are extra boiler-plate'y... should be much lighter for other aggs
1 parent 64d5c84 commit ba2210e

File tree

37 files changed

+856
-187
lines changed

37 files changed

+856
-187
lines changed

server/src/main/java/org/elasticsearch/plugins/SearchPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ default List<QuerySpec<?>> getQueries() {
114114
default List<AggregationSpec> getAggregations() {
115115
return emptyList();
116116
}
117+
/**
118+
* Allows plugins to register new aggregations using aggregation names that are already defined
119+
* in Core, as long as the new aggregations target different ValuesSourceTypes
120+
*/
121+
default List<Consumer<ValuesSourceRegistry>> getBareAggregatorRegistrar() {
122+
return emptyList();
123+
}
117124
/**
118125
* The new {@link PipelineAggregator}s added by this plugin.
119126
*/

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ private void registerAggregations(List<SearchPlugin> plugins) {
458458
registerAggregation((new AggregationSpec(CompositeAggregationBuilder.NAME, CompositeAggregationBuilder::new,
459459
CompositeAggregationBuilder.PARSER).addResultReader(InternalComposite::new)));
460460
registerFromPlugin(plugins, SearchPlugin::getAggregations, this::registerAggregation);
461+
462+
// after aggs have been registered, see if there are any new VSTypes that need to be linked to core fields
463+
registerFromPlugin(plugins, SearchPlugin::getBareAggregatorRegistrar, this::registerBareAggregatorRegistrar);
461464
}
462465

463466
private void registerAggregation(AggregationSpec spec) {
@@ -478,6 +481,12 @@ private void registerAggregation(AggregationSpec spec) {
478481
}
479482
}
480483

484+
private void registerBareAggregatorRegistrar(Consumer<ValuesSourceRegistry> registrar) {
485+
if (registrar != null) {
486+
registrar.accept(this.valuesSourceRegistry);
487+
}
488+
}
489+
481490
private void registerPipelineAggregations(List<SearchPlugin> plugins) {
482491
registerPipelineAggregation(new PipelineAggregationSpec(
483492
DerivativePipelineAggregationBuilder.NAME,

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

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.elasticsearch.common.util.ArrayUtils;
2727
import org.elasticsearch.common.util.BigArrays;
2828
import org.elasticsearch.common.util.ObjectArray;
29-
import org.elasticsearch.index.fielddata.HistogramValue;
30-
import org.elasticsearch.index.fielddata.HistogramValues;
3129
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
3230
import org.elasticsearch.search.DocValueFormat;
3331
import org.elasticsearch.search.aggregations.Aggregator;
@@ -78,18 +76,8 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
7876
return LeafBucketCollector.NO_OP_COLLECTOR;
7977
}
8078
final BigArrays bigArrays = context.bigArrays();
81-
if (valuesSource instanceof ValuesSource.Histogram) {
82-
final HistogramValues values = ((ValuesSource.Histogram)valuesSource).getHistogramValues(ctx);
83-
return collectHistogramValues(values, bigArrays, sub);
84-
} else {
85-
final SortedNumericDoubleValues values = ((ValuesSource.Numeric)valuesSource).doubleValues(ctx);
86-
return collectNumeric(values, bigArrays, sub);
87-
}
88-
89-
}
9079

91-
private LeafBucketCollector collectNumeric(final SortedNumericDoubleValues values,
92-
final BigArrays bigArrays, final LeafBucketCollector sub) {
80+
final SortedNumericDoubleValues values = ((ValuesSource.Numeric)valuesSource).doubleValues(ctx);
9381
return new LeafBucketCollectorBase(sub, values) {
9482
@Override
9583
public void collect(int doc, long bucket) throws IOException {
@@ -102,35 +90,21 @@ public void collect(int doc, long bucket) throws IOException {
10290
}
10391
}
10492
};
105-
}
10693

107-
private LeafBucketCollector collectHistogramValues(final HistogramValues values,
108-
final BigArrays bigArrays, final LeafBucketCollector sub) {
109-
return new LeafBucketCollectorBase(sub, values) {
110-
@Override
111-
public void collect(int doc, long bucket) throws IOException {
112-
DoubleHistogram state = getExistingOrNewHistogram(bigArrays, bucket);
113-
if (values.advanceExact(doc)) {
114-
final HistogramValue sketch = values.histogram();
115-
while (sketch.next()) {
116-
state.recordValueWithCount(sketch.value(), sketch.count());
117-
}
118-
}
119-
}
120-
};
12194
}
12295

12396
private DoubleHistogram getExistingOrNewHistogram(final BigArrays bigArrays, long bucket) {
12497
states = bigArrays.grow(states, bucket + 1);
12598
DoubleHistogram state = states.get(bucket);
12699
if (state == null) {
127100
state = new DoubleHistogram(numberOfSignificantValueDigits);
128-
// Set the histogram to autosize so it can resize itself as
129-
// the data range increases. Resize operations should be
130-
// rare as the histogram buckets are exponential (on the top
131-
// level). In the future we could expose the range as an
132-
// option on the request so the histogram can be fixed at
133-
// initialisation and doesn't need resizing.
101+
/* Set the histogram to autosize so it can resize itself as
102+
the data range increases. Resize operations should be
103+
rare as the histogram buckets are exponential (on the top
104+
level). In the future we could expose the range as an
105+
option on the request so the histogram can be fixed at
106+
initialisation and doesn't need resizing.
107+
*/
134108
state.setAutoResize(true);
135109
states.set(bucket, state);
136110
}

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

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import org.elasticsearch.common.util.ArrayUtils;
2626
import org.elasticsearch.common.util.BigArrays;
2727
import org.elasticsearch.common.util.ObjectArray;
28-
import org.elasticsearch.index.fielddata.HistogramValue;
29-
import org.elasticsearch.index.fielddata.HistogramValues;
3028
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
3129
import org.elasticsearch.search.DocValueFormat;
3230
import org.elasticsearch.search.aggregations.Aggregator;
@@ -77,18 +75,7 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
7775
return LeafBucketCollector.NO_OP_COLLECTOR;
7876
}
7977
final BigArrays bigArrays = context.bigArrays();
80-
if (valuesSource instanceof ValuesSource.Histogram) {
81-
final HistogramValues values = ((ValuesSource.Histogram)valuesSource).getHistogramValues(ctx);
82-
return collectHistogramValues(values, bigArrays, sub);
83-
} else {
84-
final SortedNumericDoubleValues values = ((ValuesSource.Numeric)valuesSource).doubleValues(ctx);
85-
return collectNumeric(values, bigArrays, sub);
86-
}
87-
88-
}
89-
90-
private LeafBucketCollector collectNumeric(final SortedNumericDoubleValues values,
91-
final BigArrays bigArrays, final LeafBucketCollector sub) {
78+
final SortedNumericDoubleValues values = ((ValuesSource.Numeric)valuesSource).doubleValues(ctx);
9279
return new LeafBucketCollectorBase(sub, values) {
9380
@Override
9481
public void collect(int doc, long bucket) throws IOException {
@@ -103,22 +90,6 @@ public void collect(int doc, long bucket) throws IOException {
10390
};
10491
}
10592

106-
private LeafBucketCollector collectHistogramValues(final HistogramValues values,
107-
final BigArrays bigArrays, final LeafBucketCollector sub) {
108-
return new LeafBucketCollectorBase(sub, values) {
109-
@Override
110-
public void collect(int doc, long bucket) throws IOException {
111-
TDigestState state = getExistingOrNewHistogram(bigArrays, bucket);
112-
if (values.advanceExact(doc)) {
113-
final HistogramValue sketch = values.histogram();
114-
while(sketch.next()) {
115-
state.add(sketch.value(), sketch.count());
116-
}
117-
}
118-
}
119-
};
120-
}
121-
12293
private TDigestState getExistingOrNewHistogram(final BigArrays bigArrays, long bucket) {
12394
states = bigArrays.grow(states, bucket + 1);
12495
TDigestState state = states.get(bucket);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
public class InternalHDRPercentileRanks extends AbstractInternalHDRPercentiles implements PercentileRanks {
3232
public static final String NAME = "hdr_percentile_ranks";
3333

34-
InternalHDRPercentileRanks(String name, double[] cdfValues, DoubleHistogram state, boolean keyed, DocValueFormat formatter,
35-
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
public InternalHDRPercentileRanks(String name, double[] cdfValues, DoubleHistogram state, boolean keyed, DocValueFormat formatter,
35+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
3636
super(name, cdfValues, state, keyed, formatter, pipelineAggregators, metaData);
3737
}
3838

@@ -74,7 +74,7 @@ protected AbstractInternalHDRPercentiles createReduced(String name, double[] key
7474
return new InternalHDRPercentileRanks(name, keys, merged, keyed, format, pipelineAggregators, metaData);
7575
}
7676

77-
static double percentileRank(DoubleHistogram state, double value) {
77+
public static double percentileRank(DoubleHistogram state, double value) {
7878
if (state.getTotalCount() == 0) {
7979
return Double.NaN;
8080
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
public class InternalHDRPercentiles extends AbstractInternalHDRPercentiles implements Percentiles {
3232
public static final String NAME = "hdr_percentiles";
3333

34-
InternalHDRPercentiles(String name, double[] percents, DoubleHistogram state, boolean keyed, DocValueFormat formatter,
35-
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
34+
public InternalHDRPercentiles(String name, double[] percents, DoubleHistogram state, boolean keyed, DocValueFormat formatter,
35+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
3636
super(name, percents, state, keyed, formatter, pipelineAggregators, metaData);
3737
}
3838

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
public class InternalTDigestPercentileRanks extends AbstractInternalTDigestPercentiles implements PercentileRanks {
3131
public static final String NAME = "tdigest_percentile_ranks";
3232

33-
InternalTDigestPercentileRanks(String name, double[] cdfValues, TDigestState state, boolean keyed, DocValueFormat formatter,
34-
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
33+
public InternalTDigestPercentileRanks(String name, double[] cdfValues, TDigestState state, boolean keyed, DocValueFormat formatter,
34+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
3535
super(name, cdfValues, state, keyed, formatter, pipelineAggregators, metaData);
3636
}
3737

@@ -73,7 +73,7 @@ protected AbstractInternalTDigestPercentiles createReduced(String name, double[]
7373
return new InternalTDigestPercentileRanks(name, keys, merged, keyed, format, pipelineAggregators, metaData);
7474
}
7575

76-
static double percentileRank(TDigestState state, double value) {
76+
public static double percentileRank(TDigestState state, double value) {
7777
double percentileRank = state.cdf(value);
7878
if (percentileRank < 0) {
7979
percentileRank = 0;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
public class InternalTDigestPercentiles extends AbstractInternalTDigestPercentiles implements Percentiles {
3131
public static final String NAME = "tdigest_percentiles";
3232

33-
InternalTDigestPercentiles(String name, double[] percents, TDigestState state, boolean keyed, DocValueFormat formatter,
34-
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
33+
public InternalTDigestPercentiles(String name, double[] percents, TDigestState state, boolean keyed, DocValueFormat formatter,
34+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
3535
super(name, percents, state, keyed, formatter, pipelineAggregators, metaData);
3636
}
3737

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class PercentileRanksAggregatorFactory extends ValuesSourceAggregatorFactory {
4646

4747
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
4848
valuesSourceRegistry.register(PercentileRanksAggregationBuilder.NAME,
49-
List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.HISTOGRAM, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
49+
List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
5050
new PercentilesAggregatorSupplier() {
5151
@Override
5252
public Aggregator build(String name, ValuesSource valuesSource, SearchContext context, Aggregator parent,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PercentilesAggregatorFactory extends ValuesSourceAggregatorFactory {
5050

5151
static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) {
5252
valuesSourceRegistry.register(PercentilesAggregationBuilder.NAME,
53-
List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.HISTOGRAM, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
53+
List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN),
5454
new PercentilesAggregatorSupplier() {
5555
@Override
5656
public Aggregator build(String name, ValuesSource valuesSource, SearchContext context, Aggregator parent,

0 commit comments

Comments
 (0)