From eee0e135abe50c58c170b35b79c839c877b7a5f7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 28 Apr 2025 18:56:24 +0000 Subject: [PATCH] Fix redundant loading of global ordinals (#18059) Signed-off-by: Sandesh Kumar (cherry picked from commit 1d998f112333d6948ab2d86745235c316c5aedc9) Signed-off-by: github-actions[bot] --- .../GlobalOrdinalsStringTermsAggregator.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java index d8ec9feaf44b4..130444a3d673f 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java @@ -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 ordCountConsumer) - throws IOException { + boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, BiConsumer ordCountConsumer) throws IOException { if (weight == null) { // Weight not assigned - cannot use this optimization return false; @@ -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(); @@ -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; } @@ -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) ); } @@ -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); @@ -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); } @@ -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); @@ -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); + } } /**