|
| 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; |
| 21 | + |
| 22 | +import org.apache.lucene.index.LeafReaderContext; |
| 23 | +import org.apache.lucene.search.CollectionTerminatedException; |
| 24 | +import org.apache.lucene.search.LeafCollector; |
| 25 | +import org.apache.lucene.search.MultiCollector; |
| 26 | +import org.apache.lucene.search.ScoreCachingWrappingScorer; |
| 27 | +import org.apache.lucene.search.Scorer; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +/** |
| 35 | + * A {@link BucketCollector} which allows running a bucket collection with several |
| 36 | + * {@link BucketCollector}s. It is similar to the {@link MultiCollector} except that the |
| 37 | + * {@link #wrap} method filters out the {@link BucketCollector#NO_OP_COLLECTOR}s and not |
| 38 | + * the null ones. |
| 39 | + */ |
| 40 | +public class MultiBucketCollector extends BucketCollector { |
| 41 | + |
| 42 | + /** See {@link #wrap(Iterable)}. */ |
| 43 | + public static BucketCollector wrap(BucketCollector... collectors) { |
| 44 | + return wrap(Arrays.asList(collectors)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Wraps a list of {@link BucketCollector}s with a {@link MultiBucketCollector}. This |
| 49 | + * method works as follows: |
| 50 | + * <ul> |
| 51 | + * <li>Filters out the {@link BucketCollector#NO_OP_COLLECTOR}s collectors, so they are not used |
| 52 | + * during search time. |
| 53 | + * <li>If the input contains 1 real collector, it is returned. |
| 54 | + * <li>Otherwise the method returns a {@link MultiBucketCollector} which wraps the |
| 55 | + * non-{@link BucketCollector#NO_OP_COLLECTOR} collectors. |
| 56 | + * </ul> |
| 57 | + */ |
| 58 | + public static BucketCollector wrap(Iterable<? extends BucketCollector> collectors) { |
| 59 | + // For the user's convenience, we allow NO_OP collectors to be passed. |
| 60 | + // However, to improve performance, these null collectors are found |
| 61 | + // and dropped from the array we save for actual collection time. |
| 62 | + int n = 0; |
| 63 | + for (BucketCollector c : collectors) { |
| 64 | + if (c != NO_OP_COLLECTOR) { |
| 65 | + n++; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + if (n == 0) { |
| 70 | + return NO_OP_COLLECTOR; |
| 71 | + } else if (n == 1) { |
| 72 | + // only 1 Collector - return it. |
| 73 | + BucketCollector col = null; |
| 74 | + for (BucketCollector c : collectors) { |
| 75 | + if (c != null) { |
| 76 | + col = c; |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + return col; |
| 81 | + } else { |
| 82 | + BucketCollector[] colls = new BucketCollector[n]; |
| 83 | + n = 0; |
| 84 | + for (BucketCollector c : collectors) { |
| 85 | + if (c != null) { |
| 86 | + colls[n++] = c; |
| 87 | + } |
| 88 | + } |
| 89 | + return new MultiBucketCollector(colls); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private final boolean cacheScores; |
| 94 | + private final BucketCollector[] collectors; |
| 95 | + |
| 96 | + private MultiBucketCollector(BucketCollector... collectors) { |
| 97 | + this.collectors = collectors; |
| 98 | + int numNeedsScores = 0; |
| 99 | + for (BucketCollector collector : collectors) { |
| 100 | + if (collector.needsScores()) { |
| 101 | + numNeedsScores += 1; |
| 102 | + } |
| 103 | + } |
| 104 | + this.cacheScores = numNeedsScores >= 2; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void preCollection() throws IOException { |
| 109 | + for (BucketCollector collector : collectors) { |
| 110 | + collector.preCollection(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void postCollection() throws IOException { |
| 116 | + for (BucketCollector collector : collectors) { |
| 117 | + collector.postCollection(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean needsScores() { |
| 123 | + for (BucketCollector collector : collectors) { |
| 124 | + if (collector.needsScores()) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public String toString() { |
| 133 | + return Arrays.toString(collectors); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public LeafBucketCollector getLeafCollector(LeafReaderContext context) throws IOException { |
| 138 | + final List<LeafBucketCollector> leafCollectors = new ArrayList<>(); |
| 139 | + for (BucketCollector collector : collectors) { |
| 140 | + final LeafBucketCollector leafCollector; |
| 141 | + try { |
| 142 | + leafCollector = collector.getLeafCollector(context); |
| 143 | + } catch (CollectionTerminatedException e) { |
| 144 | + // this leaf collector does not need this segment |
| 145 | + continue; |
| 146 | + } |
| 147 | + leafCollectors.add(leafCollector); |
| 148 | + } |
| 149 | + switch (leafCollectors.size()) { |
| 150 | + case 0: |
| 151 | + throw new CollectionTerminatedException(); |
| 152 | + case 1: |
| 153 | + return leafCollectors.get(0); |
| 154 | + default: |
| 155 | + return new MultiLeafBucketCollector(leafCollectors, cacheScores); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static class MultiLeafBucketCollector extends LeafBucketCollector { |
| 160 | + |
| 161 | + private final boolean cacheScores; |
| 162 | + private final LeafBucketCollector[] collectors; |
| 163 | + private int numCollectors; |
| 164 | + |
| 165 | + private MultiLeafBucketCollector(List<LeafBucketCollector> collectors, boolean cacheScores) { |
| 166 | + this.collectors = collectors.toArray(new LeafBucketCollector[collectors.size()]); |
| 167 | + this.cacheScores = cacheScores; |
| 168 | + this.numCollectors = this.collectors.length; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void setScorer(Scorer scorer) throws IOException { |
| 173 | + if (cacheScores) { |
| 174 | + scorer = new ScoreCachingWrappingScorer(scorer); |
| 175 | + } |
| 176 | + for (int i = 0; i < numCollectors; ++i) { |
| 177 | + final LeafCollector c = collectors[i]; |
| 178 | + c.setScorer(scorer); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private void removeCollector(int i) { |
| 183 | + System.arraycopy(collectors, i + 1, collectors, i, numCollectors - i - 1); |
| 184 | + --numCollectors; |
| 185 | + collectors[numCollectors] = null; |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public void collect(int doc, long bucket) throws IOException { |
| 190 | + final LeafBucketCollector[] collectors = this.collectors; |
| 191 | + int numCollectors = this.numCollectors; |
| 192 | + for (int i = 0; i < numCollectors; ) { |
| 193 | + final LeafBucketCollector collector = collectors[i]; |
| 194 | + try { |
| 195 | + collector.collect(doc, bucket); |
| 196 | + ++i; |
| 197 | + } catch (CollectionTerminatedException e) { |
| 198 | + removeCollector(i); |
| 199 | + numCollectors = this.numCollectors; |
| 200 | + if (numCollectors == 0) { |
| 201 | + throw new CollectionTerminatedException(); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments