2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-fuzzy-query]]
|
|
|
|
=== Fuzzy Query
|
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
The fuzzy query uses similarity based on Levenshtein edit distance for
|
|
|
|
`string` fields, and a `+/-` margin on numeric and date fields.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
==== String fields
|
|
|
|
|
|
|
|
The `fuzzy` query generates all possible matching terms that are within the
|
|
|
|
maximum edit distance specified in `fuzziness` and then checks the term
|
|
|
|
dictionary to find out which of those generated terms actually exist in the
|
|
|
|
index.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Here is a simple example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"fuzzy" : { "user" : "ki" }
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
Or with more advanced settings:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2014-01-02 10:45:24 -05:00
|
|
|
{
|
|
|
|
"fuzzy" : {
|
|
|
|
"user" : {
|
|
|
|
"value" : "ki",
|
|
|
|
"boost" : 1.0,
|
|
|
|
"fuzziness" : 2,
|
|
|
|
"prefix_length" : 0,
|
|
|
|
"max_expansions": 100
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
2014-01-02 10:45:24 -05:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
[float]
|
|
|
|
===== Parameters
|
|
|
|
|
|
|
|
[horizontal]
|
|
|
|
`fuzziness`::
|
|
|
|
|
|
|
|
The maximum edit distance. Defaults to `AUTO`. See <<fuzziness>>.
|
|
|
|
|
|
|
|
`prefix_length`::
|
|
|
|
|
|
|
|
The number of initial characters which will not be ``fuzzified''. This
|
|
|
|
helps to reduce the number of terms which must be examined. Defaults
|
|
|
|
to `0`.
|
|
|
|
|
|
|
|
`max_expansions`::
|
|
|
|
|
|
|
|
The maximum number of terms that the `fuzzy` query will expand to.
|
|
|
|
Defaults to `0`.
|
|
|
|
|
|
|
|
|
|
|
|
WARNING: this query can be very heavy if `prefix_length` and `max_expansions`
|
|
|
|
are both set to their defaults of `0`. This could cause every term in the
|
|
|
|
index to be examined!
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2014-01-02 10:45:24 -05:00
|
|
|
==== Numeric and date fields
|
|
|
|
|
|
|
|
Performs a <<query-dsl-range-query>> ``around'' the value using the
|
|
|
|
`fuzziness` value as a `+/-` range, where:
|
|
|
|
|
|
|
|
-fuzziness <= field value <= +fuzziness
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"fuzzy" : {
|
|
|
|
"price" : {
|
|
|
|
"value" : 12,
|
2014-01-02 10:45:24 -05:00
|
|
|
"fuzziness" : 2
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
Will result in a range query between 10 and 14. Date fields support
|
|
|
|
<<time-units,time values>>, eg:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"fuzzy" : {
|
|
|
|
"created" : {
|
|
|
|
"value" : "2010-02-05T12:05:07",
|
2014-01-02 10:45:24 -05:00
|
|
|
"fuzziness" : "1d"
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-01-02 10:45:24 -05:00
|
|
|
See <<fuzziness>> for more details about accepted values.
|