Skip to content

Commit 7892836

Browse files
authored
Speed up LeafCollector#setScorer in TopHitsAggregator (elastic#138883) (elastic#138886)
this commit introduce a wrapper for the Scorable that is shared by all the buckets so we only need to change it in one place when present.
1 parent 8fa2c74 commit 7892836

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

docs/changelog/138883.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 138883
2+
summary: Speed up `LeafCollector#setScorer` in `TopHitsAggregator`
3+
area: Search
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/search/aggregations/metrics/TopHitsAggregator.java

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
import java.io.IOException;
5252
import java.util.ArrayList;
53+
import java.util.Collection;
5354
import java.util.HashMap;
5455
import java.util.List;
5556
import java.util.Map;
@@ -114,14 +115,20 @@ public LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx,
114115
leafCollectors = new LongObjectPagedHashMap<>(1, bigArrays);
115116
return new LeafBucketCollectorBase(sub, null) {
116117

117-
Scorable scorer;
118+
// use the same Scorable for the leaf collectors
119+
ResettableScorable scorer = null;
118120

119121
@Override
120122
public void setScorer(Scorable scorer) throws IOException {
121-
this.scorer = scorer;
122-
super.setScorer(scorer);
123-
for (Cursor<LeafCollector> leafCollector : leafCollectors) {
124-
leafCollector.value.setScorer(scorer);
123+
if (this.scorer != null) {
124+
this.scorer.reset(scorer);
125+
super.setScorer(scorer);
126+
} else {
127+
this.scorer = new ResettableScorable(scorer);
128+
super.setScorer(scorer);
129+
for (Cursor<LeafCollector> leafCollector : leafCollectors) {
130+
leafCollector.value.setScorer(scorer);
131+
}
125132
}
126133
}
127134

@@ -294,4 +301,37 @@ public void collectDebugInfo(BiConsumer<String, Object> add) {
294301
protected void doClose() {
295302
Releasables.close(topDocsCollectors, leafCollectors);
296303
}
304+
305+
private static class ResettableScorable extends Scorable {
306+
307+
private Scorable scorable;
308+
309+
private ResettableScorable(Scorable scorable) {
310+
this.scorable = scorable;
311+
}
312+
313+
private void reset(Scorable scorable) {
314+
this.scorable = scorable;
315+
}
316+
317+
@Override
318+
public float score() throws IOException {
319+
return scorable.score();
320+
}
321+
322+
@Override
323+
public float smoothingScore(int docId) throws IOException {
324+
return scorable.smoothingScore(docId);
325+
}
326+
327+
@Override
328+
public void setMinCompetitiveScore(float minScore) throws IOException {
329+
scorable.setMinCompetitiveScore(minScore);
330+
}
331+
332+
@Override
333+
public Collection<ChildScorable> getChildren() throws IOException {
334+
return scorable.getChildren();
335+
}
336+
}
297337
}

0 commit comments

Comments
 (0)