diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 594504dea59..67d59e6b78a 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -186,6 +186,9 @@ Changes in Runtime Behavior * LUCENE-6255: PhraseQuery now ignores leading holes and requires that positions are positive and added in order. (Adrien Grand) +* LUCENE-6298: SimpleQueryParser returns an empty query rather than + null, if e.g. the terms were all stopwords. (Lee Hinman via Robert Muir) + ======================= Lucene 5.0.0 ======================= New Features diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java index 5dff23660c2..f5c23fef52e 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java @@ -141,14 +141,18 @@ public class SimpleQueryParser extends QueryBuilder { this.flags = flags; } - /** Parses the query text and returns parsed query (or null if empty) */ + /** Parses the query text and returns parsed query */ public Query parse(String queryText) { char data[] = queryText.toCharArray(); char buffer[] = new char[data.length]; State state = new State(data, buffer, 0, data.length); parseSubQuery(state); - return state.top; + if (state.top == null) { + return new BooleanQuery(); + } else { + return state.top; + } } private void parseSubQuery(State state) { diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java index 7b55a53d467..42158d50a2c 100644 --- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java @@ -273,17 +273,19 @@ public class TestSimpleQueryParser extends LuceneTestCase { } public void testGarbageEmpty() throws Exception { - assertNull(parse("")); - assertNull(parse(" ")); - assertNull(parse(" ")); - assertNull(parse("\\ ")); - assertNull(parse("\\ \\ ")); - assertNull(parse("\"\"")); - assertNull(parse("\" \"")); - assertNull(parse("\" \"|\" \"")); - assertNull(parse("(\" \"|\" \")")); - assertNull(parse("\" \" \" \"")); - assertNull(parse("(\" \" \" \")")); + BooleanQuery expected = new BooleanQuery(); + + assertEquals(expected, parse("")); + assertEquals(expected, parse(" ")); + assertEquals(expected, parse(" ")); + assertEquals(expected, parse("\\ ")); + assertEquals(expected, parse("\\ \\ ")); + assertEquals(expected, parse("\"\"")); + assertEquals(expected, parse("\" \"")); + assertEquals(expected, parse("\" \"|\" \"")); + assertEquals(expected, parse("(\" \"|\" \")")); + assertEquals(expected, parse("\" \" \" \"")); + assertEquals(expected, parse("(\" \" \" \")")); } public void testGarbageAND() throws Exception {