Class FuzzySuggester
- java.lang.Object
-
- org.apache.lucene.search.suggest.Lookup
-
- org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester
-
- org.apache.lucene.search.suggest.analyzing.FuzzySuggester
-
- All Implemented Interfaces:
Accountable
public final class FuzzySuggester extends AnalyzingSuggester
Implements a fuzzyAnalyzingSuggester. The similarity measurement is based on the Damerau-Levenshtein (optimal string alignment) algorithm, though you can explicitly choose classic Levenshtein by passingfalsefor thetranspositionsparameter.At most, this query will match terms up to 2 edits. Higher distances are not supported. Note that the fuzzy distance is measured in "byte space" on the bytes returned by the
TokenStream'sTermToBytesRefAttribute, usually UTF8. By default the analyzed bytes must be at least 3DEFAULT_MIN_FUZZY_LENGTHbytes before any edits are considered. Furthermore, the first 1DEFAULT_NON_FUZZY_PREFIXbyte is not allowed to be edited. We allow up to 1 (@link #DEFAULT_MAX_EDITS} edit. IfunicodeAwareparameter in the constructor is set to true, maxEdits, minFuzzyLength, transpositions and nonFuzzyPrefix are measured in Unicode code points (actual letters) instead of bytes.NOTE: This suggester does not boost suggestions that required no edits over suggestions that did require edits. This is a known limitation.
Note: complex query analyzers can have a significant impact on the lookup performance. It's recommended to not use analyzers that drop or inject terms like synonyms to keep the complexity of the prefix intersection low for good lookup performance. At index time, complex analyzers can safely be used.
- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.lucene.search.suggest.Lookup
Lookup.LookupPriorityQueue, Lookup.LookupResult
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_EDITSThe default maximum number of edits for fuzzy suggestions.static intDEFAULT_MIN_FUZZY_LENGTHThe default minimum length of the key passed toAnalyzingSuggester.lookup(java.lang.CharSequence, java.util.Set<org.apache.lucene.util.BytesRef>, boolean, int)before any edits are allowed.static intDEFAULT_NON_FUZZY_PREFIXThe default prefix length where edits are not allowed.static booleanDEFAULT_TRANSPOSITIONSThe default transposition value passed toLevenshteinAutomatastatic booleanDEFAULT_UNICODE_AWAREMeasure maxEdits, minFuzzyLength, transpositions and nonFuzzyPrefix parameters in Unicode code points (actual letters) instead of bytes.-
Fields inherited from class org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester
EXACT_FIRST, PRESERVE_SEP
-
Fields inherited from class org.apache.lucene.search.suggest.Lookup
CHARSEQUENCE_COMPARATOR
-
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLE
-
-
Constructor Summary
Constructors Constructor Description FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer analyzer)Creates aFuzzySuggesterinstance initialized with default values.FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer indexAnalyzer, Analyzer queryAnalyzer)Creates aFuzzySuggesterinstance with an index and query analyzer initialized with default values.FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int options, int maxSurfaceFormsPerAnalyzedForm, int maxGraphExpansions, boolean preservePositionIncrements, int maxEdits, boolean transpositions, int nonFuzzyPrefix, int minFuzzyLength, boolean unicodeAware)Creates aFuzzySuggesterinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected AutomatonconvertAutomaton(Automaton a)Used by subclass to change the lookup automaton, if necessary.protected List<FSTUtil.Path<PairOutputs.Pair<Long,BytesRef>>>getFullPrefixPaths(List<FSTUtil.Path<PairOutputs.Pair<Long,BytesRef>>> prefixPaths, Automaton lookupAutomaton, FST<PairOutputs.Pair<Long,BytesRef>> fst)Returns all prefix paths to initialize the search.-
Methods inherited from class org.apache.lucene.search.suggest.analyzing.AnalyzingSuggester
build, get, getChildResources, getCount, load, lookup, ramBytesUsed, store
-
-
-
-
Field Detail
-
DEFAULT_UNICODE_AWARE
public static final boolean DEFAULT_UNICODE_AWARE
Measure maxEdits, minFuzzyLength, transpositions and nonFuzzyPrefix parameters in Unicode code points (actual letters) instead of bytes.- See Also:
- Constant Field Values
-
DEFAULT_MIN_FUZZY_LENGTH
public static final int DEFAULT_MIN_FUZZY_LENGTH
The default minimum length of the key passed toAnalyzingSuggester.lookup(java.lang.CharSequence, java.util.Set<org.apache.lucene.util.BytesRef>, boolean, int)before any edits are allowed.- See Also:
- Constant Field Values
-
DEFAULT_NON_FUZZY_PREFIX
public static final int DEFAULT_NON_FUZZY_PREFIX
The default prefix length where edits are not allowed.- See Also:
- Constant Field Values
-
DEFAULT_MAX_EDITS
public static final int DEFAULT_MAX_EDITS
The default maximum number of edits for fuzzy suggestions.- See Also:
- Constant Field Values
-
DEFAULT_TRANSPOSITIONS
public static final boolean DEFAULT_TRANSPOSITIONS
The default transposition value passed toLevenshteinAutomata- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FuzzySuggester
public FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer analyzer)
Creates aFuzzySuggesterinstance initialized with default values.- Parameters:
analyzer- the analyzer used for this suggester
-
FuzzySuggester
public FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer indexAnalyzer, Analyzer queryAnalyzer)
Creates aFuzzySuggesterinstance with an index and query analyzer initialized with default values.- Parameters:
indexAnalyzer- Analyzer that will be used for analyzing suggestions while building the index.queryAnalyzer- Analyzer that will be used for analyzing query text during lookup
-
FuzzySuggester
public FuzzySuggester(Directory tempDir, String tempFileNamePrefix, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int options, int maxSurfaceFormsPerAnalyzedForm, int maxGraphExpansions, boolean preservePositionIncrements, int maxEdits, boolean transpositions, int nonFuzzyPrefix, int minFuzzyLength, boolean unicodeAware)
Creates aFuzzySuggesterinstance.- Parameters:
indexAnalyzer- Analyzer that will be used for analyzing suggestions while building the index.queryAnalyzer- Analyzer that will be used for analyzing query text during lookupoptions- seeAnalyzingSuggester.EXACT_FIRST,AnalyzingSuggester.PRESERVE_SEPmaxSurfaceFormsPerAnalyzedForm- Maximum number of surface forms to keep for a single analyzed form. When there are too many surface forms we discard the lowest weighted ones.maxGraphExpansions- Maximum number of graph paths to expand from the analyzed form. Set this to -1 for no limit.preservePositionIncrements- Whether position holes should appear in the automatonmaxEdits- must be >= 0 and <=LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE.transpositions-trueif transpositions should be treated as a primitive edit operation. If this is false, comparisons will implement the classic Levenshtein algorithm.nonFuzzyPrefix- length of common (non-fuzzy) prefix (see defaultDEFAULT_NON_FUZZY_PREFIXminFuzzyLength- minimum length of lookup key before any edits are allowed (see defaultDEFAULT_MIN_FUZZY_LENGTH)unicodeAware- operate Unicode code points instead of bytes.
-
-
Method Detail
-
getFullPrefixPaths
protected List<FSTUtil.Path<PairOutputs.Pair<Long,BytesRef>>> getFullPrefixPaths(List<FSTUtil.Path<PairOutputs.Pair<Long,BytesRef>>> prefixPaths, Automaton lookupAutomaton, FST<PairOutputs.Pair<Long,BytesRef>> fst) throws IOException
Description copied from class:AnalyzingSuggesterReturns all prefix paths to initialize the search.- Overrides:
getFullPrefixPathsin classAnalyzingSuggester- Throws:
IOException
-
convertAutomaton
protected Automaton convertAutomaton(Automaton a)
Description copied from class:AnalyzingSuggesterUsed by subclass to change the lookup automaton, if necessary.- Overrides:
convertAutomatonin classAnalyzingSuggester
-
-