You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2015/07/04 12:15:18 UTC
svn commit: r1689134 - in /lucene/dev/branches/branch_5x: ./ lucene/
lucene/sandbox/
lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java
lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
Author: mikemccand
Date: Sat Jul 4 10:15:16 2015
New Revision: 1689134
URL: http://svn.apache.org/r1689134
Log:
LUCENE-6657: don't throw NPE when a given segment never saw all terms used in the TAQ
Modified:
lucene/dev/branches/branch_5x/ (props changed)
lucene/dev/branches/branch_5x/lucene/ (props changed)
lucene/dev/branches/branch_5x/lucene/sandbox/ (props changed)
lucene/dev/branches/branch_5x/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java
lucene/dev/branches/branch_5x/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
Modified: lucene/dev/branches/branch_5x/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java?rev=1689134&r1=1689133&r2=1689134&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/sandbox/src/java/org/apache/lucene/search/TermAutomatonQuery.java Sat Jul 4 10:15:16 2015
@@ -34,7 +34,6 @@ import org.apache.lucene.index.TermState
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.search.spans.SpanNearQuery;
-import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.Operations;
@@ -386,20 +385,25 @@ public class TermAutomatonQuery extends
// Initialize the enums; null for a given slot means that term didn't appear in this reader
EnumAndScorer[] enums = new EnumAndScorer[idToTerm.size()];
+ boolean any = false;
for(Map.Entry<Integer,TermContext> ent : termStates.entrySet()) {
TermContext termContext = ent.getValue();
assert termContext.topReaderContext == ReaderUtil.getTopLevelContext(context) : "The top-reader used to create Weight (" + termContext.topReaderContext + ") is not the same as the current reader's top-reader (" + ReaderUtil.getTopLevelContext(context);
BytesRef term = idToTerm.get(ent.getKey());
TermState state = termContext.get(context.ord);
if (state != null) {
-
TermsEnum termsEnum = context.reader().terms(field).iterator();
termsEnum.seekExact(term, state);
enums[ent.getKey()] = new EnumAndScorer(ent.getKey(), termsEnum.postings(null, PostingsEnum.POSITIONS));
+ any = true;
}
}
- return new TermAutomatonScorer(this, enums, anyTermID, idToTerm, similarity.simScorer(stats, context));
+ if (any) {
+ return new TermAutomatonScorer(this, enums, anyTermID, idToTerm, similarity.simScorer(stats, context));
+ } else {
+ return null;
+ }
}
@Override
Modified: lucene/dev/branches/branch_5x/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java?rev=1689134&r1=1689133&r2=1689134&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java Sat Jul 4 10:15:16 2015
@@ -731,4 +731,51 @@ public class TestTermAutomatonQuery exte
r.close();
dir.close();
}
+
+ public void testTermDoesNotExist() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+ Document doc = new Document();
+ doc.add(newTextField("field", "x y z", Field.Store.NO));
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ IndexSearcher s = newSearcher(r);
+
+ TokenStream ts = new CannedTokenStream(new Token[] {
+ token("a", 1, 1),
+ });
+
+ TermAutomatonQuery q = new TokenStreamToTermAutomatonQuery().toQuery("field", ts);
+ // System.out.println("DOT: " + q.toDot());
+ assertEquals(0, s.search(q, 1).totalHits);
+
+ w.close();
+ r.close();
+ dir.close();
+ }
+
+ public void testOneTermDoesNotExist() throws Exception {
+ Directory dir = newDirectory();
+ RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+ Document doc = new Document();
+ doc.add(newTextField("field", "x y z", Field.Store.NO));
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+ IndexSearcher s = newSearcher(r);
+
+ TokenStream ts = new CannedTokenStream(new Token[] {
+ token("a", 1, 1),
+ token("x", 1, 1),
+ });
+
+ TermAutomatonQuery q = new TokenStreamToTermAutomatonQuery().toQuery("field", ts);
+ // System.out.println("DOT: " + q.toDot());
+ assertEquals(0, s.search(q, 1).totalHits);
+
+ w.close();
+ r.close();
+ dir.close();
+ }
}