Skip to content

Apache Lucene open-source search software

LUCENE-8305: ComplexPhraseQuery.rewrite now handles an embedded MultiTermQuery that rewrites to a MatchNoDocsQuery instead of throwing an exception. Fixes #258 Fixes #327

David Smileye91d120999d0
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index baede740f05..2535eb15fa0 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -198,6 +198,10 @@ Bug Fixes
* LUCENE-8244: Do not leak open file descriptors in SearcherTaxonomyManager's
refresh on exception (Mike McCandless)
+* LUCENE-8305: ComplexPhraseQuery.rewrite now handles an embedded MultiTermQuery
+ that rewrites to a MatchNoDocsQuery instead of throwing an exception.
+ (Bjarke Mortensen, Andy Tran via David Smiley)
+
Other
* LUCENE-8301: Update randomizedtesting to 2.6.0. (Dawid Weiss)
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java
index 32f4fb31e8c..ffe0066ed1d 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java
@@ -398,6 +398,13 @@ public class ComplexPhraseQueryParser extends QueryParser {
} else if (childQuery instanceof BooleanQuery) {
BooleanQuery cbq = (BooleanQuery) childQuery;
addComplexPhraseClause(chosenList, cbq);
+ } else if (childQuery instanceof MatchNoDocsQuery) {
+ // Insert fake term e.g. phrase query was for "Fred Smithe*" and
+ // there were no "Smithe*" terms - need to
+ // prevent match on just "Fred".
+ SpanQuery stq = new SpanTermQuery(new Term(field,
+ "Dummy clause because no terms found - must match nothing"));
+ chosenList.add(stq);
} else {
// TODO alternatively could call extract terms here?
throw new IllegalArgumentException("Unknown query type:"
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
index 5c45e280774..5935da96048 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
@@ -64,6 +64,8 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
checkMatches("\"john\"", "1,3"); // Simple single-term still works
checkMatches("\"(john OR johathon) smith\"", "1,2"); // boolean logic with
// brackets works.
+ checkMatches("\"(john OR nosuchword*) smith\"", "1"); // boolean logic with
+ // brackets works when one of the terms in BooleanQuery does not exist (LUCENE-8305).
checkMatches("\"(jo* -john) smyth~\"", "2"); // boolean logic with
// brackets works.