5151
5252import java .io .IOException ;
5353import java .util .ArrayList ;
54+ import java .util .Arrays ;
55+ import java .util .Collections ;
5456import java .util .List ;
5557import java .util .Map ;
5658
@@ -116,34 +118,38 @@ protected SignificantStringTerms buildEmptySignificantTermsAggregation(long subs
116118 }
117119
118120 @ Override
119- public InternalAggregation convertRow (Map <String , Object []> shardResult , int row , SearchContext searchContext ) {
120- String termKey = (String ) searchContext .convertToComparable (shardResult .get (name )[row ]);
121- long docCount = 1 ;
122-
123- List <InternalAggregation > subAggs = new ArrayList <>();
124- for (Aggregator aggregator : subAggregators ) {
125- if (aggregator instanceof ShardResultConvertor convertor ) {
126- InternalAggregation subAgg = convertor .convertRow (shardResult , row , searchContext );
127- if (aggregator instanceof ValueCountAggregator ) {
128- docCount = ((InternalValueCount ) subAgg ).getValue ();
121+ public List <InternalAggregation > convert (Map <String , Object []> shardResult , SearchContext searchContext ) {
122+ int rowCount = shardResult .get (shardResult .keySet ().stream ().findFirst ().get ()).length ;
123+ List <StringTerms .Bucket > buckets = new ArrayList <>(rowCount );
124+ for (int row = 0 ; row < rowCount ; row ++) {
125+ String termKey = (String ) searchContext .convertToComparable (shardResult .get (name )[row ]);
126+ List <InternalAggregation > subAggs = new ArrayList <>();
127+ long docCount = 1 ;
128+ for (Aggregator aggregator : subAggregators ) {
129+ if (aggregator instanceof ShardResultConvertor convertor ) {
130+ InternalAggregation subAgg = convertor .convertRow (shardResult , row , searchContext );
131+ if (aggregator instanceof ValueCountAggregator ) {
132+ docCount = ((InternalValueCount ) subAgg ).getValue ();
133+ }
134+ subAggs .add (subAgg );
129135 }
130- subAggs .add (subAgg );
131136 }
132- }
133137
138+ buckets .add (new StringTerms .Bucket (
139+ new BytesRef (termKey ),
140+ docCount ,
141+ InternalAggregations .from (subAggs ),
142+ showTermDocCountError ,
143+ 0 ,
144+ format
145+ ));
146+ }
134147 BucketOrder reduceOrder = order ;
135148 if (isKeyOrder (order ) == false ) {
136149 reduceOrder = InternalOrder .key (true );
150+ buckets .sort (reduceOrder .comparator ());
137151 }
138- StringTerms .Bucket bucket = new StringTerms .Bucket (
139- new BytesRef (termKey ),
140- docCount ,
141- InternalAggregations .from (subAggs ),
142- showTermDocCountError ,
143- 0 ,
144- format
145- );
146- return new StringTerms (
152+ return List .of (new StringTerms (
147153 name ,
148154 reduceOrder ,
149155 order ,
@@ -152,10 +158,10 @@ public InternalAggregation convertRow(Map<String, Object[]> shardResult, int row
152158 bucketCountThresholds .getShardSize (),
153159 showTermDocCountError ,
154160 0 ,
155- List . of ( bucket ) ,
161+ buckets ,
156162 0 ,
157163 bucketCountThresholds
158- );
164+ )) ;
159165 }
160166
161167}
0 commit comments