lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dna...@apache.org
Subject svn commit: r202284 - in /lucene/java/trunk/src: java/org/apache/lucene/queryParser/MultiFieldQueryParser.java test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
Date Tue, 28 Jun 2005 21:43:48 GMT
Author: dnaber
Date: Tue Jun 28 14:43:47 2005
New Revision: 202284

URL: http://svn.apache.org/viewcvs?rev=202284&view=rev
Log:
avoid NullPointerException when an analyzer returns null for some term
(bug #33848), patch inspired by John Wang

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java
    lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java?rev=202284&r1=202283&r2=202284&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java Tue
Jun 28 14:43:47 2005
@@ -62,9 +62,11 @@
   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);

Modified: lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java?rev=202284&r1=202283&r2=202284&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
(original)
+++ lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
Tue Jun 28 14:43:47 2005
@@ -16,8 +16,13 @@
  * 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;
 
@@ -153,6 +158,44 @@
       fail();
     } catch(IllegalArgumentException e) {
       // expected exception, array length differs
+    }
+  }
+
+  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;
+      }
     }
   }
 



Mime
View raw message