search_queries_fuzzy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2012-present 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. //
  8. // For more details, see
  9. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-fuzzy-query.html
  10. type FuzzyQuery struct {
  11. name string
  12. value interface{}
  13. boost *float64
  14. fuzziness interface{}
  15. prefixLength *int
  16. maxExpansions *int
  17. transpositions *bool
  18. rewrite string
  19. queryName string
  20. }
  21. // NewFuzzyQuery creates a new fuzzy query.
  22. func NewFuzzyQuery(name string, value interface{}) *FuzzyQuery {
  23. q := &FuzzyQuery{
  24. name: name,
  25. value: value,
  26. }
  27. return q
  28. }
  29. // Boost sets the boost for this query. Documents matching this query will
  30. // (in addition to the normal weightings) have their score multiplied by
  31. // the boost provided.
  32. func (q *FuzzyQuery) Boost(boost float64) *FuzzyQuery {
  33. q.boost = &boost
  34. return q
  35. }
  36. // Fuzziness can be an integer/long like 0, 1 or 2 as well as strings
  37. // like "auto", "0..1", "1..4" or "0.0..1.0".
  38. func (q *FuzzyQuery) Fuzziness(fuzziness interface{}) *FuzzyQuery {
  39. q.fuzziness = fuzziness
  40. return q
  41. }
  42. func (q *FuzzyQuery) PrefixLength(prefixLength int) *FuzzyQuery {
  43. q.prefixLength = &prefixLength
  44. return q
  45. }
  46. func (q *FuzzyQuery) MaxExpansions(maxExpansions int) *FuzzyQuery {
  47. q.maxExpansions = &maxExpansions
  48. return q
  49. }
  50. func (q *FuzzyQuery) Transpositions(transpositions bool) *FuzzyQuery {
  51. q.transpositions = &transpositions
  52. return q
  53. }
  54. func (q *FuzzyQuery) Rewrite(rewrite string) *FuzzyQuery {
  55. q.rewrite = rewrite
  56. return q
  57. }
  58. // QueryName sets the query name for the filter that can be used when
  59. // searching for matched filters per hit.
  60. func (q *FuzzyQuery) QueryName(queryName string) *FuzzyQuery {
  61. q.queryName = queryName
  62. return q
  63. }
  64. // Source returns JSON for the function score query.
  65. func (q *FuzzyQuery) Source() (interface{}, error) {
  66. // {
  67. // "fuzzy" : {
  68. // "user" : {
  69. // "value" : "ki",
  70. // "boost" : 1.0,
  71. // "fuzziness" : 2,
  72. // "prefix_length" : 0,
  73. // "max_expansions" : 100
  74. // }
  75. // }
  76. source := make(map[string]interface{})
  77. query := make(map[string]interface{})
  78. source["fuzzy"] = query
  79. fq := make(map[string]interface{})
  80. query[q.name] = fq
  81. fq["value"] = q.value
  82. if q.boost != nil {
  83. fq["boost"] = *q.boost
  84. }
  85. if q.transpositions != nil {
  86. fq["transpositions"] = *q.transpositions
  87. }
  88. if q.fuzziness != nil {
  89. fq["fuzziness"] = q.fuzziness
  90. }
  91. if q.prefixLength != nil {
  92. fq["prefix_length"] = *q.prefixLength
  93. }
  94. if q.maxExpansions != nil {
  95. fq["max_expansions"] = *q.maxExpansions
  96. }
  97. if q.rewrite != "" {
  98. fq["rewrite"] = q.rewrite
  99. }
  100. if q.queryName != "" {
  101. fq["_name"] = q.queryName
  102. }
  103. return source, nil
  104. }