In ContextQuery, use a more comprehensive check for an empty prefix automaton. (#383)

Context queries check if the wrapped automaton is empty, and if so return an
empty automaton. This commit improves the check for empty automata, which
allows for handling an empty PrefixCompletionQuery as well.
This commit is contained in:
Julie Tibshirani 2021-01-01 20:39:57 -08:00 committed by GitHub
parent 17adcc7aa4
commit beb163c916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View File

@ -187,8 +187,8 @@ public class ContextQuery extends CompletionQuery implements Accountable {
// If the inner automaton matches nothing, then we return an empty weight to avoid
// traversing all contexts during scoring.
if (innerAutomaton.getNumStates() == 0) {
return new CompletionWeight(this, innerAutomaton);
if (Operations.isEmpty(innerAutomaton)) {
return new CompletionWeight(this, Automata.makeEmpty());
}
// if separators are preserved the fst contains a SEP_LABEL

View File

@ -34,6 +34,7 @@ import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.suggest.BitsProducer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
@ -432,6 +433,12 @@ public class TestPrefixCompletionQuery extends LuceneTestCase {
ContextQuery query = new ContextQuery(new PrefixCompletionQuery(analyzer, new Term("suggest_field", "")));
query.addContext("type", 1);
// Ensure that context queries optimize an empty prefix to a fully empty automaton.
CompletionWeight weight = (CompletionWeight) query.createWeight(
suggestIndexSearcher, ScoreMode.COMPLETE, 1.0F);
assertEquals(0, weight.getAutomaton().getNumStates());
// Check that there are no suggestions.
TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 5, false);
assertEquals(0, suggest.scoreDocs.length);

View File

@ -22,6 +22,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.After;
@ -188,6 +189,12 @@ public class TestRegexCompletionQuery extends LuceneTestCase {
ContextQuery query = new ContextQuery(new RegexCompletionQuery(new Term("suggest_field", "")));
query.addContext("type", 1);
// Ensure that context queries optimize an empty regex to a fully empty automaton.
CompletionWeight weight = (CompletionWeight) query.createWeight(
suggestIndexSearcher, ScoreMode.COMPLETE, 1.0F);
assertEquals(0, weight.getAutomaton().getNumStates());
// Check that there are no suggestions.
TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 5, false);
assertEquals(0, suggest.scoreDocs.length);