|
20 | 20 | package org.elasticsearch.search.aggregations.bucket.terms;
|
21 | 21 |
|
22 | 22 | import org.apache.logging.log4j.LogManager;
|
| 23 | +import org.apache.lucene.index.LeafReaderContext; |
23 | 24 | import org.apache.lucene.search.IndexSearcher;
|
24 | 25 | import org.elasticsearch.common.ParseField;
|
25 | 26 | import org.elasticsearch.common.logging.DeprecationLogger;
|
@@ -133,7 +134,7 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, Aggregator pare
|
133 | 134 | if (valuesSource instanceof ValuesSource.Bytes.WithOrdinals == false) {
|
134 | 135 | execution = ExecutionMode.MAP;
|
135 | 136 | }
|
136 |
| - final long maxOrd = getMaxOrd(valuesSource, context.searcher()); |
| 137 | + final long maxOrd = getMaxOrd(context.searcher(), valuesSource, execution); |
137 | 138 | if (execution == null) {
|
138 | 139 | execution = ExecutionMode.GLOBAL_ORDINALS;
|
139 | 140 | }
|
@@ -207,13 +208,23 @@ static SubAggCollectionMode subAggCollectionMode(int expectedSize, long maxOrd)
|
207 | 208 | }
|
208 | 209 |
|
209 | 210 | /**
|
210 |
| - * Get the maximum global ordinal value for the provided {@link ValuesSource} or -1 |
| 211 | + * Get the maximum ordinal value for the provided {@link ValuesSource} or -1 |
211 | 212 | * if the values source is not an instance of {@link ValuesSource.Bytes.WithOrdinals}.
|
212 | 213 | */
|
213 |
| - static long getMaxOrd(ValuesSource source, IndexSearcher searcher) throws IOException { |
| 214 | + static long getMaxOrd(IndexSearcher searcher, ValuesSource source, ExecutionMode executionMode) throws IOException { |
214 | 215 | if (source instanceof ValuesSource.Bytes.WithOrdinals) {
|
215 | 216 | ValuesSource.Bytes.WithOrdinals valueSourceWithOrdinals = (ValuesSource.Bytes.WithOrdinals) source;
|
216 |
| - return valueSourceWithOrdinals.globalMaxOrd(searcher); |
| 217 | + if (executionMode == ExecutionMode.MAP) { |
| 218 | + // global ordinals are not requested so we don't load them |
| 219 | + // and return the biggest cardinality per segment instead. |
| 220 | + long maxOrd = -1; |
| 221 | + for (LeafReaderContext leaf : searcher.getIndexReader().leaves()) { |
| 222 | + maxOrd = Math.max(maxOrd, valueSourceWithOrdinals.ordinalsValues(leaf).getValueCount()); |
| 223 | + } |
| 224 | + return maxOrd; |
| 225 | + } else { |
| 226 | + return valueSourceWithOrdinals.globalMaxOrd(searcher); |
| 227 | + } |
217 | 228 | } else {
|
218 | 229 | return -1;
|
219 | 230 | }
|
@@ -258,7 +269,7 @@ Aggregator create(String name,
|
258 | 269 | List<PipelineAggregator> pipelineAggregators,
|
259 | 270 | Map<String, Object> metaData) throws IOException {
|
260 | 271 |
|
261 |
| - final long maxOrd = getMaxOrd(valuesSource, context.searcher()); |
| 272 | + final long maxOrd = getMaxOrd(context.searcher(), valuesSource, ExecutionMode.GLOBAL_ORDINALS); |
262 | 273 | assert maxOrd != -1;
|
263 | 274 | final double ratio = maxOrd / ((double) context.searcher().getIndexReader().numDocs());
|
264 | 275 |
|
|
0 commit comments