java.lang.Object
org.apache.lucene.search.suggest.fst.FSTCompletion
Finite state automata based implementation of "autocomplete" functionality.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
A single completion for a given key. -
Field Summary
FieldsModifier and TypeFieldDescriptionFinite state automaton encoding all the lookup terms.static final int
Default number of buckets.private static final ArrayList<FSTCompletion.Completion>
An empty result.private boolean
private boolean
An array of arcs leaving the root automaton state and encoding weights of all completions in their sub-trees. -
Constructor Summary
ConstructorsConstructorDescriptionFSTCompletion
(FST<Object> automaton) Defaults to higher weights first and exact first.FSTCompletion
(FST<Object> automaton, boolean higherWeightsFirst, boolean exactFirst) Constructs an FSTCompletion, specifying higherWeightsFirst and exactFirst. -
Method Summary
Modifier and TypeMethodDescriptioncacheRootArcs
(FST<Object> automaton) Cache the root node's output arcs starting with completions with the highest weights.private Stream<? extends FSTCompletion.Completion>
completionStream
(BytesRef output, int bucket, FST.Arc<Object> fromArc) Return a stream of all completions starting from the provided arc.private boolean
descendWithPrefix
(FST.Arc<Object> arc, BytesRef utf8) Descend along the path starting atarc
and going through bytes in the argument.int
getBucket
(CharSequence key) Returns the bucket assigned to a given key (if found) or-1
if no exact match exists.int
Returns the bucket count (discretization thresholds).private int
getExactMatchStartingFromRootArc
(int rootArcIndex, BytesRef utf8) Returns the first exact match by traversing root arcs, starting from the arcrootArcIndex
.getFST()
Returns the internal automaton.lookup
(CharSequence key) Lookup suggestions tokey
and return a stream of matching completions.lookup
(CharSequence key, int num) Lookup suggestions tokey
.private Stream<FSTCompletion.Completion>
Lookup suggestions sorted by weight (descending order).
-
Field Details
-
DEFAULT_BUCKETS
public static final int DEFAULT_BUCKETSDefault number of buckets.- See Also:
-
EMPTY_RESULT
An empty result. Keep this anArrayList
to keep all the returned lists of single type (monomorphic calls). -
automaton
Finite state automaton encoding all the lookup terms. See class notes for details. -
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:
-
higherWeightsFirst
private boolean higherWeightsFirst- See Also:
-
-
Constructor Details
-
FSTCompletion
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 tofalse
has 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
Defaults to higher weights first and exact first.- See Also:
-
-
Method Details
-
cacheRootArcs
Cache the root node's output arcs starting with completions with the highest weights. -
getExactMatchStartingFromRootArc
Returns the first exact match by traversing root arcs, starting from the arcrootArcIndex
.- Parameters:
rootArcIndex
- The first root arc index inrootArcs
to consider when matching.utf8
- The sequence of utf8 bytes to follow.- Returns:
- Returns the bucket number of the match or
-1
if no match was found.
-
lookup
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).
-
lookup
Lookup suggestions tokey
and return a stream of matching completions. The stream fetches completions dynamically - it can be filtered and limited to acquire the desired number of completions without collecting all of them.- Parameters:
key
- The prefix to which suggestions should be sought.- Returns:
- Returns the suggestions
-
lookupSortedByWeight
Lookup suggestions sorted by weight (descending order).- Throws:
IOException
-
completionStream
private Stream<? extends FSTCompletion.Completion> completionStream(BytesRef output, int bucket, FST.Arc<Object> fromArc) throws IOException Return a stream of all completions starting from the provided arc.- Throws:
IOException
-
descendWithPrefix
Descend along the path starting atarc
and 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
,arc
will be set to the arc matching last byte ofterm
.false
is returned if no such prefix exists. - Throws:
IOException
-
getBucketCount
public int getBucketCount()Returns the bucket count (discretization thresholds). -
getBucket
Returns the bucket assigned to a given key (if found) or-1
if no exact match exists. -
getFST
Returns the internal automaton.
-