Skip to content

Commit c95dcea

Browse files
authored
Speed up LeafCollector#setScorer in TopHitsAggregator (elastic#138883)
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 e24330e commit c95dcea

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;
@@ -115,14 +116,20 @@ public LeafBucketCollector getLeafCollector(AggregationExecutionContext aggCtx,
115116
leafCollectors = new LongObjectPagedHashMap<>(1, bigArrays);
116117
return new LeafBucketCollectorBase(sub, null) {
117118

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

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

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

0 commit comments

Comments
 (0)