@Override public void setNextReader(AtomicReaderContext reader) { if (segmentOrdinals != null && segmentOrdinals.getMaxOrd() != globalOrdinals.getMaxOrd()) { mapSegmentCountsToGlobalCounts(); } globalValues = valuesSource.globalBytesValues(); globalOrdinals = globalValues.ordinals(); BytesValues.WithOrdinals bytesValues = valuesSource.bytesValues(); segmentOrdinals = bytesValues.ordinals(); if (segmentOrdinals.getMaxOrd() != globalOrdinals.getMaxOrd()) { current = segmentDocCounts; } else { current = getDocCounts(); } }
@Override public void setNextReader(AtomicReaderContext context) throws IOException { values = indexFieldData.load(context).getBytesValues(parentType); if (values != null) { ordinals = values.ordinals(); final long maxOrd = ordinals.getMaxOrd(); if (parentIdsIndex == null) { parentIdsIndex = bigArrays.newLongArray(BigArrays.overSize(maxOrd), false); } else if (parentIdsIndex.size() < maxOrd) { parentIdsIndex = bigArrays.grow(parentIdsIndex, maxOrd); } parentIdsIndex.fill(0, maxOrd, -1L); } }
@Override public long getMaxOrd() { return inner.getMaxOrd(); }
@Override protected void doPostCollection() { if (segmentOrdinals.getMaxOrd() != globalOrdinals.getMaxOrd()) { mapSegmentCountsToGlobalCounts(); } }
@Override public InternalAggregation buildAggregation(long owningBucketOrdinal) { if (globalOrdinals == null) { // no context in this reader return buildEmptyAggregation(); } final int size; if (bucketCountThresholds.getMinDocCount() == 0) { // if minDocCount == 0 then we can end up with more buckets then maxBucketOrd() returns size = (int) Math.min(globalOrdinals.getMaxOrd(), bucketCountThresholds.getShardSize()); } else { size = (int) Math.min(maxBucketOrd(), bucketCountThresholds.getShardSize()); } BucketPriorityQueue ordered = new BucketPriorityQueue(size, order.comparator(this)); OrdBucket spare = new OrdBucket(-1, 0, null); for (long globalTermOrd = Ordinals.MIN_ORDINAL; globalTermOrd < globalOrdinals.getMaxOrd(); ++globalTermOrd) { if (includeExclude != null && !acceptedGlobalOrdinals.get(globalTermOrd)) { continue; } final long bucketOrd = getBucketOrd(globalTermOrd); final long bucketDocCount = bucketOrd < 0 ? 0 : bucketDocCount(bucketOrd); if (bucketCountThresholds.getMinDocCount() > 0 && bucketDocCount == 0) { continue; } spare.globalOrd = globalTermOrd; spare.bucketOrd = bucketOrd; spare.docCount = bucketDocCount; if (bucketCountThresholds.getShardMinDocCount() <= spare.docCount) { spare = (OrdBucket) ordered.insertWithOverflow(spare); if (spare == null) { spare = new OrdBucket(-1, 0, null); } } } // Get the top buckets final InternalTerms.Bucket[] list = new InternalTerms.Bucket[ordered.size()]; long survivingBucketOrds[] = new long[ordered.size()]; for (int i = ordered.size() - 1; i >= 0; --i) { final OrdBucket bucket = (OrdBucket) ordered.pop(); survivingBucketOrds[i] = bucket.bucketOrd; BytesRef scratch = new BytesRef(); copy(globalValues.getValueByOrd(bucket.globalOrd), scratch); list[i] = new StringTerms.Bucket(scratch, bucket.docCount, null); list[i].bucketOrd = bucket.bucketOrd; } // replay any deferred collections runDeferredCollections(survivingBucketOrds); // Now build the aggs for (int i = 0; i < list.length; i++) { Bucket bucket = list[i]; bucket.aggregations = bucket.docCount == 0 ? bucketEmptyAggregations() : bucketAggregations(bucket.bucketOrd); } return new StringTerms( name, order, bucketCountThresholds.getRequiredSize(), bucketCountThresholds.getMinDocCount(), Arrays.asList(list)); }