99package org .elasticsearch .search .aggregations .bucket .composite ;
1010
1111import org .apache .lucene .util .BytesRef ;
12+ import org .apache .lucene .util .PriorityQueue ;
1213import org .elasticsearch .Version ;
1314import org .elasticsearch .common .io .stream .StreamInput ;
1415import org .elasticsearch .common .io .stream .StreamOutput ;
2930import java .util .List ;
3031import java .util .Map ;
3132import java .util .Objects ;
32- import java .util .PriorityQueue ;
3333import java .util .Set ;
3434
3535public class InternalComposite
@@ -149,7 +149,12 @@ int[] getReverseMuls() {
149149
150150 @ Override
151151 public InternalAggregation reduce (List <InternalAggregation > aggregations , ReduceContext reduceContext ) {
152- PriorityQueue <BucketIterator > pq = new PriorityQueue <>(aggregations .size ());
152+ PriorityQueue <BucketIterator > pq = new PriorityQueue <>(aggregations .size ()) {
153+ @ Override
154+ protected boolean lessThan (BucketIterator a , BucketIterator b ) {
155+ return a .compareTo (b ) < 0 ;
156+ }
157+ };
153158 boolean earlyTerminated = false ;
154159 for (InternalAggregation agg : aggregations ) {
155160 InternalComposite sortedAgg = (InternalComposite ) agg ;
@@ -163,7 +168,7 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Reduce
163168 List <InternalBucket > buckets = new ArrayList <>();
164169 List <InternalBucket > result = new ArrayList <>();
165170 while (pq .size () > 0 ) {
166- BucketIterator bucketIt = pq .poll ();
171+ BucketIterator bucketIt = pq .top ();
167172 if (lastBucket != null && bucketIt .current .compareKey (lastBucket ) != 0 ) {
168173 InternalBucket reduceBucket = reduceBucket (buckets , reduceContext );
169174 buckets .clear ();
@@ -175,7 +180,9 @@ public InternalAggregation reduce(List<InternalAggregation> aggregations, Reduce
175180 lastBucket = bucketIt .current ;
176181 buckets .add (bucketIt .current );
177182 if (bucketIt .next () != null ) {
178- pq .add (bucketIt );
183+ pq .updateTop ();
184+ } else {
185+ pq .pop ();
179186 }
180187 }
181188 if (buckets .size () > 0 ) {
0 commit comments