/
fuzzy_query.go
248 lines (226 loc) · 6.15 KB
/
fuzzy_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package picker
import (
"encoding/json"
"github.com/chanced/dynamic"
)
type Fuzzier interface {
Fuzzy() (*FuzzyQuery, error)
}
type CompleteFuzzier interface {
Fuzzier
CompleteClause
}
// FuzzyQueryParams returns documents that contain terms similar to the search term,
// as measured by a Levenshtein edit distance.
//
// An edit distance is the number of one-character changes needed to turn one
// term into another. These changes can include:
//
// - Changing a character (box → fox)
//
// - Removing a character (black → lack)
//
// - Inserting a character (sic → sick)
//
// - Transposing two adjacent characters (act → cat)
//
// 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.
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
type FuzzyQueryParams struct {
// Value or term to find in the provided <field>. (Required)
Value string
// Field which is being queried against.
//
// This will be ignored if set through a mutator
Field string
// Maximum edit distance allowed for matching. See Fuzziness for valid
// values and more information. (Optional)
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#fuzziness
Fuzziness string
// PrefixLength is the number of beginning characters left unchanged when
// creating expansions. Defaults to 0. (Optional)
PrefixLength int
// Transpositions indicates whether edits include transpositions of two
// adjacent characters (ab → ba). (Optional)
//
Transpositions interface{}
// Rewrite method used to rewrite the query. (Optional)
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-term-rewrite.html
Rewrite Rewrite
// Maximum number of variations created. Defaults to 50. (Optional)
//
// 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.
MaxExpansions interface{}
// Name of the query (Optional)
Name string
}
func (f FuzzyQueryParams) Fuzzy() (*FuzzyQuery, error) {
q := &FuzzyQuery{field: f.Field}
err := q.setValue(f.Value)
if err != nil {
return q, newQueryError(err, QueryKindFuzzy, f.Field)
}
err = q.SetMaxExpansions(f.MaxExpansions)
if err != nil {
return q, newQueryError(err, QueryKindFuzzy, f.Field)
}
err = q.SetRewrite(f.Rewrite)
if err != nil {
return q, newQueryError(err, QueryKindFuzzy, f.Field)
}
err = q.SetTranspositions(f.Transpositions)
if err != nil {
return q, newQueryError(err, QueryKindFuzzy, f.Field)
}
q.SetName(f.Name)
q.SetFuzziness(f.Fuzziness)
err = q.SetPrefixLength(f.PrefixLength)
if err != nil {
return q, newQueryError(err, QueryKindFuzzy, f.Field)
}
return q, nil
}
func (f FuzzyQueryParams) Clause() (QueryClause, error) {
return f.Fuzzy()
}
// func NewFuzzyQuery(params Fuzzier) (*FuzzyQuery, error) {
// q, err := params.Fuzzy()
// if err != nil {
// return nil, NewQueryError(err, KindFuzzy, getField(q, nil))
// }
// err = checkField(q.field, KindFuzzy)
// if err != nil {
// return nil, err
// }
// return q, nil
// }
// FuzzyQuery returns documents that contain terms similar to the search term,
// as measured by a Levenshtein edit distance.
//
// An edit distance is the number of one-character changes needed to turn one
// term into another. These changes can include:
//
// - Changing a character (box → fox)
//
// - Removing a character (black → lack)
//
// - Inserting a character (sic → sick)
//
// - Transposing two adjacent characters (act → cat)
//
// 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.
//
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
type FuzzyQuery struct {
field string
value string
fuzzinessParam
maxExpansionsParam
prefixLengthParam
transpositionsParam
rewriteParam
nameParam
completeClause
}
func (f *FuzzyQuery) Clause() (QueryClause, error) {
return f, nil
}
func (f *FuzzyQuery) Vale() string {
return f.value
}
func (f FuzzyQuery) Field() string {
return f.field
}
func (f FuzzyQuery) Kind() QueryKind {
return QueryKindFuzzy
}
func (f *FuzzyQuery) IsEmpty() bool {
if f == nil {
return true
}
return !(len(f.field) != 0 || len(f.value) != 0)
}
func (f *FuzzyQuery) Set(field string, fuzzier Fuzzier) error {
q, err := fuzzier.Fuzzy()
if err != nil {
return newQueryError(err, QueryKindFuzzy, field)
}
err = checkField(field, QueryKindFuzzy)
if err != nil {
return err
}
q.field = field
*f = *q
return nil
}
func (f *FuzzyQuery) setValue(v string) error {
if len(v) == 0 {
return ErrValueRequired
}
return nil
}
func (f FuzzyQuery) MarshalBSON() ([]byte, error) {
return f.MarshalJSON()
}
func (f FuzzyQuery) MarshalJSON() ([]byte, error) {
if f.IsEmpty() {
return dynamic.Null, nil
}
data, err := f.marshalClauseJSON()
if err != nil {
return nil, err
}
return json.Marshal(dynamic.Map{f.field: data})
}
func (f FuzzyQuery) marshalClauseJSON() (dynamic.JSON, error) {
params, err := marshalClauseParams(&f)
if err != nil {
return nil, err
}
params["value"] = f.value
return json.Marshal(params)
}
func (f *FuzzyQuery) UnmarshalBSON(data []byte) error {
return f.UnmarshalJSON(data)
}
func (f *FuzzyQuery) UnmarshalJSON(data []byte) error {
*f = FuzzyQuery{}
d := map[string]dynamic.JSON{}
err := json.Unmarshal(data, &d)
if err != nil {
return err
}
for k, v := range d {
f.field = k
return f.unmarshalClauseJSON(v)
}
return nil
}
func (f *FuzzyQuery) unmarshalClauseJSON(data dynamic.JSON) error {
fields, err := unmarshalClauseParams(data, f)
if err != nil {
return err
}
if v, ok := fields["value"]; ok {
var value string
err := json.Unmarshal(v, &value)
if err != nil {
return err
}
f.value = value
}
return nil
}
func (f *FuzzyQuery) Clear() {
*f = FuzzyQuery{}
}