-
Notifications
You must be signed in to change notification settings - Fork 57
/
query_match.go
253 lines (216 loc) · 7.72 KB
/
query_match.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
249
250
251
252
253
package esquery
import (
"github.com/fatih/structs"
)
type matchType uint8
const (
// TypeMatch denotes a query of type "match"
TypeMatch matchType = iota
// TypeMatchBool denotes a query of type "match_bool_prefix"
TypeMatchBoolPrefix
// TypeMatchPhrase denotes a query of type "match_phrase"
TypeMatchPhrase
// TypeMatchPhrasePrefix denotes a query of type "match_phrase_prefix"
TypeMatchPhrasePrefix
)
// MatchQuery represents a query of type "match", "match_bool_prefix",
// "match_phrase" and "match_phrase_prefix". While all four share the same
// general structure, they don't necessarily support all the same options. The
// library does not attempt to verify provided options are supported.
// See the ElasticSearch documentation for more information:
// - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
// - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-bool-prefix-query.html
// - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
// - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html
type MatchQuery struct {
field string
mType matchType
params matchParams
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *MatchQuery) Map() map[string]interface{} {
var mType string
switch q.mType {
case TypeMatch:
mType = "match"
case TypeMatchBoolPrefix:
mType = "match_bool_prefix"
case TypeMatchPhrase:
mType = "match_phrase"
case TypeMatchPhrasePrefix:
mType = "match_phrase_prefix"
}
return map[string]interface{}{
mType: map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}
type matchParams struct {
Qry interface{} `structs:"query"`
Anl string `structs:"analyzer,omitempty"`
AutoGenerate *bool `structs:"auto_generate_synonyms_phrase_query,omitempty"`
Fuzz string `structs:"fuzziness,omitempty"`
MaxExp uint16 `structs:"max_expansions,omitempty"`
PrefLen uint16 `structs:"prefix_length,omitempty"`
Trans *bool `structs:"transpositions,omitempty"`
FuzzyRw string `structs:"fuzzy_rewrite,omitempty"`
Lent bool `structs:"lenient,omitempty"`
Op MatchOperator `structs:"operator,string,omitempty"`
MinMatch string `structs:"minimum_should_match,omitempty"`
ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"`
Slp uint16 `structs:"slop,omitempty"` // only relevant for match_phrase query
}
// Match creates a new query of type "match" with the provided field name.
// A comparison value can optionally be provided to quickly create a simple
// query such as { "match": { "message": "this is a test" } }
func Match(fieldName string, simpleQuery ...interface{}) *MatchQuery {
return newMatch(TypeMatch, fieldName, simpleQuery...)
}
// MatchBoolPrefix creates a new query of type "match_bool_prefix" with the
// provided field name. A comparison value can optionally be provided to quickly
// create a simple query such as { "match": { "message": "this is a test" } }
func MatchBoolPrefix(fieldName string, simpleQuery ...interface{}) *MatchQuery {
return newMatch(TypeMatchBoolPrefix, fieldName, simpleQuery...)
}
// MatchPhrase creates a new query of type "match_phrase" with the
// provided field name. A comparison value can optionally be provided to quickly
// create a simple query such as { "match": { "message": "this is a test" } }
func MatchPhrase(fieldName string, simpleQuery ...interface{}) *MatchQuery {
return newMatch(TypeMatchPhrase, fieldName, simpleQuery...)
}
// MatchPhrasePrefix creates a new query of type "match_phrase_prefix" with the
// provided field name. A comparison value can optionally be provided to quickly
// create a simple query such as { "match": { "message": "this is a test" } }
func MatchPhrasePrefix(fieldName string, simpleQuery ...interface{}) *MatchQuery {
return newMatch(TypeMatchPhrasePrefix, fieldName, simpleQuery...)
}
func newMatch(mType matchType, fieldName string, simpleQuery ...interface{}) *MatchQuery {
var qry interface{}
if len(simpleQuery) > 0 {
qry = simpleQuery[len(simpleQuery)-1]
}
return &MatchQuery{
field: fieldName,
mType: mType,
params: matchParams{
Qry: qry,
},
}
}
// Query sets the data to find in the query's field (it is the "query" component
// of the query).
func (q *MatchQuery) Query(data interface{}) *MatchQuery {
q.params.Qry = data
return q
}
// Analyzer sets the analyzer used to convert the text in the "query" value into
// tokens.
func (q *MatchQuery) Analyzer(a string) *MatchQuery {
q.params.Anl = a
return q
}
// AutoGenerateSynonymsPhraseQuery sets the "auto_generate_synonyms_phrase_query"
// boolean.
func (q *MatchQuery) AutoGenerateSynonymsPhraseQuery(b bool) *MatchQuery {
q.params.AutoGenerate = &b
return q
}
// Fuzziness set the maximum edit distance allowed for matching.
func (q *MatchQuery) Fuzziness(f string) *MatchQuery {
q.params.Fuzz = f
return q
}
// MaxExpansions sets the maximum number of terms to which the query will expand.
func (q *MatchQuery) MaxExpansions(e uint16) *MatchQuery {
q.params.MaxExp = e
return q
}
// PrefixLength sets the number of beginning characters left unchanged for fuzzy
// matching.
func (q *MatchQuery) PrefixLength(l uint16) *MatchQuery {
q.params.PrefLen = l
return q
}
// Transpositions sets whether edits for fuzzy matching include transpositions
// of two adjacent characters.
func (q *MatchQuery) Transpositions(b bool) *MatchQuery {
q.params.Trans = &b
return q
}
// FuzzyRewrite sets the method used to rewrite the query.
func (q *MatchQuery) FuzzyRewrite(s string) *MatchQuery {
q.params.FuzzyRw = s
return q
}
// Lenient sets whether format-based errors should be ignored.
func (q *MatchQuery) Lenient(b bool) *MatchQuery {
q.params.Lent = b
return q
}
// Operator sets the boolean logic used to interpret text in the query value.
func (q *MatchQuery) Operator(op MatchOperator) *MatchQuery {
q.params.Op = op
return q
}
// MinimumShouldMatch sets the minimum number of clauses that must match for a
// document to be returned.
func (q *MatchQuery) MinimumShouldMatch(s string) *MatchQuery {
q.params.MinMatch = s
return q
}
// Slop sets the maximum number of positions allowed between matching tokens.
func (q *MatchQuery) Slop(n uint16) *MatchQuery {
q.params.Slp = n
return q
}
// ZeroTermsQuery sets the "zero_terms_query" option to use. This indicates
// whether no documents are returned if the analyzer removes all tokens, such as
// when using a stop filter.
func (q *MatchQuery) ZeroTermsQuery(s ZeroTerms) *MatchQuery {
q.params.ZeroTerms = s
return q
}
// MatchOperator is an enumeration type representing supported values for a
// match query's "operator" parameter.
type MatchOperator uint8
const (
// OperatorOr is the "or" operator
OperatorOr MatchOperator = iota
// OperatorAnd is the "and" operator
OperatorAnd
)
// String returns a string representation of the match operator, as known to
// ElasticSearch.
func (a MatchOperator) String() string {
switch a {
case OperatorOr:
return "OR"
case OperatorAnd:
return "AND"
default:
return ""
}
}
// ZeroTerms is an enumeration type representing supported values for a match
// query's "zero_terms_query" parameter.
type ZeroTerms uint8
const (
// ZeroTermsNone is the "none" value
ZeroTermsNone ZeroTerms = iota
// ZeroTermsAll is the "all" value
ZeroTermsAll
)
// String returns a string representation of the zero_terms_query parameter, as
// known to ElasticSearch.
func (a ZeroTerms) String() string {
switch a {
case ZeroTermsNone:
return "none"
case ZeroTermsAll:
return "all"
default:
return ""
}
}