lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From uschind...@apache.org
Subject lucene-solr:master: LUCENE-7416: Simplify MatchNoDocs rewrite in BQ (using Java 8 streams); add another special case: MUST_NOT with MatchAllDocsQuery also produces no results
Date Fri, 26 Aug 2016 07:29:05 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 63b2e8052 -> 646b6bfd2


LUCENE-7416: Simplify MatchNoDocs rewrite in BQ (using Java 8 streams); add another special
case: MUST_NOT with MatchAllDocsQuery also produces no results


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/646b6bfd
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/646b6bfd
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/646b6bfd

Branch: refs/heads/master
Commit: 646b6bfd2c23df36f911a99fd2807b85a961a36b
Parents: 63b2e80
Author: Uwe Schindler <uschindler@apache.org>
Authored: Fri Aug 26 09:28:49 2016 +0200
Committer: Uwe Schindler <uschindler@apache.org>
Committed: Fri Aug 26 09:28:49 2016 +0200

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  2 +-
 .../org/apache/lucene/search/BooleanQuery.java  | 14 +++++-----
 .../lucene/search/TestBooleanRewrites.java      | 28 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/646b6bfd/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 00de5a5..214badc 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -26,7 +26,7 @@ Optimizations
 
 * LUCENE-7416: BooleanQuery optimizes queries that have queries that occur both
   in the sets of SHOULD and FILTER clauses, or both in MUST/FILTER and MUST_NOT
-  clauses. (Spyros Kapnissis via Adrien Grand)
+  clauses. (Spyros Kapnissis via Adrien Grand, Uwe Schindler)
 
 Other
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/646b6bfd/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
----------------------------------------------------------------------
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 b2477e8..e67d7f4 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanQuery.java
@@ -29,6 +29,7 @@ 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.IndexReader;
 import org.apache.lucene.search.BooleanClause.Occur;
@@ -273,14 +274,15 @@ public class BooleanQuery extends Query implements Iterable<BooleanClause>
{
     }
 
     // Check whether some clauses are both required and excluded
-    if (clauseSets.get(Occur.MUST_NOT).size() > 0) {
-      final Set<Query> reqAndExclQueries = new HashSet<Query>(clauseSets.get(Occur.FILTER));
-      reqAndExclQueries.addAll(clauseSets.get(Occur.MUST));
-      reqAndExclQueries.retainAll(clauseSets.get(Occur.MUST_NOT));
-
-      if (reqAndExclQueries.isEmpty() == false) {
+    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.contains(new MatchAllDocsQuery())) {
+        return new MatchNoDocsQuery("MUST_NOT clause is MatchAllDocsQuery");
+      }
     }
 
     // remove FILTER clauses that are also MUST clauses

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/646b6bfd/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
----------------------------------------------------------------------
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 4470841..3ec2dd3 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanRewrites.java
@@ -265,6 +265,34 @@ public class TestBooleanRewrites extends LuceneTestCase {
     assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq2));
   }
 
+  // MatchAllQuery as MUST_NOT clause cannot return anything
+  public void testMatchAllMustNot() throws IOException {
+    IndexSearcher searcher = newSearcher(new MultiReader());
+
+    // Test Must with MatchAll MustNot
+    BooleanQuery bq = new BooleanQuery.Builder()
+            .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
+            .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
+            .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
+            //
+            .add(new MatchAllDocsQuery(), Occur.MUST_NOT)
+            .build();
+
+    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq));
+
+    // Test Must with MatchAll MustNot and other MustNot
+    BooleanQuery bq2 = new BooleanQuery.Builder()
+            .add(new TermQuery(new Term("foo", "bar")), Occur.MUST)
+            .add(new TermQuery(new Term("foo", "baz")), Occur.FILTER)
+            .add(new TermQuery(new Term("foo", "bad")), Occur.SHOULD)
+            //
+            .add(new TermQuery(new Term("foo", "bor")), Occur.MUST_NOT)
+            .add(new MatchAllDocsQuery(), Occur.MUST_NOT)
+            .build();
+
+    assertEquals(new MatchNoDocsQuery(), searcher.rewrite(bq2));
+  }
+
   public void testRemoveMatchAllFilter() throws IOException {
     IndexSearcher searcher = newSearcher(new MultiReader());
 


Mime
View raw message