Skip to content

Commit 0aff5a3

Browse files
committed
Check self references in metric agg after last doc collection (#33593)
1 parent 44c7c4b commit 0aff5a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public ScoreMode scoreMode() {
6868
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
6969
final LeafBucketCollector sub) throws IOException {
7070
final ScriptedMetricAggContexts.MapScript leafMapScript = mapScript.newInstance(ctx);
71+
final int numDocs = ctx.reader().numDocs();
7172
return new LeafBucketCollectorBase(sub, leafMapScript) {
73+
private long docCount = 0;
74+
7275
@Override
7376
public void setScorer(Scorable scorer) throws IOException {
7477
leafMapScript.setScorer(scorer);
@@ -80,7 +83,11 @@ public void collect(int doc, long bucket) throws IOException {
8083

8184
leafMapScript.setDocument(doc);
8285
leafMapScript.execute();
83-
CollectionUtils.ensureNoSelfReferences(aggState, "Scripted metric aggs map script");
86+
docCount++;
87+
88+
if (docCount == numDocs) {
89+
CollectionUtils.ensureNoSelfReferences(aggState, "Scripted metric aggs map script");
90+
}
8491
}
8592
};
8693
}

0 commit comments

Comments
 (0)