mirror of https://github.com/apache/lucene.git
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
This commit is contained in:
parent
45a706eb30
commit
ae706faf12
|
@ -76,6 +76,8 @@ public class MultiFieldQueryParser extends QueryParser
|
||||||
clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
|
clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (clauses.size() == 0) // happens for stopwords
|
||||||
|
return null;
|
||||||
return getBooleanQuery(clauses, true);
|
return getBooleanQuery(clauses, true);
|
||||||
}
|
}
|
||||||
return super.getFieldQuery(field, queryText);
|
return super.getFieldQuery(field, queryText);
|
||||||
|
|
|
@ -24,7 +24,14 @@ import org.apache.lucene.analysis.Analyzer;
|
||||||
import org.apache.lucene.analysis.Token;
|
import org.apache.lucene.analysis.Token;
|
||||||
import org.apache.lucene.analysis.TokenStream;
|
import org.apache.lucene.analysis.TokenStream;
|
||||||
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
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.search.Query;
|
||||||
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests QueryParser.
|
* 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());
|
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".
|
* Return empty tokens for field "f1".
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue