diff --git a/CHANGELOG.md b/CHANGELOG.md index 875e912f9ea19..db706c7b6e641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Changed - Migrate BC libs to their FIPS counterparts ([#14912](https://github.com/opensearch-project/OpenSearch/pull/14912)) - Increase the floor segment size to 16MB ([#17699](https://github.com/opensearch-project/OpenSearch/pull/17699)) +- Unwrap singleton DocValues in global ordinal value source of composite histogram aggregation ([#17740](https://github.com/opensearch-project/OpenSearch/pull/17740)) - Unwrap singleton DocValues in date histogram aggregation. ([#17643](https://github.com/opensearch-project/OpenSearch/pull/17643)) - Introduce 512 byte limit to search and ingest pipeline IDs ([#17786](https://github.com/opensearch-project/OpenSearch/pull/17786)) diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/composite/GlobalOrdinalValuesSource.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/composite/GlobalOrdinalValuesSource.java index 227dce543cfe9..ad1116d842360 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/composite/GlobalOrdinalValuesSource.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/composite/GlobalOrdinalValuesSource.java @@ -32,8 +32,10 @@ package org.opensearch.search.aggregations.bucket.composite; +import org.apache.lucene.index.DocValues; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; @@ -171,6 +173,26 @@ LeafBucketCollector getLeafCollector(LeafReaderContext context, LeafBucketCollec if (lookup == null) { initLookup(dvs); } + + // unwrapSingleton() returns non-null only if the field is single-valued + final SortedDocValues singleton = DocValues.unwrapSingleton(dvs); + + // Direct ordinal access for single-valued fields + if (singleton != null) { + return new LeafBucketCollector() { + @Override + public void collect(int doc, long bucket) throws IOException { + if (singleton.advanceExact(doc)) { + currentValue = singleton.ordValue(); + next.collect(doc, bucket); + } else if (missingBucket) { + currentValue = -1; + next.collect(doc, bucket); + } + } + }; + } + return new LeafBucketCollector() { @Override public void collect(int doc, long bucket) throws IOException {