24
24
import org .apache .lucene .search .ScoreMode ;
25
25
import org .apache .lucene .search .Weight ;
26
26
import org .apache .lucene .util .NumericUtils ;
27
- import org .opensearch .common .CheckedBiFunction ;
28
27
import org .opensearch .common .Rounding ;
29
28
import org .opensearch .common .lucene .search .function .FunctionScoreQuery ;
30
29
import org .opensearch .index .mapper .DateFieldMapper ;
@@ -262,17 +261,20 @@ public boolean isRewriteable(final Object parent, final int subAggLength) {
262
261
}
263
262
264
263
public void buildFastFilter () throws IOException {
265
- this .filters = this .buildFastFilter (FastFilterRewriteHelper ::getDateHistoAggBounds );
264
+ assert filters == null : "Filters should only be built once, but they are already built" ;
265
+ this .filters = this .aggregationType .buildFastFilter (context );
266
266
if (filters != null ) {
267
267
logger .debug ("Fast filter built for shard {}" , context .indexShard ().shardId ());
268
268
filtersBuiltAtShardLevel = true ;
269
269
}
270
270
}
271
271
272
- // This method can also be used at segment level
273
- private Weight [] buildFastFilter (CheckedBiFunction <SearchContext , String , long [], IOException > getBounds ) throws IOException {
272
+ public void buildFastFilter (LeafReaderContext leaf ) throws IOException {
274
273
assert filters == null : "Filters should only be built once, but they are already built" ;
275
- return this .aggregationType .buildFastFilter (context , getBounds );
274
+ this .filters = this .aggregationType .buildFastFilter (leaf , context );
275
+ if (filters != null ) {
276
+ logger .debug ("Fast filter built for shard {} segment {}" , context .indexShard ().shardId (), leaf .ord );
277
+ }
276
278
}
277
279
}
278
280
@@ -283,8 +285,9 @@ interface AggregationType {
283
285
284
286
boolean isRewriteable (Object parent , int subAggLength );
285
287
286
- Weight [] buildFastFilter (SearchContext ctx , CheckedBiFunction <SearchContext , String , long [], IOException > getBounds )
287
- throws IOException ;
288
+ Weight [] buildFastFilter (SearchContext ctx ) throws IOException ;
289
+
290
+ Weight [] buildFastFilter (LeafReaderContext leaf , SearchContext ctx ) throws IOException ;
288
291
289
292
default int getSize () {
290
293
return Integer .MAX_VALUE ;
@@ -322,19 +325,15 @@ public boolean isRewriteable(Object parent, int subAggLength) {
322
325
}
323
326
324
327
@ Override
325
- public Weight [] buildFastFilter (SearchContext context , CheckedBiFunction <SearchContext , String , long [], IOException > getBounds )
326
- throws IOException {
327
- long [] bounds = getBounds .apply (context , fieldType .name ());
328
+ public Weight [] buildFastFilter (SearchContext context ) throws IOException {
329
+ long [] bounds = getDateHistoAggBounds (context , fieldType .name ());
328
330
logger .debug ("Bounds are {} for shard {}" , bounds , context .indexShard ().shardId ());
329
331
return buildFastFilter (context , bounds );
330
332
}
331
333
332
- private Weight [] buildFastFilterWithSegBounds (
333
- SearchContext context ,
334
- CheckedBiFunction <LeafReaderContext , String , long [], IOException > getBounds ,
335
- LeafReaderContext leaf
336
- ) throws IOException {
337
- long [] bounds = getBounds .apply (leaf , fieldType .name ());
334
+ @ Override
335
+ public Weight [] buildFastFilter (LeafReaderContext leaf , SearchContext context ) throws IOException {
336
+ long [] bounds = getSegmentBounds (leaf , fieldType .name ());
338
337
logger .debug ("Bounds are {} for shard {} segment {}" , bounds , context .indexShard ().shardId (), leaf .ord );
339
338
return buildFastFilter (context , bounds );
340
339
}
@@ -411,6 +410,8 @@ public static long getBucketOrd(long bucketOrd) {
411
410
412
411
/**
413
412
* Try to get the bucket doc counts from the fast filters for the aggregation
413
+ * <p>
414
+ * Usage: invoked at segment level — in getLeafCollector of aggregator
414
415
*
415
416
* @param incrementDocCount takes in the bucket key value and the bucket count
416
417
*/
@@ -446,15 +447,9 @@ public static boolean tryFastFilterAggregation(
446
447
fastFilterContext .context .indexShard ().shardId (),
447
448
ctx .ord
448
449
);
449
- if (fastFilterContext .aggregationType instanceof AbstractDateHistogramAggregationType ) {
450
- filters = ((AbstractDateHistogramAggregationType ) fastFilterContext .aggregationType ).buildFastFilterWithSegBounds (
451
- fastFilterContext .context ,
452
- FastFilterRewriteHelper ::getSegmentBounds ,
453
- ctx
454
- );
455
- }
450
+ fastFilterContext .buildFastFilter (ctx );
451
+ filters = fastFilterContext .filters ;
456
452
if (filters == null ) {
457
-
458
453
return false ;
459
454
}
460
455
}
0 commit comments