You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2026/10/22 00:02:50 UTC

(lucene) 05/07: Rewrite BooleanQuery to MatchNoDocsQuery when it has MUST_NOT clause (#16667)

This is an automated email from the ASF dual-hosted git repository.

HoustonPutman pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit ff8fe18c2232ec75ead1c1ca49bc53f60c7614d0
Author: Egor Potemkin <eg...@gmail.com>
AuthorDate: Wed Sep 30 18:03:44 2026 +0100

    Rewrite BooleanQuery to MatchNoDocsQuery when it has MUST_NOT clause (#16667)
    
    matching one of its MUST boost or constant score query clauses.
    
    (cherry picked from commit 5e537615b292d5af7cd38c7546842f76dc1fba66)
---
 lucene/CHANGES.txt                                 |  3 ++
 .../org/apache/lucene/search/BooleanQuery.java     | 18 ++++++++---
 .../apache/lucene/search/TestBooleanRewrites.java  | 36 ++++++++++++++++++++++
 3 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c76a5d61d4f..164d98c9c63 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -265,6 +265,9 @@ Optimizations
 
 * GITHUB#16669: Use intoBitSet to speed up ReqExclBulkScorer with dense exclusions. (Ke Wei)
 
+* GITHUB#16667: Rewrite BooleanQuery to MatchNoDocsQuery when it has MUST_NOT clause matching one of
+  its MUST boost or constant score query clauses. (Egor Potemkin)
+
 * GITHUB#16769: The merge mapping of the raw vectors keeps the hints the file was opened with,
   changing only how it is read. (Jim Ferenczi)
 
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
index 3ab0bf72038..d00c1087158 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
@@ -29,7 +29,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
-import java.util.function.Predicate;
 import org.apache.lucene.index.TermStates;
 import org.apache.lucene.search.BooleanClause.Occur;
 
@@ -365,9 +364,20 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause> {
     // Check whether some clauses are both required and excluded
     final Collection<Query> mustNotClauses = clauseSets.get(Occur.MUST_NOT);
     if (!mustNotClauses.isEmpty()) {
-      final Predicate<Query> p = clauseSets.get(Occur.MUST)::contains;
-      if (mustNotClauses.stream().anyMatch(p.or(clauseSets.get(Occur.FILTER)::contains))) {
-        return new MatchNoDocsQuery("FILTER or MUST clause also in MUST_NOT");
+      if (mustNotClauses.stream().anyMatch(clauseSets.get(Occur.FILTER)::contains)) {
+        return new MatchNoDocsQuery("FILTER clause also in MUST_NOT");
+      }
+      for (Query q : clauseSets.get(Occur.MUST)) {
+        Query unwrapped = q;
+        if (q instanceof BoostQuery boostQuery) {
+          unwrapped = boostQuery.getQuery();
+        }
+        if (unwrapped instanceof ConstantScoreQuery csq) {
+          unwrapped = csq.getQuery();
+        }
+        if (mustNotClauses.contains(unwrapped)) {
+          return new MatchNoDocsQuery("MUST clause also in MUST_NOT");
+        }
       }
       if (mustNotClauses.contains(MatchAllDocsQuery.INSTANCE)) {
         return new MatchNoDocsQuery("MUST_NOT clause is MatchAllDocsQuery");
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
index 01b646f58b0..d00c98d7c4f 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
@@ -292,6 +292,42 @@ public class TestBooleanRewrites extends LuceneTestCase {
             .build();
 
     assertEquals(MatchNoDocsQuery.INSTANCE, searcher.rewrite(bq2));
+
+    // Test Filter BoostQuery+ConstantScoreQuery with MustNot
+    BooleanQuery bq3 =
+        new BooleanQuery.Builder()
+            .add(
+                new BoostQuery(
+                    new BoostQuery(
+                        new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), 2),
+                    5),
+                Occur.FILTER)
+            // other terms
+            .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
+            .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
+            //
+            .add(new TermQuery(new Term("foo", "bar")), Occur.MUST_NOT)
+            .build();
+
+    assertEquals(MatchNoDocsQuery.INSTANCE, searcher.rewrite(bq3));
+
+    // Test Must BoostQuery+ConstantScoreQuery with MustNot
+    BooleanQuery bq4 =
+        new BooleanQuery.Builder()
+            .add(
+                new BoostQuery(
+                    new BoostQuery(
+                        new ConstantScoreQuery(new TermQuery(new Term("foo", "bar"))), 2),
+                    5),
+                Occur.MUST)
+            // other terms
+            .add(new TermQuery(new Term("foo", "baz")), Occur.MUST)
+            .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
+            //
+            .add(new TermQuery(new Term("foo", "bar")), Occur.MUST_NOT)
+            .build();
+
+    assertEquals(MatchNoDocsQuery.INSTANCE, searcher.rewrite(bq4));
   }
 
   // MatchAllQuery as MUST_NOT clause cannot return anything