2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-fuzzy-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Fuzzy query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Fuzzy</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
Returns documents that contain terms similar to the search term, as measured by
|
2020-08-17 11:27:04 -04:00
|
|
|
a {wikipedia}/Levenshtein_distance[Levenshtein edit distance].
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
An edit distance is the number of one-character changes needed to turn one term
|
|
|
|
into another. These changes can include:
|
2014-01-02 10:45:24 -05:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
* Changing a character (**b**ox → **f**ox)
|
|
|
|
* Removing a character (**b**lack → lack)
|
|
|
|
* Inserting a character (sic → sic**k**)
|
|
|
|
* Transposing two adjacent characters (**ac**t → **ca**t)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
[[fuzzy-query-ex-request]]
|
|
|
|
==== Example requests
|
|
|
|
|
|
|
|
[[fuzzy-query-ex-simple]]
|
|
|
|
===== Simple example
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-08-14 13:06:23 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"fuzzy": {
|
2020-08-05 12:47:42 -04:00
|
|
|
"user.id": {
|
2020-07-21 15:49:58 -04:00
|
|
|
"value": "ki"
|
|
|
|
}
|
2016-05-24 05:58:43 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2019-08-14 13:06:23 -04:00
|
|
|
----
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
[[fuzzy-query-ex-advanced]]
|
|
|
|
===== Example using advanced parameters
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-08-14 13:06:23 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /_search
|
2014-01-02 10:45:24 -05:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"fuzzy": {
|
2020-08-05 12:47:42 -04:00
|
|
|
"user.id": {
|
2020-07-21 15:49:58 -04:00
|
|
|
"value": "ki",
|
|
|
|
"fuzziness": "AUTO",
|
|
|
|
"max_expansions": 50,
|
|
|
|
"prefix_length": 0,
|
|
|
|
"transpositions": true,
|
|
|
|
"rewrite": "constant_score"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2014-01-02 10:45:24 -05:00
|
|
|
}
|
2019-08-14 13:06:23 -04:00
|
|
|
----
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
[[fuzzy-query-top-level-params]]
|
|
|
|
==== Top-level parameters for `fuzzy`
|
|
|
|
`<field>`::
|
|
|
|
(Required, object) Field you wish to search.
|
2014-01-02 10:45:24 -05:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
[[fuzzy-query-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
|
|
|
`value`::
|
|
|
|
(Required, string) Term you wish to find in the provided `<field>`.
|
2014-01-02 10:45:24 -05:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
`fuzziness`::
|
|
|
|
(Optional, string) Maximum edit distance allowed for matching. See <<fuzziness>>
|
|
|
|
for valid values and more information.
|
2014-01-02 10:45:24 -05:00
|
|
|
|
|
|
|
|
|
|
|
`max_expansions`::
|
2019-08-14 13:06:23 -04:00
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, integer) Maximum number of variations created. Defaults to `50`.
|
2014-01-02 10:45:24 -05:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
WARNING: Avoid using a high value in the `max_expansions` parameter, especially
|
|
|
|
if the `prefix_length` parameter value is `0`. High values in the
|
|
|
|
`max_expansions` parameter can cause poor performance due to the high number of
|
|
|
|
variations examined.
|
|
|
|
--
|
2014-01-02 10:45:24 -05:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
`prefix_length`::
|
|
|
|
(Optional, integer) Number of beginning characters left unchanged when creating
|
|
|
|
expansions. Defaults to `0`.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
`transpositions`::
|
2020-10-29 10:05:57 -04:00
|
|
|
(Optional, Boolean) Indicates whether edits include transpositions of two
|
2019-08-14 13:06:23 -04:00
|
|
|
adjacent characters (ab → ba). Defaults to `true`.
|
2016-05-24 05:58:43 -04:00
|
|
|
|
2019-08-14 13:06:23 -04:00
|
|
|
`rewrite`::
|
|
|
|
(Optional, string) Method used to rewrite the query. For valid values and more
|
2020-02-12 16:56:14 -05:00
|
|
|
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
|
|
|
|
|
2020-08-12 14:01:10 -04:00
|
|
|
[[fuzzy-query-notes]]
|
2020-02-12 16:56:14 -05:00
|
|
|
==== Notes
|
|
|
|
Fuzzy queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
|
|
|
|
is set to false.
|