Apache Lucene open-source search software
LUCENE-7576: detect when special case automaton is passed to Terms.intersect
Mike McCandlessfcccd317ddb4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e62a99d1eb0..4afc5078fa2 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -81,6 +81,10 @@ Bug Fixes* LUCENE-7536: ASCIIFoldingFilterFactory used to return an illegal multi-termcomponent when preserveOriginal was set to true. (Adrien Grand)+* LUCENE-7576: Fix Terms.intersect in the default codec to detect when+ the incoming automaton is a special case and throw a clearer+ exception than NullPointerException (Tom Mortimer via Mike McCandless)+Improvements* LUCENE-6824: TermAutomatonQuery now rewrites to TermQuery,diff --git a/lucene/core/src/java/org/apache/lucene/codecs/blocktree/FieldReader.java b/lucene/core/src/java/org/apache/lucene/codecs/blocktree/FieldReader.javaindex 7f13a3264ce..4ee38262403 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/blocktree/FieldReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/blocktree/FieldReader.java@@ -182,6 +182,9 @@ public final class FieldReader extends Terms implements Accountable {//System.out.println("intersect: " + compiled.type + " a=" + compiled.automaton);// TODO: we could push "it's a range" or "it's a prefix" down into IntersectTermsEnum?// can we optimize knowing that...?+ if (compiled.type != CompiledAutomaton.AUTOMATON_TYPE.NORMAL) {+ throw new IllegalArgumentException("please use CompiledAutomaton.getTermsEnum instead");+ }return new IntersectTermsEnum(this, compiled.automaton, compiled.runAutomaton, compiled.commonSuffixRef, startTerm, compiled.sinkState);}diff --git a/lucene/core/src/java/org/apache/lucene/index/Terms.java b/lucene/core/src/java/org/apache/lucene/index/Terms.javaindex dd48ce9c189..7197e25e549 100644--- a/lucene/core/src/java/org/apache/lucene/index/Terms.java+++ b/lucene/core/src/java/org/apache/lucene/index/Terms.java@@ -49,8 +49,12 @@ public abstract class Terms {* provided <code>startTerm</code> must be accepted by* the automaton.*- * <p><b>NOTE</b>: the returned TermsEnum cannot- * seek</p>.+ * <p>This is an expert low-level API and will only work+ * for {@code NORMAL} compiled automata. To handle any+ * compiled automata you should instead use+ * {@link CompiledAutomaton#getTermsEnum} instead.+ *+ * <p><b>NOTE</b>: the returned TermsEnum cannot seek</p>.** <p><b>NOTE</b>: the terms dictionary is free to* return arbitrary terms as long as the resulted visiteddiff --git a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.javaindex 3f15381e54c..a388d42ae30 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum.java@@ -998,4 +998,22 @@ public class TestTermsEnum extends LuceneTestCase {}dir.close();}++ // LUCENE-7576+ public void testIntersectRegexp() throws Exception {+ Directory d = newDirectory();+ RandomIndexWriter w = new RandomIndexWriter(random(), d);+ Document doc = new Document();+ doc.add(newStringField("field", "foobar", Field.Store.NO));+ w.addDocument(doc);+ IndexReader r = w.getReader();+ Fields fields = MultiFields.getFields(r);+ CompiledAutomaton automaton = new CompiledAutomaton(new RegExp("do_not_match_anything").toAutomaton());+ Terms terms = fields.terms("field");+ String message = expectThrows(IllegalArgumentException.class, () -> {terms.intersect(automaton, null);}).getMessage();+ assertEquals("please use CompiledAutomaton.getTermsEnum instead", message);+ r.close();+ w.close();+ d.close();+ }}