Skip to content

[Backport 3.0] [Bug-fix] Fix redundant loading of global ordinals #18112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,18 @@ public void setWeight(Weight weight) {
this.weight = weight;
}

SortedSetDocValues getGlobalOrds(LeafReaderContext ctx) throws IOException {
return valuesSource.globalOrdinalsValues(ctx);
}

/**
Read doc frequencies directly from indexed terms in the segment to skip iterating through individual documents
@param ctx The LeafReaderContext to collect terms from
@param globalOrds The SortedSetDocValues for the field's ordinals
@param ordCountConsumer A consumer to accept collected term frequencies
@return A LeafBucketCollector implementation with collection termination, since collection is complete
@throws IOException If an I/O error occurs during reading
*/
boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, SortedSetDocValues globalOrds, BiConsumer<Long, Integer> ordCountConsumer)
throws IOException {
boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, BiConsumer<Long, Integer> ordCountConsumer) throws IOException {
if (weight == null) {
// Weight not assigned - cannot use this optimization
return false;
Expand Down Expand Up @@ -205,6 +207,7 @@ boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, SortedSetDocValues

TermsEnum indexTermsEnum = segmentTerms.iterator();
BytesRef indexTerm = indexTermsEnum.next();
final SortedSetDocValues globalOrds = this.getGlobalOrds(ctx);
TermsEnum globalOrdinalTermsEnum = globalOrds.termsEnum();
BytesRef ordinalTerm = globalOrdinalTermsEnum.next();

Expand All @@ -229,7 +232,6 @@ boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, SortedSetDocValues

@Override
protected boolean tryPrecomputeAggregationForLeaf(LeafReaderContext ctx) throws IOException {
SortedSetDocValues globalOrds = valuesSource.globalOrdinalsValues(ctx);
if (tryStarTreePrecompute(ctx) == true) {
return true;
}
Expand All @@ -238,7 +240,6 @@ protected boolean tryPrecomputeAggregationForLeaf(LeafReaderContext ctx) throws
&& subAggregators.length == 0) {
return tryCollectFromTermFrequencies(
ctx,
globalOrds,
(ord, docCount) -> incrementBucketDocCount(collectionStrategy.globalOrdToBucketOrd(0, ord), docCount)
);
}
Expand All @@ -258,7 +259,7 @@ protected boolean tryStarTreePrecompute(LeafReaderContext ctx) throws IOExceptio

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
SortedSetDocValues globalOrds = valuesSource.globalOrdinalsValues(ctx);
SortedSetDocValues globalOrds = this.getGlobalOrds(ctx);
collectionStrategy.globalOrdsReady(globalOrds);

SortedDocValues singleValues = DocValues.unwrapSingleton(globalOrds);
Expand Down Expand Up @@ -510,14 +511,8 @@ protected boolean tryPrecomputeAggregationForLeaf(LeafReaderContext ctx) throws
if (mapping != null) {
mapSegmentCountsToGlobalCounts(mapping);
}
final SortedSetDocValues segmentOrds = valuesSource.ordinalsValues(ctx);
segmentDocCounts = context.bigArrays().grow(segmentDocCounts, 1 + segmentOrds.getValueCount());
mapping = valuesSource.globalOrdinalsMapping(ctx);
return tryCollectFromTermFrequencies(
ctx,
segmentOrds,
(ord, docCount) -> incrementBucketDocCount(mapping.applyAsLong(ord), docCount)
);
return tryCollectFromTermFrequencies(ctx, (ord, docCount) -> incrementBucketDocCount(mapping.applyAsLong(ord), docCount));
}
return tryStarTreePrecompute(ctx);
}
Expand All @@ -527,7 +522,7 @@ public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCol
if (mapping != null) {
mapSegmentCountsToGlobalCounts(mapping);
}
final SortedSetDocValues segmentOrds = valuesSource.ordinalsValues(ctx);
final SortedSetDocValues segmentOrds = this.getGlobalOrds(ctx);
segmentDocCounts = context.bigArrays().grow(segmentDocCounts, 1 + segmentOrds.getValueCount());
assert sub == LeafBucketCollector.NO_OP_COLLECTOR;
mapping = valuesSource.globalOrdinalsMapping(ctx);
Expand Down Expand Up @@ -592,6 +587,11 @@ private void mapSegmentCountsToGlobalCounts(LongUnaryOperator mapping) throws IO
incrementBucketDocCount(collectionStrategy.globalOrdToBucketOrd(0, globalOrd), inc);
}
}

@Override
SortedSetDocValues getGlobalOrds(LeafReaderContext ctx) throws IOException {
return valuesSource.ordinalsValues(ctx);
}
}

/**
Expand Down
Loading