Skip to content

Commit 5f8255b

Browse files
committed
small refactor
Signed-off-by: bowenlan-amzn <bowenlan23@gmail.com>
1 parent 7aeee3d commit 5f8255b

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

server/src/main/java/org/opensearch/search/aggregations/bucket/FastFilterRewriteHelper.java

+19-24
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.lucene.search.ScoreMode;
2525
import org.apache.lucene.search.Weight;
2626
import org.apache.lucene.util.NumericUtils;
27-
import org.opensearch.common.CheckedBiFunction;
2827
import org.opensearch.common.Rounding;
2928
import org.opensearch.common.lucene.search.function.FunctionScoreQuery;
3029
import org.opensearch.index.mapper.DateFieldMapper;
@@ -262,17 +261,20 @@ public boolean isRewriteable(final Object parent, final int subAggLength) {
262261
}
263262

264263
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);
266266
if (filters != null) {
267267
logger.debug("Fast filter built for shard {}", context.indexShard().shardId());
268268
filtersBuiltAtShardLevel = true;
269269
}
270270
}
271271

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 {
274273
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+
}
276278
}
277279
}
278280

@@ -283,8 +285,9 @@ interface AggregationType {
283285

284286
boolean isRewriteable(Object parent, int subAggLength);
285287

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;
288291

289292
default int getSize() {
290293
return Integer.MAX_VALUE;
@@ -322,19 +325,15 @@ public boolean isRewriteable(Object parent, int subAggLength) {
322325
}
323326

324327
@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());
328330
logger.debug("Bounds are {} for shard {}", bounds, context.indexShard().shardId());
329331
return buildFastFilter(context, bounds);
330332
}
331333

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());
338337
logger.debug("Bounds are {} for shard {} segment {}", bounds, context.indexShard().shardId(), leaf.ord);
339338
return buildFastFilter(context, bounds);
340339
}
@@ -411,6 +410,8 @@ public static long getBucketOrd(long bucketOrd) {
411410

412411
/**
413412
* 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
414415
*
415416
* @param incrementDocCount takes in the bucket key value and the bucket count
416417
*/
@@ -446,15 +447,9 @@ public static boolean tryFastFilterAggregation(
446447
fastFilterContext.context.indexShard().shardId(),
447448
ctx.ord
448449
);
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;
456452
if (filters == null) {
457-
458453
return false;
459454
}
460455
}

0 commit comments

Comments
 (0)