|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.search.aggregations.bucket.terms; |
| 10 | + |
| 11 | +import org.apache.lucene.util.ArrayUtil; |
| 12 | +import org.apache.lucene.util.PriorityQueue; |
| 13 | +import org.opensearch.search.aggregations.BucketOrder; |
| 14 | +import org.opensearch.search.aggregations.InternalMultiBucketAggregation; |
| 15 | +import org.opensearch.search.aggregations.bucket.LocalBucketCountThresholds; |
| 16 | +import org.opensearch.search.aggregations.bucket.terms.LongKeyedBucketOrds.BucketOrdsEnum; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.function.Supplier; |
| 23 | + |
| 24 | +import static org.opensearch.search.aggregations.InternalOrder.isKeyOrder; |
| 25 | + |
| 26 | +/** |
| 27 | + * Strategy for selecting top buckets from aggregation results. |
| 28 | + * |
| 29 | + */ |
| 30 | +enum BucketSelectionStrategy { |
| 31 | + PRIORITY_QUEUE { |
| 32 | + @Override |
| 33 | + public <B extends InternalMultiBucketAggregation.InternalBucket> SelectionResult<B> selectTopBuckets(SelectionInput<B> input) |
| 34 | + throws IOException { |
| 35 | + PriorityQueue<B> ordered = input.buildPriorityQueue.buildPriorityQueue(input.size); |
| 36 | + B spare = null; |
| 37 | + long otherDocCount = 0; |
| 38 | + |
| 39 | + while (input.ordsEnum.next()) { |
| 40 | + long docCount = input.bucketDocCountFunction.bucketDocCount(input.ordsEnum.ord()); |
| 41 | + otherDocCount += docCount; |
| 42 | + if (docCount < input.localBucketCountThresholds.getMinDocCount()) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (spare == null) { |
| 46 | + spare = input.emptyBucketBuilder.get(); |
| 47 | + } |
| 48 | + input.bucketUpdateFunction.updateBucket(spare, input.ordsEnum, docCount); |
| 49 | + spare = ordered.insertWithOverflow(spare); |
| 50 | + } |
| 51 | + |
| 52 | + B[] topBuckets = input.bucketArrayBuilder.buildBuckets(ordered.size()); |
| 53 | + if (isKeyOrder(input.order)) { |
| 54 | + for (int b = ordered.size() - 1; b >= 0; --b) { |
| 55 | + topBuckets[b] = ordered.pop(); |
| 56 | + otherDocCount -= topBuckets[b].getDocCount(); |
| 57 | + } |
| 58 | + } else { |
| 59 | + Iterator<B> itr = ordered.iterator(); |
| 60 | + for (int b = ordered.size() - 1; b >= 0; --b) { |
| 61 | + topBuckets[b] = itr.next(); |
| 62 | + otherDocCount -= topBuckets[b].getDocCount(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return new SelectionResult<>(topBuckets, otherDocCount, "priority_queue"); |
| 67 | + } |
| 68 | + }, |
| 69 | + |
| 70 | + QUICK_SELECT_OR_SELECT_ALL { |
| 71 | + @Override |
| 72 | + public <B extends InternalMultiBucketAggregation.InternalBucket> SelectionResult<B> selectTopBuckets(SelectionInput<B> input) |
| 73 | + throws IOException { |
| 74 | + B[] bucketsForOrd = input.bucketArrayBuilder.buildBuckets((int) input.bucketsInOrd); |
| 75 | + int validBucketCount = 0; |
| 76 | + long otherDocCount = 0; |
| 77 | + |
| 78 | + // Collect all valid buckets |
| 79 | + while (input.ordsEnum.next()) { |
| 80 | + long docCount = input.bucketDocCountFunction.bucketDocCount(input.ordsEnum.ord()); |
| 81 | + otherDocCount += docCount; |
| 82 | + if (docCount < input.localBucketCountThresholds.getMinDocCount()) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + B spare = input.emptyBucketBuilder.get(); |
| 87 | + input.bucketUpdateFunction.updateBucket(spare, input.ordsEnum, docCount); |
| 88 | + bucketsForOrd[validBucketCount++] = spare; |
| 89 | + } |
| 90 | + |
| 91 | + B[] topBuckets; |
| 92 | + String actualStrategy; |
| 93 | + if (validBucketCount > input.size) { |
| 94 | + ArrayUtil.select( |
| 95 | + bucketsForOrd, |
| 96 | + 0, |
| 97 | + validBucketCount, |
| 98 | + input.size, |
| 99 | + (b1, b2) -> input.partiallyBuiltBucketComparator.compare((InternalTerms.Bucket<?>) b1, (InternalTerms.Bucket<?>) b2) |
| 100 | + ); |
| 101 | + topBuckets = Arrays.copyOf(bucketsForOrd, input.size); |
| 102 | + for (int b = 0; b < input.size; b++) { |
| 103 | + otherDocCount -= topBuckets[b].getDocCount(); |
| 104 | + } |
| 105 | + actualStrategy = "quick_select"; |
| 106 | + } else { |
| 107 | + // Return all buckets (no selection needed) |
| 108 | + topBuckets = Arrays.copyOf(bucketsForOrd, validBucketCount); |
| 109 | + otherDocCount = 0L; |
| 110 | + actualStrategy = "select_all"; |
| 111 | + } |
| 112 | + |
| 113 | + return new SelectionResult<>(topBuckets, otherDocCount, actualStrategy); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + public static BucketSelectionStrategy determine( |
| 118 | + int size, |
| 119 | + long bucketsInOrd, |
| 120 | + BucketOrder order, |
| 121 | + Comparator<InternalTerms.Bucket<?>> partiallyBuiltBucketComparator, |
| 122 | + int factor |
| 123 | + ) { |
| 124 | + /* |
| 125 | + We select the strategy based on the following condition with configurable threshold factor: |
| 126 | + case 1: size is less than 20% of bucketsInOrd: PRIORITY_QUEUE |
| 127 | + case 2: size is greater than 20% of bucketsInOrd: QUICK_SELECT |
| 128 | + case 3: size == bucketsInOrd : return all buckets |
| 129 | + case 2 and 3 are encapsulated in QUICK_SELECT_OR_SELECT_ALL method. |
| 130 | +
|
| 131 | + Along with the above conditions, we also go with the original PRIORITY_QUEUE based approach |
| 132 | + if isKeyOrder or its significant term aggregation. |
| 133 | +
|
| 134 | + if factor is 0, always use PRIORITY_QUEUE strategy (since 0 < bucketsInOrd is always true). |
| 135 | + */ |
| 136 | + if (((long) size * factor < bucketsInOrd) || isKeyOrder(order) || partiallyBuiltBucketComparator == null) { |
| 137 | + return PRIORITY_QUEUE; |
| 138 | + } else { |
| 139 | + return QUICK_SELECT_OR_SELECT_ALL; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public abstract <B extends InternalMultiBucketAggregation.InternalBucket> SelectionResult<B> selectTopBuckets(SelectionInput<B> input) |
| 144 | + throws IOException; |
| 145 | + |
| 146 | + /** |
| 147 | + * Represents the inputs for strategy execution to select buckets |
| 148 | + */ |
| 149 | + public static class SelectionInput<B extends InternalMultiBucketAggregation.InternalBucket> { |
| 150 | + public final int size; |
| 151 | + public final long bucketsInOrd; |
| 152 | + public final BucketOrdsEnum ordsEnum; |
| 153 | + public final Supplier<B> emptyBucketBuilder; |
| 154 | + public final LocalBucketCountThresholds localBucketCountThresholds; |
| 155 | + public final int ordIdx; |
| 156 | + public final BucketOrder order; |
| 157 | + public final PriorityQueueBuilder<B> buildPriorityQueue; |
| 158 | + public final BucketArrayBuilder<B> bucketArrayBuilder; |
| 159 | + public final BucketUpdateFunction<B> bucketUpdateFunction; |
| 160 | + public final BucketDocCountFunction bucketDocCountFunction; |
| 161 | + public final Comparator<InternalTerms.Bucket<?>> partiallyBuiltBucketComparator; |
| 162 | + |
| 163 | + public SelectionInput( |
| 164 | + int size, |
| 165 | + long bucketsInOrd, |
| 166 | + BucketOrdsEnum ordsEnum, |
| 167 | + Supplier<B> emptyBucketBuilder, |
| 168 | + LocalBucketCountThresholds localBucketCountThresholds, |
| 169 | + int ordIdx, |
| 170 | + BucketOrder order, |
| 171 | + PriorityQueueBuilder<B> buildPriorityQueue, |
| 172 | + BucketArrayBuilder<B> bucketArrayBuilder, |
| 173 | + BucketUpdateFunction<B> bucketUpdateFunction, |
| 174 | + BucketDocCountFunction bucketDocCountFunction, |
| 175 | + Comparator<InternalTerms.Bucket<?>> partiallyBuiltBucketComparator |
| 176 | + ) { |
| 177 | + this.size = size; |
| 178 | + this.bucketsInOrd = bucketsInOrd; |
| 179 | + this.ordsEnum = ordsEnum; |
| 180 | + this.emptyBucketBuilder = emptyBucketBuilder; |
| 181 | + this.localBucketCountThresholds = localBucketCountThresholds; |
| 182 | + this.ordIdx = ordIdx; |
| 183 | + this.order = order; |
| 184 | + this.buildPriorityQueue = buildPriorityQueue; |
| 185 | + this.bucketArrayBuilder = bucketArrayBuilder; |
| 186 | + this.bucketUpdateFunction = bucketUpdateFunction; |
| 187 | + this.bucketDocCountFunction = bucketDocCountFunction; |
| 188 | + this.partiallyBuiltBucketComparator = partiallyBuiltBucketComparator; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Represents the results strategy execution to select buckets |
| 194 | + */ |
| 195 | + public static class SelectionResult<B extends InternalMultiBucketAggregation.InternalBucket> { |
| 196 | + public final B[] topBuckets; |
| 197 | + public final long otherDocCount; |
| 198 | + public final String actualStrategyUsed; |
| 199 | + |
| 200 | + public SelectionResult(B[] topBuckets, long otherDocCount, String actualStrategyUsed) { |
| 201 | + this.topBuckets = topBuckets; |
| 202 | + this.otherDocCount = otherDocCount; |
| 203 | + this.actualStrategyUsed = actualStrategyUsed; |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Interface for bucketDocCount method |
| 209 | + */ |
| 210 | + @FunctionalInterface |
| 211 | + public interface BucketDocCountFunction { |
| 212 | + long bucketDocCount(long ord) throws IOException; |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Interface for updateBucket method |
| 217 | + */ |
| 218 | + @FunctionalInterface |
| 219 | + public interface BucketUpdateFunction<B> { |
| 220 | + void updateBucket(B spare, BucketOrdsEnum ordsEnum, long docCount) throws IOException; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Interface for buildBuckets method |
| 225 | + */ |
| 226 | + @FunctionalInterface |
| 227 | + public interface BucketArrayBuilder<B> { |
| 228 | + B[] buildBuckets(int size); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Interface for buildPriorityQueue method |
| 233 | + */ |
| 234 | + @FunctionalInterface |
| 235 | + public interface PriorityQueueBuilder<B> { |
| 236 | + PriorityQueue<B> buildPriorityQueue(int size); |
| 237 | + } |
| 238 | +} |
0 commit comments