mirror of https://github.com/apache/lucene.git
avoid NullPointerException when an analyzer returns null for some term
(bug #33848), patch inspired by John Wang git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@202284 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
efc2367ac6
commit
04b155a227
|
@ -62,9 +62,11 @@ public class MultiFieldQueryParser extends QueryParser
|
|||
protected Query getFieldQuery(String field, String queryText) throws ParseException {
|
||||
if (field == null) {
|
||||
Vector clauses = new Vector();
|
||||
for (int i = 0; i < fields.length; i++)
|
||||
clauses.add(new BooleanClause(super.getFieldQuery(fields[i], queryText),
|
||||
BooleanClause.Occur.SHOULD));
|
||||
for (int i = 0; i < fields.length; i++) {
|
||||
Query q = super.getFieldQuery(fields[i], queryText);
|
||||
if (q != null)
|
||||
clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
|
||||
}
|
||||
return getBooleanQuery(clauses, true);
|
||||
}
|
||||
return super.getFieldQuery(field, queryText);
|
||||
|
|
|
@ -16,8 +16,13 @@ package org.apache.lucene.queryParser;
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.search.Query;
|
||||
|
||||
|
@ -156,4 +161,42 @@ public class TestMultiFieldQueryParser extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testAnalyzerReturningNull() throws ParseException {
|
||||
String[] fields = new String[] { "f1", "f2", "f3" };
|
||||
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new AnalyzerReturningNull());
|
||||
Query q = parser.parse("bla AND blo");
|
||||
assertEquals("+(f2:bla f3:bla) +(f2:blo f3:blo)", q.toString());
|
||||
// the following queries are not affected as their terms are not analyzed anyway:
|
||||
q = parser.parse("bla*");
|
||||
assertEquals("f1:bla* f2:bla* f3:bla*", q.toString());
|
||||
q = parser.parse("bla~");
|
||||
assertEquals("f1:bla~0.5 f2:bla~0.5 f3:bla~0.5", q.toString());
|
||||
q = parser.parse("[a TO c]");
|
||||
assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return empty tokens for field "f1".
|
||||
*/
|
||||
private static class AnalyzerReturningNull extends Analyzer {
|
||||
StandardAnalyzer stdAnalyzer = new StandardAnalyzer();
|
||||
|
||||
public AnalyzerReturningNull() {
|
||||
}
|
||||
|
||||
public TokenStream tokenStream(String fieldName, Reader reader) {
|
||||
if ("f1".equals(fieldName)) {
|
||||
return new EmptyTokenStream();
|
||||
} else {
|
||||
return stdAnalyzer.tokenStream(fieldName, reader);
|
||||
}
|
||||
}
|
||||
|
||||
private static class EmptyTokenStream extends TokenStream {
|
||||
public Token next() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue