From 64de7ab2a93f4bae08896d2a26729e157447a8db Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 29 Nov 2024 10:41:31 +0100 Subject: [PATCH] Don't use bulk scoring if the filter leads iteration. --- .../lucene/search/BooleanScorerSupplier.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java index 7732445e8cd..3c3fd8c790d 100644 --- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java +++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java @@ -308,7 +308,23 @@ final class BooleanScorerSupplier extends ScorerSupplier { || minShouldMatch > 1) { return null; } - long cost = cost(); + + long filterCost = Long.MAX_VALUE; + for (ScorerSupplier supplier : subs.get(Occur.FILTER)) { + filterCost = Math.min(filterCost, supplier.cost()); + } + + long shouldCost = 0; + for (ScorerSupplier supplier : subs.get(Occur.SHOULD)) { + shouldCost += supplier.cost(); + } + + if (filterCost < shouldCost) { + // Don't do bulk scoring if the filter leads iteration. + return null; + } + + long cost = Math.min(shouldCost, filterCost); List optionalScorers = new ArrayList<>(); for (ScorerSupplier ss : subs.get(Occur.SHOULD)) { optionalScorers.add(ss.get(cost));