Class FuzzyQuery
- All Implemented Interfaces:
Cloneable
false
to the transpositions
parameter.
This query uses MultiTermQuery.TopTermsScoringBooleanQueryRewrite
as default. So terms will be collected and scored according to their
edit distance. Only the top terms are used for building the BooleanQuery
.
It is not recommended to change the rewrite mode for fuzzy queries.
At most, this query will match terms up to 2 edits. Higher distances (especially with transpositions enabled), are generally not useful and will match a significant amount of the term dictionary. If you really want this, consider using an n-gram indexing technique (such as the SpellChecker in the suggest module) instead.
NOTE: terms of length 1 or 2 will sometimes not match because of how the scaled distance between two terms is computed. For a term to match, the edit distance between the terms must be less than the minimum length term (either the input term, or the candidate term). For example, FuzzyQuery on term "abcd" with maxEdits=2 will not match an indexed term "ab", and FuzzyQuery on term "a" with maxEdits=2 will not match an indexed term "abc".
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.lucene.search.MultiTermQuery
MultiTermQuery.ConstantScoreAutoRewrite, MultiTermQuery.RewriteMethod, MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite, MultiTermQuery.TopTermsScoringBooleanQueryRewrite
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final float
Deprecated.pass integer edit distances instead.static final int
static final boolean
Fields inherited from class org.apache.lucene.search.MultiTermQuery
CONSTANT_SCORE_AUTO_REWRITE_DEFAULT, CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE, CONSTANT_SCORE_FILTER_REWRITE, SCORING_BOOLEAN_QUERY_REWRITE
-
Constructor Summary
ConstructorsConstructorDescriptionFuzzyQuery
(Term term) FuzzyQuery
(Term term, int maxEdits) FuzzyQuery
(Term term, int maxEdits, int prefixLength) FuzzyQuery
(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions) Create a new FuzzyQuery that will match terms with an edit distance of at mostmaxEdits
toterm
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
static int
floatToEdits
(float minimumSimilarity, int termLen) Deprecated.pass integer edit distances instead.int
int
Returns the non-fuzzy prefix length.getTerm()
Returns the pattern term.boolean
Returns true if transpositions should be treated as a primitive edit operation.int
hashCode()
Prints a query to a string, withfield
assumed to be the default field and omitted.Methods inherited from class org.apache.lucene.search.MultiTermQuery
getField, getRewriteMethod, rewrite, setRewriteMethod
Methods inherited from class org.apache.lucene.search.Query
clone, createWeight, extractTerms, getBoost, setBoost, toString
-
Field Details
-
defaultMaxEdits
public static final int defaultMaxEdits- See Also:
-
defaultPrefixLength
public static final int defaultPrefixLength- See Also:
-
defaultMaxExpansions
public static final int defaultMaxExpansions- See Also:
-
defaultTranspositions
public static final boolean defaultTranspositions- See Also:
-
defaultMinSimilarity
Deprecated.pass integer edit distances instead.- See Also:
-
-
Constructor Details
-
FuzzyQuery
public FuzzyQuery(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions) Create a new FuzzyQuery that will match terms with an edit distance of at mostmaxEdits
toterm
. If aprefixLength
> 0 is specified, a common prefix of that length is also required.- Parameters:
term
- the term to search formaxEdits
- must be >= 0 and invalid input: '<'=LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE
.prefixLength
- length of common (non-fuzzy) prefixmaxExpansions
- the maximum number of terms to match. If this number is greater thanBooleanQuery.getMaxClauseCount()
when the query is rewritten, then the maxClauseCount will be used instead.transpositions
- true if transpositions should be treated as a primitive edit operation. If this is false, comparisons will implement the classic Levenshtein algorithm.
-
FuzzyQuery
-
FuzzyQuery
-
FuzzyQuery
-
-
Method Details
-
getMaxEdits
public int getMaxEdits()- Returns:
- the maximum number of edit distances allowed for this query to match.
-
getPrefixLength
public int getPrefixLength()Returns the non-fuzzy prefix length. This is the number of characters at the start of a term that must be identical (not fuzzy) to the query term if the query is to match that term. -
getTranspositions
public boolean getTranspositions()Returns true if transpositions should be treated as a primitive edit operation. If this is false, comparisons will implement the classic Levenshtein algorithm. -
getTerm
Returns the pattern term. -
toString
Description copied from class:Query
Prints a query to a string, withfield
assumed to be the default field and omitted. -
hashCode
public int hashCode()- Overrides:
hashCode
in classMultiTermQuery
-
equals
- Overrides:
equals
in classMultiTermQuery
-
floatToEdits
Deprecated.pass integer edit distances instead.Helper function to convert from deprecated "minimumSimilarity" fractions to raw edit distances.- Parameters:
minimumSimilarity
- scaled similaritytermLen
- length (in unicode codepoints) of the term.- Returns:
- equivalent number of maxEdits
-