51
51
import org .opensearch .common .util .LongHash ;
52
52
import org .opensearch .core .common .io .stream .StreamOutput ;
53
53
import org .opensearch .core .xcontent .XContentBuilder ;
54
+ import org .opensearch .index .codec .composite .CompositeIndexFieldInfo ;
55
+ import org .opensearch .index .compositeindex .datacube .startree .index .StarTreeValues ;
56
+ import org .opensearch .index .compositeindex .datacube .startree .utils .iterator .SortedNumericStarTreeValuesIterator ;
57
+ import org .opensearch .index .compositeindex .datacube .startree .utils .iterator .SortedSetStarTreeValuesIterator ;
54
58
import org .opensearch .index .mapper .DocCountFieldMapper ;
55
59
import org .opensearch .search .DocValueFormat ;
56
60
import org .opensearch .search .aggregations .AggregationExecutionException ;
63
67
import org .opensearch .search .aggregations .InternalOrder ;
64
68
import org .opensearch .search .aggregations .LeafBucketCollector ;
65
69
import org .opensearch .search .aggregations .LeafBucketCollectorBase ;
70
+ import org .opensearch .search .aggregations .StarTreeBucketCollector ;
71
+ import org .opensearch .search .aggregations .StarTreePreComputeCollector ;
66
72
import org .opensearch .search .aggregations .bucket .LocalBucketCountThresholds ;
67
73
import org .opensearch .search .aggregations .bucket .terms .SignificanceLookup .BackgroundFrequencyForBytes ;
68
74
import org .opensearch .search .aggregations .bucket .terms .heuristic .SignificanceHeuristic ;
69
75
import org .opensearch .search .aggregations .support .ValuesSource ;
70
76
import org .opensearch .search .internal .SearchContext ;
77
+ import org .opensearch .search .startree .StarTreeQueryHelper ;
78
+ import org .opensearch .search .startree .StarTreeTraversalUtil ;
79
+ import org .opensearch .search .startree .filter .DimensionFilter ;
71
80
72
81
import java .io .IOException ;
73
82
import java .util .Arrays ;
83
+ import java .util .List ;
74
84
import java .util .Map ;
75
85
import java .util .function .BiConsumer ;
76
86
import java .util .function .Function ;
85
95
*
86
96
* @opensearch.internal
87
97
*/
88
- public class GlobalOrdinalsStringTermsAggregator extends AbstractStringTermsAggregator {
98
+ public class GlobalOrdinalsStringTermsAggregator extends AbstractStringTermsAggregator implements StarTreePreComputeCollector {
89
99
protected final ResultStrategy <?, ?, ?> resultStrategy ;
90
100
protected final ValuesSource .Bytes .WithOrdinals valuesSource ;
91
101
92
102
private final LongPredicate acceptedGlobalOrdinals ;
93
103
private final long valueCount ;
94
- private final String fieldName ;
104
+ protected final String fieldName ;
95
105
private Weight weight ;
96
106
protected final CollectionStrategy collectionStrategy ;
97
107
private final SetOnce <SortedSetDocValues > dvs = new SetOnce <>();
98
108
protected int segmentsWithSingleValuedOrds = 0 ;
99
109
protected int segmentsWithMultiValuedOrds = 0 ;
110
+ LongUnaryOperator globalOperator ;
100
111
101
112
/**
102
113
* Lookup global ordinals
@@ -219,6 +230,9 @@ boolean tryCollectFromTermFrequencies(LeafReaderContext ctx, SortedSetDocValues
219
230
@ Override
220
231
protected boolean tryPrecomputeAggregationForLeaf (LeafReaderContext ctx ) throws IOException {
221
232
SortedSetDocValues globalOrds = valuesSource .globalOrdinalsValues (ctx );
233
+ if (tryStarTreePrecompute (ctx ) == true ) {
234
+ return true ;
235
+ }
222
236
if (collectionStrategy instanceof DenseGlobalOrds
223
237
&& this .resultStrategy instanceof StandardTermsResults
224
238
&& subAggregators .length == 0 ) {
@@ -231,6 +245,17 @@ protected boolean tryPrecomputeAggregationForLeaf(LeafReaderContext ctx) throws
231
245
return false ;
232
246
}
233
247
248
+ protected boolean tryStarTreePrecompute (LeafReaderContext ctx ) throws IOException {
249
+ CompositeIndexFieldInfo supportedStarTree = StarTreeQueryHelper .getSupportedStarTree (this .context .getQueryShardContext ());
250
+ if (supportedStarTree != null ) {
251
+ globalOperator = valuesSource .globalOrdinalsMapping (ctx );
252
+ StarTreeBucketCollector starTreeBucketCollector = getStarTreeBucketCollector (ctx , supportedStarTree , null );
253
+ StarTreeQueryHelper .preComputeBucketsWithStarTree (starTreeBucketCollector );
254
+ return true ;
255
+ }
256
+ return false ;
257
+ }
258
+
234
259
@ Override
235
260
public LeafBucketCollector getLeafCollector (LeafReaderContext ctx , LeafBucketCollector sub ) throws IOException {
236
261
SortedSetDocValues globalOrds = valuesSource .globalOrdinalsValues (ctx );
@@ -307,6 +332,56 @@ public void collect(int doc, long owningBucketOrd) throws IOException {
307
332
});
308
333
}
309
334
335
+ public StarTreeBucketCollector getStarTreeBucketCollector (
336
+ LeafReaderContext ctx ,
337
+ CompositeIndexFieldInfo starTree ,
338
+ StarTreeBucketCollector parent
339
+ ) throws IOException {
340
+ assert parent == null ;
341
+ StarTreeValues starTreeValues = StarTreeQueryHelper .getStarTreeValues (ctx , starTree );
342
+ SortedSetStarTreeValuesIterator valuesIterator = (SortedSetStarTreeValuesIterator ) starTreeValues .getDimensionValuesIterator (
343
+ fieldName
344
+ );
345
+ SortedNumericStarTreeValuesIterator docCountsIterator = StarTreeQueryHelper .getDocCountsIterator (starTreeValues , starTree );
346
+
347
+ return new StarTreeBucketCollector (
348
+ starTreeValues ,
349
+ StarTreeTraversalUtil .getStarTreeResult (
350
+ starTreeValues ,
351
+ StarTreeQueryHelper .mergeDimensionFilterIfNotExists (
352
+ context .getQueryShardContext ().getStarTreeQueryContext ().getBaseQueryStarTreeFilter (),
353
+ fieldName ,
354
+ List .of (DimensionFilter .MATCH_ALL_DEFAULT )
355
+ ),
356
+ context
357
+ )
358
+ ) {
359
+ @ Override
360
+ public void setSubCollectors () throws IOException {
361
+ for (Aggregator aggregator : subAggregators ) {
362
+ this .subCollectors .add (((StarTreePreComputeCollector ) aggregator ).getStarTreeBucketCollector (ctx , starTree , this ));
363
+ }
364
+ }
365
+
366
+ @ Override
367
+ public void collectStarTreeEntry (int starTreeEntry , long owningBucketOrd ) throws IOException {
368
+ if (valuesIterator .advanceExact (starTreeEntry ) == false ) {
369
+ return ;
370
+ }
371
+ for (int i = 0 , count = valuesIterator .docValueCount (); i < count ; i ++) {
372
+ long dimensionValue = valuesIterator .value ();
373
+ long ord = globalOperator .applyAsLong (dimensionValue );
374
+
375
+ if (docCountsIterator .advanceExact (starTreeEntry )) {
376
+ long metricValue = docCountsIterator .nextValue ();
377
+ long bucketOrd = collectionStrategy .globalOrdToBucketOrd (0 , ord );
378
+ collectStarTreeBucket (this , metricValue , bucketOrd , starTreeEntry );
379
+ }
380
+ }
381
+ }
382
+ };
383
+ }
384
+
310
385
@ Override
311
386
public InternalAggregation [] buildAggregations (long [] owningBucketOrds ) throws IOException {
312
387
return resultStrategy .buildAggregations (owningBucketOrds );
@@ -444,7 +519,7 @@ protected boolean tryPrecomputeAggregationForLeaf(LeafReaderContext ctx) throws
444
519
(ord , docCount ) -> incrementBucketDocCount (mapping .applyAsLong (ord ), docCount )
445
520
);
446
521
}
447
- return false ;
522
+ return tryStarTreePrecompute ( ctx ) ;
448
523
}
449
524
450
525
@ Override
0 commit comments