Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6046: fix test failure, add maxDeterminizedStates to AutomatonQuery and WildcardQuery too

Michael McCandless9014bb342d63
diff --git a/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java b/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
index c6b95272495..77b46a5fd04 100644
--- a/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/AutomatonQuery.java
@@ -26,6 +26,7 @@ import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.ToStringUtils;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.CompiledAutomaton;
+import org.apache.lucene.util.automaton.Operations;
/**
* A {@link Query} that will match terms against a finite-state machine.
@@ -61,10 +62,26 @@ public class AutomatonQuery extends MultiTermQuery {
* match.
*/
public AutomatonQuery(final Term term, Automaton automaton) {
+ this(term, automaton, Operations.DEFAULT_MAX_DETERMINIZED_STATES);
+ }
+
+ /**
+ * Create a new AutomatonQuery from an {@link Automaton}.
+ *
+ * @param term Term containing field and possibly some pattern structure. The
+ * term text is ignored.
+ * @param automaton Automaton to run, terms that are accepted are considered a
+ * match.
+ * @param maxDeterminizedStates maximum number of states in the resulting
+ * automata. If the automata would need more than this many states
+ * TooComplextToDeterminizeException is thrown. Higher number require more
+ * space but can process more complex automata.
+ */
+ public AutomatonQuery(final Term term, Automaton automaton, int maxDeterminizedStates) {
super(term.field());
this.term = term;
this.automaton = automaton;
- this.compiled = new CompiledAutomaton(automaton);
+ this.compiled = new CompiledAutomaton(automaton, null, true, maxDeterminizedStates);
}
@Override
diff --git a/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java b/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
index d804c3c857b..547a20ca84c 100644
--- a/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/RegexpQuery.java
@@ -105,8 +105,9 @@ public class RegexpQuery extends AutomatonQuery {
*/
public RegexpQuery(Term term, int flags, AutomatonProvider provider,
int maxDeterminizedStates) {
- super(term, new RegExp(term.text(), flags).toAutomaton(
- provider, maxDeterminizedStates));
+ super(term,
+ new RegExp(term.text(), flags).toAutomaton(
+ provider, maxDeterminizedStates), maxDeterminizedStates);
}
/** Prints a user-readable version of this query. */
diff --git a/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java b/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
index eae96855c5e..d89a2db910d 100644
--- a/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/WildcardQuery.java
@@ -23,8 +23,8 @@ import java.util.List;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.ToStringUtils;
import org.apache.lucene.util.automaton.Automata;
-import org.apache.lucene.util.automaton.Operations;
import org.apache.lucene.util.automaton.Automaton;
+import org.apache.lucene.util.automaton.Operations;
/** Implements the wildcard search query. Supported wildcards are <code>*</code>, which
* matches any character sequence (including the empty one), and <code>?</code>,
@@ -57,6 +57,17 @@ public class WildcardQuery extends AutomatonQuery {
super(term, toAutomaton(term));
}
+ /**
+ * Constructs a query for terms matching <code>term</code>.
+ * @param maxDeterminizedStates maximum number of states in the resulting
+ * automata. If the automata would need more than this many states
+ * TooComplextToDeterminizeException is thrown. Higher number require more
+ * space but can process more complex automata.
+ */
+ public WildcardQuery(Term term, int maxDeterminizedStates) {
+ super(term, toAutomaton(term), maxDeterminizedStates);
+ }
+
/**
* Convert Lucene wildcard syntax into an automaton.
* @lucene.internal
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
index 2ea0aa456db..32afd40717b 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestTermsEnum2.java
@@ -100,9 +100,9 @@ public class TestTermsEnum2 extends LuceneTestCase {
Automaton alternate = Automata.makeStringUnion(matchedTerms);
//System.out.println("match " + matchedTerms.size() + " " + alternate.getNumberOfStates() + " states, sigma=" + alternate.getStartPoints().length);
//AutomatonTestUtil.minimizeSimple(alternate);
- //System.out.println("minmize done");
+ //System.out.println("minimize done");
AutomatonQuery a1 = new AutomatonQuery(new Term("field", ""), automaton);
- AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate);
+ AutomatonQuery a2 = new AutomatonQuery(new Term("field", ""), alternate, Integer.MAX_VALUE);
ScoreDoc[] origHits = searcher.search(a1, 25).scoreDocs;
ScoreDoc[] newHits = searcher.search(a2, 25).scoreDocs;
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
index f2e861f0142..9b34c577d81 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestAutomatonQuery.java
@@ -18,6 +18,9 @@ package org.apache.lucene.search;
*/
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.lucene.document.Document;
@@ -30,6 +33,7 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.Rethrow;
import org.apache.lucene.util.TestUtil;
@@ -237,4 +241,13 @@ public class TestAutomatonQuery extends LuceneTestCase {
thread.join();
}
}
+
+ public void testHugeAutomaton() {
+ List<BytesRef> terms = new ArrayList<>();
+ while (terms.size() < 10000) {
+ terms.add(new BytesRef(TestUtil.randomUnicodeString(random())));
+ }
+ Collections.sort(terms);
+ new AutomatonQuery(new Term("foo", "bar"), Automata.makeStringUnion(terms), Integer.MAX_VALUE);
+ }
}