fuzzy-query.asciidoc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. [[query-dsl-fuzzy-query]]
  2. === Fuzzy Query
  3. A fuzzy query that uses similarity based on Levenshtein (edit
  4. distance) algorithm. This maps to Lucene's `FuzzyQuery`.
  5. Warning: this query is not very scalable with its default prefix length
  6. of 0 - in this case, *every* term will be enumerated and cause an edit
  7. score calculation or `max_expansions` is not set.
  8. Here is a simple example:
  9. [source,js]
  10. --------------------------------------------------
  11. {
  12. "fuzzy" : { "user" : "ki" }
  13. }
  14. --------------------------------------------------
  15. More complex settings can be set (the values here are the default
  16. values):
  17. [source,js]
  18. --------------------------------------------------
  19. {
  20. "fuzzy" : {
  21. "user" : {
  22. "value" : "ki",
  23. "boost" : 1.0,
  24. "min_similarity" : 0.5,
  25. "prefix_length" : 0
  26. }
  27. }
  28. }
  29. --------------------------------------------------
  30. The `max_expansions` parameter (unbounded by default) controls the
  31. number of terms the fuzzy query will expand to.
  32. [float]
  33. ==== Numeric / Date Fuzzy
  34. `fuzzy` query on a numeric field will result in a range query "around"
  35. the value using the `min_similarity` value. For example:
  36. [source,js]
  37. --------------------------------------------------
  38. {
  39. "fuzzy" : {
  40. "price" : {
  41. "value" : 12,
  42. "min_similarity" : 2
  43. }
  44. }
  45. }
  46. --------------------------------------------------
  47. Will result in a range query between 10 and 14. Same applies to dates,
  48. with support for time format for the `min_similarity` field:
  49. [source,js]
  50. --------------------------------------------------
  51. {
  52. "fuzzy" : {
  53. "created" : {
  54. "value" : "2010-02-05T12:05:07",
  55. "min_similarity" : "1d"
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. In the mapping, numeric and date types now allow to configure a
  61. `fuzzy_factor` mapping value (defaults to 1), which will be used to
  62. multiply the fuzzy value by it when used in a `query_string` type query.
  63. For example, for dates, a fuzzy factor of "1d" will result in
  64. multiplying whatever fuzzy value provided in the min_similarity by it.
  65. Note, this is explicitly supported since query_string query only allowed
  66. for similarity valued between 0.0 and 1.0.