search_queries_fuzzy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. // FuzzyQuery uses similarity based on Levenshtein edit distance for
  6. // string fields, and a +/- margin on numeric and date fields.
  7. // For more details, see
  8. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
  9. type FuzzyQuery struct {
  10. Query
  11. name string
  12. value interface{}
  13. boost float32
  14. fuzziness interface{}
  15. prefixLength *int
  16. maxExpansions *int
  17. transpositions *bool
  18. queryName string
  19. }
  20. // NewFuzzyQuery creates a new fuzzy query.
  21. func NewFuzzyQuery() FuzzyQuery {
  22. q := FuzzyQuery{
  23. boost: -1.0,
  24. }
  25. return q
  26. }
  27. func (q FuzzyQuery) Name(name string) FuzzyQuery {
  28. q.name = name
  29. return q
  30. }
  31. func (q FuzzyQuery) Value(value interface{}) FuzzyQuery {
  32. q.value = value
  33. return q
  34. }
  35. func (q FuzzyQuery) Boost(boost float32) FuzzyQuery {
  36. q.boost = boost
  37. return q
  38. }
  39. // Fuzziness can be an integer/long like 0, 1 or 2 as well as strings like "auto",
  40. // "0..1", "1..4" or "0.0..1.0".
  41. func (q FuzzyQuery) Fuzziness(fuzziness interface{}) FuzzyQuery {
  42. q.fuzziness = fuzziness
  43. return q
  44. }
  45. func (q FuzzyQuery) PrefixLength(prefixLength int) FuzzyQuery {
  46. q.prefixLength = &prefixLength
  47. return q
  48. }
  49. func (q FuzzyQuery) MaxExpansions(maxExpansions int) FuzzyQuery {
  50. q.maxExpansions = &maxExpansions
  51. return q
  52. }
  53. func (q FuzzyQuery) Transpositions(transpositions bool) FuzzyQuery {
  54. q.transpositions = &transpositions
  55. return q
  56. }
  57. func (q FuzzyQuery) QueryName(queryName string) FuzzyQuery {
  58. q.queryName = queryName
  59. return q
  60. }
  61. // Creates the query source for the ids query.
  62. func (q FuzzyQuery) Source() interface{} {
  63. // {
  64. // "fuzzy" : {
  65. // "user" : {
  66. // "value" : "ki",
  67. // "boost" : 1.0,
  68. // "fuzziness" : 2,
  69. // "prefix_length" : 0,
  70. // "max_expansions" : 100
  71. // }
  72. // }
  73. source := make(map[string]interface{})
  74. query := make(map[string]interface{})
  75. source["fuzzy"] = query
  76. fq := make(map[string]interface{})
  77. query[q.name] = fq
  78. fq["value"] = q.value
  79. if q.boost != -1.0 {
  80. fq["boost"] = q.boost
  81. }
  82. if q.transpositions != nil {
  83. fq["transpositions"] = *q.transpositions
  84. }
  85. if q.fuzziness != nil {
  86. fq["fuzziness"] = q.fuzziness
  87. }
  88. if q.prefixLength != nil {
  89. fq["prefix_length"] = *q.prefixLength
  90. }
  91. if q.maxExpansions != nil {
  92. fq["max_expansions"] = *q.maxExpansions
  93. }
  94. if q.queryName != "" {
  95. fq["_name"] = q.queryName
  96. }
  97. return source
  98. }