Fuzzy query
Returns documents that contain terms similar to the search term, as measured by a Levenshtein edit distance.
An edit distance is the number of one-character changes needed to turn one term into another. These changes can include:
- Changing a character (box → fox)
- Removing a character (black → lack)
- Inserting a character (sic → sick)
- Transposing two adjacent characters (act → cat)
To find similar terms, the fuzzy query creates a set of all possible variations, or expansions, of the search term within a specified edit distance. The query then returns exact matches for each expansion.
Example requests
Simple example
GET /_search
{
"query": {
"fuzzy": {
"user.id": {
"value": "ki"
}
}
}
} Example using advanced parameters
GET /_search
{
"query": {
"fuzzy": {
"user.id": {
"value": "ki",
"fuzziness": "AUTO",
"max_expansions": 50,
"prefix_length": 0,
"transpositions": true,
"rewrite": "constant_score"
}
}
}
} Top-level parameters for fuzzy
-
<field> - (Required, object) Field you wish to search.
Parameters for <field>
-
value - (Required, string) Term you wish to find in the provided
<field>. -
fuzziness - (Optional, string) Maximum edit distance allowed for matching. See Fuzziness for valid values and more information.
-
max_expansions -
(Optional, integer) Maximum number of variations created. Defaults to
50.Avoid using a high value in the
max_expansionsparameter, especially if theprefix_lengthparameter value is0. High values in themax_expansionsparameter can cause poor performance due to the high number of variations examined. -
prefix_length - (Optional, integer) Number of beginning characters left unchanged when creating expansions. Defaults to
0. -
transpositions - (Optional, Boolean) Indicates whether edits include transpositions of two adjacent characters (ab → ba). Defaults to
true. -
rewrite - (Optional, string) Method used to rewrite the query. For valid values and more information, see the
rewriteparameter.
Notes
Fuzzy queries will not be executed if search.allow_expensive_queries is set to false.
© 2023-2025 Elasticsearch
As of September 2024, Elasticsearch is available under a choice of three licenses: the Server Side Public License (SSPL), the Elastic License, or the AGPLv3 (OSI approved).
Elasticsearch and the Elasticsearch logo are trademarks of Elasticsearch B.V., registered in the U.S. and in other countries.
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-fuzzy-query.html