|
| 1 | +/* |
| 2 | + * Licensed to ElasticSearch and Shay Banon under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. ElasticSearch licenses this |
| 6 | + * file to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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.percentile; |
| 21 | + |
| 22 | +import org.elasticsearch.common.util.BigArrays; |
| 23 | +import org.elasticsearch.common.util.ObjectArray; |
| 24 | +import org.elasticsearch.index.fielddata.DoubleValues; |
| 25 | +import org.elasticsearch.search.aggregations.Aggregator; |
| 26 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 27 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 28 | +import org.elasticsearch.search.aggregations.metrics.percentile.providers.FrugalProvider; |
| 29 | +import org.elasticsearch.search.aggregations.metrics.percentile.providers.PercentileProvider; |
| 30 | +import org.elasticsearch.search.aggregations.metrics.percentile.providers.QDigestProvider; |
| 31 | +import org.elasticsearch.search.aggregations.metrics.percentile.providers.TDigestProvider; |
| 32 | +import org.elasticsearch.search.aggregations.support.AggregationContext; |
| 33 | +import org.elasticsearch.search.aggregations.support.ValueSourceAggregatorFactory; |
| 34 | +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; |
| 35 | +import org.elasticsearch.search.aggregations.support.numeric.NumericValuesSource; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + */ |
| 42 | +public class PercentileAggregator extends Aggregator { |
| 43 | + |
| 44 | + private final NumericValuesSource valuesSource; |
| 45 | + |
| 46 | + private ObjectArray<PercentileProvider> percentiles; |
| 47 | + private PercentileProvider.Factory percentileFactory; |
| 48 | + |
| 49 | + |
| 50 | + public PercentileAggregator(String name, long estimatedBucketsCount, NumericValuesSource valuesSource, AggregationContext context, Aggregator parent, PercentileProvider.Factory percentileFactory) { |
| 51 | + super(name, BucketAggregationMode.MULTI_BUCKETS, AggregatorFactories.EMPTY, estimatedBucketsCount, context, parent); |
| 52 | + this.valuesSource = valuesSource; |
| 53 | + this.percentileFactory = percentileFactory; |
| 54 | + if (valuesSource != null) { |
| 55 | + final long initialSize = estimatedBucketsCount < 2 ? 1 : estimatedBucketsCount; |
| 56 | + percentiles = BigArrays.newObjectArray(initialSize); |
| 57 | + for (long i = 0; i < initialSize; ++i) { |
| 58 | + percentiles.set(i, this.percentileFactory.create()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean shouldCollect() { |
| 65 | + return valuesSource != null; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void collect(int doc, long owningBucketOrdinal) throws IOException { |
| 70 | + assert valuesSource != null : "if value source is null, collect should never be called"; |
| 71 | + |
| 72 | + DoubleValues values = valuesSource.doubleValues(); |
| 73 | + if (values == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (owningBucketOrdinal >= percentiles.size()) { |
| 78 | + long from = percentiles.size(); |
| 79 | + percentiles = BigArrays.grow(percentiles, owningBucketOrdinal + 1); |
| 80 | + for (long i = from; i < percentiles.size(); ++i) { |
| 81 | + percentiles.set(i, this.percentileFactory.create()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + final int valueCount = values.setDocument(doc); |
| 86 | + PercentileProvider percentile = percentiles.get(owningBucketOrdinal); |
| 87 | + for (int i = 0; i < valueCount; i++) { |
| 88 | + percentile.offer(values.nextValue()); |
| 89 | + } |
| 90 | + |
| 91 | + percentiles.set(owningBucketOrdinal, percentile); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public InternalAggregation buildAggregation(long owningBucketOrdinal) { |
| 96 | + if (valuesSource == null || owningBucketOrdinal >= percentiles.size()) { |
| 97 | + return new InternalPercentile(name, percentileFactory.create()); |
| 98 | + } |
| 99 | + return new InternalPercentile(name, percentiles.get(owningBucketOrdinal)); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public InternalAggregation buildEmptyAggregation() { |
| 104 | + return new InternalPercentile(name, percentileFactory.create()); |
| 105 | + } |
| 106 | + |
| 107 | + public static class Factory extends ValueSourceAggregatorFactory.LeafOnly<NumericValuesSource> { |
| 108 | + |
| 109 | + private final PercentileProvider.Factory percentileFactory; |
| 110 | + |
| 111 | + public Factory(String name, String type, ValuesSourceConfig<NumericValuesSource> valuesSourceConfig, InternalPercentile.EXECUTION_HINT execution_hint, double[] percentiles) { |
| 112 | + super(name, type, valuesSourceConfig); |
| 113 | + |
| 114 | + if (execution_hint == InternalPercentile.EXECUTION_HINT.QDIGEST) { |
| 115 | + this.percentileFactory = new QDigestProvider.Factory(percentiles); |
| 116 | + } else if (execution_hint == InternalPercentile.EXECUTION_HINT.FRUGAL) { |
| 117 | + this.percentileFactory = new FrugalProvider.Factory(percentiles); |
| 118 | + } else if (execution_hint == InternalPercentile.EXECUTION_HINT.TDIGEST) { |
| 119 | + this.percentileFactory = new TDigestProvider.Factory(percentiles); |
| 120 | + } else { |
| 121 | + this.percentileFactory = new TDigestProvider.Factory(percentiles); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + protected Aggregator createUnmapped(AggregationContext aggregationContext, Aggregator parent) { |
| 127 | + return new PercentileAggregator(name, 0, null, aggregationContext, parent, percentileFactory); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected Aggregator create(NumericValuesSource valuesSource, long expectedBucketsCount, AggregationContext aggregationContext, Aggregator parent) { |
| 132 | + return new PercentileAggregator(name, expectedBucketsCount, valuesSource, aggregationContext, parent, percentileFactory); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments