Skip to content

Commit 9c488f3

Browse files
authored
[DOCS] Rewrite fuzzy query docs (#42078)
1 parent b28f089 commit 9c488f3

File tree

1 file changed

+62
-38
lines changed

1 file changed

+62
-38
lines changed

docs/reference/query-dsl/fuzzy-query.asciidoc

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,99 @@
44
<titleabbrev>Fuzzy</titleabbrev>
55
++++
66

7-
The fuzzy query uses similarity based on Levenshtein edit distance.
7+
Returns documents that contain terms similar to the search term, as measured by
8+
a http://en.wikipedia.org/wiki/Levenshtein_distance[Levenshtein edit distance].
89

9-
==== String fields
10+
An edit distance is the number of one-character changes needed to turn one term
11+
into another. These changes can include:
1012

11-
The `fuzzy` query generates matching terms that are within the
12-
maximum edit distance specified in `fuzziness` and then checks the term
13-
dictionary to find out which of those generated terms actually exist in the
14-
index. The final query uses up to `max_expansions` matching terms.
13+
* Changing a character (**b**ox → **f**ox)
14+
* Removing a character (**b**lack → lack)
15+
* Inserting a character (sic → sic**k**)
16+
* Transposing two adjacent characters (**ac**t → **ca**t)
1517

16-
Here is a simple example:
18+
To find similar terms, the `fuzzy` query creates a set of all possible
19+
variations, or expansions, of the search term within a specified edit distance.
20+
The query then returns exact matches for each expansion.
21+
22+
[[fuzzy-query-ex-request]]
23+
==== Example requests
24+
25+
[[fuzzy-query-ex-simple]]
26+
===== Simple example
1727

1828
[source,js]
19-
--------------------------------------------------
29+
----
2030
GET /_search
2131
{
2232
"query": {
23-
"fuzzy" : { "user" : "ki" }
33+
"fuzzy": {
34+
"user": {
35+
"value": "ki"
36+
}
37+
}
2438
}
2539
}
26-
--------------------------------------------------
40+
----
2741
// CONSOLE
2842

29-
Or with more advanced settings:
43+
[[fuzzy-query-ex-advanced]]
44+
===== Example using advanced parameters
3045

3146
[source,js]
32-
--------------------------------------------------
47+
----
3348
GET /_search
3449
{
3550
"query": {
36-
"fuzzy" : {
37-
"user" : {
51+
"fuzzy": {
52+
"user": {
3853
"value": "ki",
39-
"boost": 1.0,
40-
"fuzziness": 2,
54+
"fuzziness": "AUTO",
55+
"max_expansions": 50,
4156
"prefix_length": 0,
42-
"max_expansions": 100
57+
"transpositions": true,
58+
"rewrite": "constant_score"
4359
}
4460
}
4561
}
4662
}
47-
--------------------------------------------------
63+
----
4864
// CONSOLE
4965

50-
[float]
51-
===== Parameters
66+
[[fuzzy-query-top-level-params]]
67+
==== Top-level parameters for `fuzzy`
68+
`<field>`::
69+
(Required, object) Field you wish to search.
5270

53-
[horizontal]
54-
`fuzziness`::
55-
56-
The maximum edit distance. Defaults to `AUTO`. See <<fuzziness>>.
71+
[[fuzzy-query-field-params]]
72+
==== Parameters for `<field>`
73+
`value`::
74+
(Required, string) Term you wish to find in the provided `<field>`.
5775

58-
`prefix_length`::
76+
`fuzziness`::
77+
(Optional, string) Maximum edit distance allowed for matching. See <<fuzziness>>
78+
for valid values and more information.
5979

60-
The number of initial characters which will not be ``fuzzified''. This
61-
helps to reduce the number of terms which must be examined. Defaults
62-
to `0`.
6380

6481
`max_expansions`::
82+
+
83+
--
84+
(Optional, integer) Maximum number of variations created. Defaults to `50`.
6585

66-
The maximum number of terms that the `fuzzy` query will expand to.
67-
Defaults to `50`.
68-
69-
`transpositions`::
70-
71-
Whether fuzzy transpositions (`ab` -> `ba`) are supported.
72-
Default is `true`.
86+
WARNING: Avoid using a high value in the `max_expansions` parameter, especially
87+
if the `prefix_length` parameter value is `0`. High values in the
88+
`max_expansions` parameter can cause poor performance due to the high number of
89+
variations examined.
90+
--
7391

74-
WARNING: This query can be very heavy if `prefix_length` is set to `0` and if
75-
`max_expansions` is set to a high number. It could result in every term in the
76-
index being examined!
92+
`prefix_length`::
93+
(Optional, integer) Number of beginning characters left unchanged when creating
94+
expansions. Defaults to `0`.
7795

96+
`transpositions`::
97+
(Optional, boolean) Indicates whether edits include transpositions of two
98+
adjacent characters (ab → ba). Defaults to `true`.
7899

100+
`rewrite`::
101+
(Optional, string) Method used to rewrite the query. For valid values and more
102+
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.

0 commit comments

Comments
 (0)