From ae706faf1262537ba4f8a4ec67dd230ca20ee324 Mon Sep 17 00:00:00 2001 From: Daniel Naber Date: Tue, 4 Oct 2005 21:30:37 +0000 Subject: [PATCH] don't let MultiFieldQueryParser create empty clauses for stopwords git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@294924 13f79535-47bb-0310-9956-ffa450edef68 --- .../queryParser/MultiFieldQueryParser.java | 2 ++ .../TestMultiFieldQueryParser.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java index f47615ee186..a72fdb6387d 100644 --- a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java +++ b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java @@ -76,6 +76,8 @@ public class MultiFieldQueryParser extends QueryParser clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD)); } } + if (clauses.size() == 0) // happens for stopwords + return null; return getBooleanQuery(clauses, true); } return super.getFieldQuery(field, queryText); diff --git a/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java b/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java index 6ab65b4a357..0677926591d 100644 --- a/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java +++ b/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java @@ -24,7 +24,14 @@ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Token; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; /** * Tests QueryParser. @@ -181,6 +188,25 @@ public class TestMultiFieldQueryParser extends TestCase { assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString()); } + public void testStopWordSearching() throws Exception { + Analyzer analyzer = new StandardAnalyzer(); + Directory ramDir = new RAMDirectory(); + IndexWriter iw = new IndexWriter(ramDir, analyzer, true); + Document doc = new Document(); + doc.add(new Field("body", "blah the footest blah", Field.Store.NO, Field.Index.TOKENIZED)); + iw.addDocument(doc); + iw.close(); + + MultiFieldQueryParser mfqp = + new MultiFieldQueryParser(new String[] {"body"}, analyzer); + mfqp.setDefaultOperator(QueryParser.Operator.AND); + Query q = mfqp.parse("the footest"); + IndexSearcher is = new IndexSearcher(ramDir); + Hits hits = is.search(q); + assertEquals(1, hits.length()); + is.close(); + } + /** * Return empty tokens for field "f1". */