forked from aquasecurity/esquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_multi_match.go
211 lines (179 loc) · 5.87 KB
/
query_multi_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
package query
import (
"github.com/fatih/structs"
)
type MultiMatchQuery struct {
params multiMatchParams
}
// Map returns a map representation of the query; implementing the
// Mappable interface.
func (q *MultiMatchQuery) Map() map[string]interface{} {
return map[string]interface{}{
"multi_match": structs.Map(q.params),
}
}
type multiMatchParams struct {
Qry interface{} `structs:"query"`
Fields []string `structs:"fields"`
Type MultiMatchType `structs:"type,string,omitempty"`
TieBrk float32 `structs:"tie_breaker,omitempty"`
Boost float32 `structs:"boost,omitempty"`
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"`
Name string `structs:"_name,omitempty"`
}
// MultiMatch creates a new query of type "multi_match"
func MultiMatch(fieldNames []string, simpleQuery ...interface{}) *MultiMatchQuery {
return newMultiMatch(fieldNames, simpleQuery...)
}
func newMultiMatch(fieldNames []string, simpleQuery ...interface{}) *MultiMatchQuery {
var qry interface{}
if len(simpleQuery) > 0 {
qry = simpleQuery[len(simpleQuery)-1]
}
return &MultiMatchQuery{
params: multiMatchParams{
Fields: fieldNames,
Qry: qry,
},
}
}
// Query sets the data to find in the query's field (it is the "query" component
// of the query).
func (q *MultiMatchQuery) Query(data interface{}) *MultiMatchQuery {
q.params.Qry = data
return q
}
// Analyzer sets the analyzer used to convert the text in the "query" value into
// tokens.
func (q *MultiMatchQuery) Analyzer(a string) *MultiMatchQuery {
q.params.Anl = a
return q
}
// AutoGenerateSynonymsPhraseQuery sets the "auto_generate_synonyms_phrase_query"
// boolean.
func (q *MultiMatchQuery) AutoGenerateSynonymsPhraseQuery(b bool) *MultiMatchQuery {
q.params.AutoGenerate = &b
return q
}
// Fuzziness set the maximum edit distance allowed for matching.
func (q *MultiMatchQuery) Fuzziness(f string) *MultiMatchQuery {
q.params.Fuzz = f
return q
}
// MaxExpansions sets the maximum number of terms to which the query will expand.
func (q *MultiMatchQuery) MaxExpansions(e uint16) *MultiMatchQuery {
q.params.MaxExp = e
return q
}
// PrefixLength sets the number of beginning characters left unchanged for fuzzy
// matching.
func (q *MultiMatchQuery) PrefixLength(l uint16) *MultiMatchQuery {
q.params.PrefLen = l
return q
}
// TieBreaker
func (q *MultiMatchQuery) TieBreaker(l float32) *MultiMatchQuery {
q.params.TieBrk = l
return q
}
// Boost
func (q *MultiMatchQuery) Boost(l float32) *MultiMatchQuery {
q.params.Boost = l
return q
}
// Transpositions sets whether edits for fuzzy matching include transpositions
// of two adjacent characters.
func (q *MultiMatchQuery) Transpositions(b bool) *MultiMatchQuery {
q.params.Trans = &b
return q
}
// FuzzyRewrite sets the method used to rewrite the query.
func (q *MultiMatchQuery) FuzzyRewrite(s string) *MultiMatchQuery {
q.params.FuzzyRw = s
return q
}
// Lenient sets whether format-based errors should be ignored.
func (q *MultiMatchQuery) Lenient(b bool) *MultiMatchQuery {
q.params.Lent = &b
return q
}
// Operator sets the boolean logic used to interpret text in the query value.
func (q *MultiMatchQuery) Operator(op MatchOperator) *MultiMatchQuery {
q.params.Op = op
return q
}
// Type sets the query type
func (q *MultiMatchQuery) Type(t MultiMatchType) *MultiMatchQuery {
q.params.Type = t
return q
}
// MinimumShouldMatch sets the minimum number of clauses that must match for a
// document to be returned.
func (q *MultiMatchQuery) MinimumShouldMatch(s string) *MultiMatchQuery {
q.params.MinMatch = s
return q
}
// Slop sets the maximum number of positions allowed between matching tokens.
func (q *MultiMatchQuery) Slop(n uint16) *MultiMatchQuery {
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 *MultiMatchQuery) ZeroTermsQuery(s ZeroTerms) *MultiMatchQuery {
q.params.ZeroTerms = s
return q
}
func (q *MultiMatchQuery) Name(name string) Mappable {
q.params.Name = name
return q
}
// MatchType is an enumeration type representing supported values for a
// multi match query's "type" parameter.
type MultiMatchType uint8
const (
// TypeBestFields is the "best_fields" type
MatchTypeBestFields MultiMatchType = iota
// TypeMostFields is the "most_fields" type
MatchTypeMostFields
// TypeMostFields is the "cross_fields" type
MatchTypeCrossFields
// TypeMostFields is the "phrase" type
MatchTypePhrase
// TypeMostFields is the "phrase_prefix" type
MatchTypePhrasePrefix
// TypeMostFields is the "bool_prefix" type
MatchTypeBoolPrefix
)
// String returns a string representation of the match operator, as known to
// ElasticSearch.
func (a MultiMatchType) String() string {
switch a {
case MatchTypeBestFields:
return "best_fields"
case MatchTypeMostFields:
return "most_fields"
case MatchTypeCrossFields:
return "cross_fields"
case MatchTypePhrase:
return "phrase"
case MatchTypePhrasePrefix:
return "phrase_prefix"
case MatchTypeBoolPrefix:
return "bool_prefix"
default:
return ""
}
}