Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions docs/changelog/138883.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 138883
summary: Speed up `LeafCollector#setScorer` in `TopHitsAggregator`
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -115,14 +116,20 @@ public LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx,
leafCollectors = new LongObjectPagedHashMap<>(1, bigArrays);
return new LeafBucketCollectorBase(sub, null) {

Scorable scorer;
// use the same Scorable for the leaf collectors
ResettableScorable scorer = null;

@Override
public void setScorer(Scorable scorer) throws IOException {
this.scorer = scorer;
super.setScorer(scorer);
for (Cursor<LeafCollector> leafCollector : leafCollectors) {
leafCollector.value.setScorer(scorer);
if (this.scorer != null) {
this.scorer.reset(scorer);
super.setScorer(scorer);
} else {
this.scorer = new ResettableScorable(scorer);
super.setScorer(scorer);
for (Cursor<LeafCollector> leafCollector : leafCollectors) {
leafCollector.value.setScorer(scorer);
}
}
}

Expand Down Expand Up @@ -295,4 +302,37 @@ public void collectDebugInfo(BiConsumer<String, Object> add) {
protected void doClose() {
Releasables.close(topDocsCollectors, leafCollectors);
}

private static class ResettableScorable extends Scorable {

private Scorable scorable;

private ResettableScorable(Scorable scorable) {
this.scorable = scorable;
}

private void reset(Scorable scorable) {
this.scorable = scorable;
}

@Override
public float score() throws IOException {
return scorable.score();
}

@Override
public float smoothingScore(int docId) throws IOException {
return scorable.smoothingScore(docId);
}

@Override
public void setMinCompetitiveScore(float minScore) throws IOException {
scorable.setMinCompetitiveScore(minScore);
}

@Override
public Collection<ChildScorable> getChildren() throws IOException {
return scorable.getChildren();
}
}
}