Class FSTCompletion
- java.lang.Object
-
- org.apache.lucene.search.suggest.fst.FSTCompletion
-
public class FSTCompletion extends java.lang.ObjectFinite state automata based implementation of "autocomplete" functionality.- See Also:
FSTCompletionBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFSTCompletion.CompletionA single completion for a given key.
-
Field Summary
Fields Modifier and Type Field Description private FST<java.lang.Object>automatonFinite state automaton encoding all the lookup terms.static intDEFAULT_BUCKETSDefault number of buckets.private static java.util.ArrayList<FSTCompletion.Completion>EMPTY_RESULTAn empty result.private booleanexactFirstprivate booleanhigherWeightsFirstprivate FST.Arc<java.lang.Object>[]rootArcsAn array of arcs leaving the root automaton state and encoding weights of all completions in their sub-trees.
-
Constructor Summary
Constructors Constructor Description FSTCompletion(FST<java.lang.Object> automaton)Defaults to higher weights first and exact first.FSTCompletion(FST<java.lang.Object> automaton, boolean higherWeightsFirst, boolean exactFirst)Constructs an FSTCompletion, specifying higherWeightsFirst and exactFirst.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static FST.Arc<java.lang.Object>[]cacheRootArcs(FST<java.lang.Object> automaton)Cache the root node's output arcs starting with completions with the highest weights.private booleancheckExistingAndReorder(java.util.ArrayList<FSTCompletion.Completion> list, BytesRef key)Checks if the list ofLookup.LookupResults already has akey.private booleancollect(java.util.List<FSTCompletion.Completion> res, int num, int bucket, BytesRef output, FST.Arc<java.lang.Object> arc)Recursive collect lookup results from the automaton subgraph starting atarc.private booleandescendWithPrefix(FST.Arc<java.lang.Object> arc, BytesRef utf8)Descend along the path starting atarcand going through bytes in the argument.intgetBucket(java.lang.CharSequence key)Returns the bucket assigned to a given key (if found) or-1if no exact match exists.intgetBucketCount()Returns the bucket count (discretization thresholds).private intgetExactMatchStartingFromRootArc(int rootArcIndex, BytesRef utf8)Returns the first exact match by traversing root arcs, starting from the arcrootArcIndex.FST<java.lang.Object>getFST()Returns the internal automaton.java.util.List<FSTCompletion.Completion>lookup(java.lang.CharSequence key, int num)Lookup suggestions tokey.private java.util.List<FSTCompletion.Completion>lookupSortedAlphabetically(BytesRef key, int num)Lookup suggestions sorted alphabetically if weights are not constant.private java.util.ArrayList<FSTCompletion.Completion>lookupSortedByWeight(BytesRef key, int num, boolean collectAll)Lookup suggestions sorted by weight (descending order).
-
-
-
Field Detail
-
DEFAULT_BUCKETS
public static final int DEFAULT_BUCKETS
Default number of buckets.- See Also:
- Constant Field Values
-
EMPTY_RESULT
private static final java.util.ArrayList<FSTCompletion.Completion> EMPTY_RESULT
An empty result. Keep this anArrayListto keep all the returned lists of single type (monomorphic calls).
-
automaton
private final FST<java.lang.Object> automaton
Finite state automaton encoding all the lookup terms. See class notes for details.
-
rootArcs
private final FST.Arc<java.lang.Object>[] rootArcs
An array of arcs leaving the root automaton state and encoding weights of all completions in their sub-trees.
-
exactFirst
private boolean exactFirst
- See Also:
FSTCompletion(FST, boolean, boolean)
-
higherWeightsFirst
private boolean higherWeightsFirst
- See Also:
FSTCompletion(FST, boolean, boolean)
-
-
Constructor Detail
-
FSTCompletion
public FSTCompletion(FST<java.lang.Object> automaton, boolean higherWeightsFirst, boolean exactFirst)
Constructs an FSTCompletion, specifying higherWeightsFirst and exactFirst.- Parameters:
automaton- Automaton with completions. SeeFSTCompletionBuilder.higherWeightsFirst- Return most popular suggestions first. This is the default behavior for this implementation. Setting it tofalsehas no effect (use constant term weights to sort alphabetically only).exactFirst- Find and push an exact match to the first position of the result list if found.
-
FSTCompletion
public FSTCompletion(FST<java.lang.Object> automaton)
Defaults to higher weights first and exact first.- See Also:
FSTCompletion(FST, boolean, boolean)
-
-
Method Detail
-
cacheRootArcs
private static FST.Arc<java.lang.Object>[] cacheRootArcs(FST<java.lang.Object> automaton)
Cache the root node's output arcs starting with completions with the highest weights.
-
getExactMatchStartingFromRootArc
private int getExactMatchStartingFromRootArc(int rootArcIndex, BytesRef utf8)Returns the first exact match by traversing root arcs, starting from the arcrootArcIndex.- Parameters:
rootArcIndex- The first root arc index inrootArcsto consider when matching.utf8- The sequence of utf8 bytes to follow.- Returns:
- Returns the bucket number of the match or
-1if no match was found.
-
lookup
public java.util.List<FSTCompletion.Completion> lookup(java.lang.CharSequence key, int num)
Lookup suggestions tokey.- Parameters:
key- The prefix to which suggestions should be sought.num- At most this number of suggestions will be returned.- Returns:
- Returns the suggestions, sorted by their approximated weight first (decreasing) and then alphabetically (UTF-8 codepoint order).
-
lookupSortedAlphabetically
private java.util.List<FSTCompletion.Completion> lookupSortedAlphabetically(BytesRef key, int num) throws java.io.IOException
Lookup suggestions sorted alphabetically if weights are not constant. This is a workaround: in general, use constant weights for alphabetically sorted result.- Throws:
java.io.IOException
-
lookupSortedByWeight
private java.util.ArrayList<FSTCompletion.Completion> lookupSortedByWeight(BytesRef key, int num, boolean collectAll) throws java.io.IOException
Lookup suggestions sorted by weight (descending order).- Parameters:
collectAll- Iftrue, the routine terminates immediately whennumsuggestions have been collected. Iffalse, it will collect suggestions from all weight arcs (needed forlookupSortedAlphabetically(org.apache.lucene.util.BytesRef, int).- Throws:
java.io.IOException
-
checkExistingAndReorder
private boolean checkExistingAndReorder(java.util.ArrayList<FSTCompletion.Completion> list, BytesRef key)
Checks if the list ofLookup.LookupResults already has akey. If so, reorders thatLookup.LookupResultto the first position.- Returns:
- Returns
trueif and only iflistcontainedkey.
-
descendWithPrefix
private boolean descendWithPrefix(FST.Arc<java.lang.Object> arc, BytesRef utf8) throws java.io.IOException
Descend along the path starting atarcand going through bytes in the argument.- Parameters:
arc- The starting arc. This argument is modified in-place.utf8- The term to descend along.- Returns:
- If
true,arcwill be set to the arc matching last byte ofterm.falseis returned if no such prefix exists. - Throws:
java.io.IOException
-
collect
private boolean collect(java.util.List<FSTCompletion.Completion> res, int num, int bucket, BytesRef output, FST.Arc<java.lang.Object> arc) throws java.io.IOException
Recursive collect lookup results from the automaton subgraph starting atarc.- Parameters:
num- Maximum number of results needed (early termination).- Throws:
java.io.IOException
-
getBucketCount
public int getBucketCount()
Returns the bucket count (discretization thresholds).
-
getBucket
public int getBucket(java.lang.CharSequence key)
Returns the bucket assigned to a given key (if found) or-1if no exact match exists.
-
getFST
public FST<java.lang.Object> getFST()
Returns the internal automaton.
-
-