|
| 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.metrics; |
| 21 | + |
| 22 | +import org.elasticsearch.index.query.QueryShardContext; |
| 23 | +import org.elasticsearch.search.DocValueFormat; |
| 24 | +import org.elasticsearch.search.aggregations.AggregationExecutionException; |
| 25 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 26 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 27 | +import org.elasticsearch.search.aggregations.AggregatorFactory; |
| 28 | +import org.elasticsearch.search.aggregations.metrics.PercentilesAggregationBuilder.PercentilesConfig; |
| 29 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 30 | +import org.elasticsearch.search.aggregations.support.AggregatorSupplier; |
| 31 | +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; |
| 32 | +import org.elasticsearch.search.aggregations.support.ValuesSource; |
| 33 | +import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; |
| 34 | +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; |
| 35 | +import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; |
| 36 | +import org.elasticsearch.search.internal.SearchContext; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * This factory is used to generate both TDigest and HDRHisto aggregators, depending |
| 44 | + * on the selected method |
| 45 | + */ |
| 46 | +class PercentilesAggregatorFactory extends ValuesSourceAggregatorFactory { |
| 47 | + |
| 48 | + private final double[] percents; |
| 49 | + private final PercentilesConfig percentilesConfig; |
| 50 | + private final boolean keyed; |
| 51 | + |
| 52 | + static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { |
| 53 | + valuesSourceRegistry.register(PercentilesAggregationBuilder.NAME, |
| 54 | + List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.HISTOGRAM), |
| 55 | + new PercentilesAggregatorSupplier() { |
| 56 | + @Override |
| 57 | + public Aggregator build(String name, ValuesSource valuesSource, SearchContext context, Aggregator parent, |
| 58 | + double[] percents, PercentilesConfig percentilesConfig, boolean keyed, DocValueFormat formatter, |
| 59 | + List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { |
| 60 | + |
| 61 | + if (percentilesConfig.getMethod().equals(PercentilesMethod.TDIGEST)) { |
| 62 | + double compression = ((PercentilesConfig.TDigestConfig)percentilesConfig).getCompression(); |
| 63 | + return new TDigestPercentilesAggregator(name, valuesSource, context, parent, percents, compression, keyed, |
| 64 | + formatter, pipelineAggregators, metaData); |
| 65 | + } else if (percentilesConfig.getMethod().equals(PercentilesMethod.HDR)) { |
| 66 | + int numSigFig = ((PercentilesConfig.HdrHistoConfig)percentilesConfig).getNumberOfSignificantValueDigits(); |
| 67 | + return new HDRPercentilesAggregator(name, valuesSource, context, parent, percents, numSigFig, keyed, |
| 68 | + formatter, pipelineAggregators, metaData); |
| 69 | + } |
| 70 | + |
| 71 | + // This should already have thrown but just in case |
| 72 | + throw new IllegalStateException("Unknown percentiles method: [" + percentilesConfig.getMethod().toString() + "]"); |
| 73 | + } |
| 74 | + } |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + PercentilesAggregatorFactory(String name, ValuesSourceConfig config, double[] percents, |
| 79 | + PercentilesConfig percentilesConfig, boolean keyed, QueryShardContext queryShardContext, |
| 80 | + AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder, |
| 81 | + Map<String, Object> metaData) throws IOException { |
| 82 | + super(name, config, queryShardContext, parent, subFactoriesBuilder, metaData); |
| 83 | + this.percents = percents; |
| 84 | + this.percentilesConfig = percentilesConfig; |
| 85 | + this.keyed = keyed; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected Aggregator createUnmapped(SearchContext searchContext, |
| 90 | + Aggregator parent, |
| 91 | + List<PipelineAggregator> pipelineAggregators, |
| 92 | + Map<String, Object> metaData) throws IOException { |
| 93 | + if (percentilesConfig.getMethod().equals(PercentilesMethod.TDIGEST)) { |
| 94 | + double compression = ((PercentilesConfig.TDigestConfig)percentilesConfig).getCompression(); |
| 95 | + return new TDigestPercentilesAggregator(name, null, searchContext, parent, percents, compression, keyed, config.format(), |
| 96 | + pipelineAggregators, metaData); |
| 97 | + } else if (percentilesConfig.getMethod().equals(PercentilesMethod.HDR)) { |
| 98 | + int numSigFig = ((PercentilesConfig.HdrHistoConfig)percentilesConfig).getNumberOfSignificantValueDigits(); |
| 99 | + return new HDRPercentilesAggregator(name, null, searchContext, parent, percents, numSigFig, keyed, |
| 100 | + config.format(), pipelineAggregators, metaData); |
| 101 | + } |
| 102 | + |
| 103 | + // This should already have thrown but just in case |
| 104 | + throw new IllegalStateException("Unknown percentiles method: [" + percentilesConfig.getMethod().toString() + "]"); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected Aggregator doCreateInternal(ValuesSource valuesSource, |
| 109 | + SearchContext searchContext, |
| 110 | + Aggregator parent, |
| 111 | + boolean collectsFromSingleBucket, |
| 112 | + List<PipelineAggregator> pipelineAggregators, |
| 113 | + Map<String, Object> metaData) throws IOException { |
| 114 | + |
| 115 | + AggregatorSupplier aggregatorSupplier = ValuesSourceRegistry.getInstance().getAggregator(config.valueSourceType(), |
| 116 | + PercentilesAggregationBuilder.NAME); |
| 117 | + |
| 118 | + if (aggregatorSupplier instanceof PercentilesAggregatorSupplier == false) { |
| 119 | + throw new AggregationExecutionException("Registry miss-match - expected PercentilesAggregatorSupplier, found [" + |
| 120 | + aggregatorSupplier.getClass().toString() + "]"); |
| 121 | + } |
| 122 | + PercentilesAggregatorSupplier percentilesAggregatorSupplier = (PercentilesAggregatorSupplier) aggregatorSupplier; |
| 123 | + return percentilesAggregatorSupplier.build(name, valuesSource, searchContext, parent, percents, percentilesConfig, keyed, |
| 124 | + config.format(), pipelineAggregators, metaData); |
| 125 | + } |
| 126 | +} |
0 commit comments