forked from aquasecurity/esquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_term_level.go
562 lines (477 loc) · 15.2 KB
/
query_term_level.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package query
import (
"github.com/fatih/structs"
)
// ExistsQuery represents a query of type "exists", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
type ExistsQuery struct {
// Field is the name of the field to check for existence
Field string `structs:"field"`
}
// Exists creates a new query of type "exists" on the provided field.
func Exists(field string) *ExistsQuery {
return &ExistsQuery{field}
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *ExistsQuery) Map() map[string]interface{} {
return map[string]interface{}{
"exists": structs.Map(q),
}
}
//----------------------------------------------------------------------------//
// IDsQuery represents a query of type "ids", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
type IDsQuery struct {
// IDs is the "ids" component of the query
IDs struct {
// Values is the list of ID values
Values []string `structs:"values"`
} `structs:"ids"`
}
// IDs creates a new query of type "ids" with the provided values.
func IDs(vals ...string) *IDsQuery {
q := &IDsQuery{}
q.IDs.Values = vals
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *IDsQuery) Map() map[string]interface{} {
return structs.Map(q)
}
//----------------------------------------------------------------------------//
// PrefixQuery represents query of type "prefix", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
type PrefixQuery struct {
field string
params prefixQueryParams
}
type prefixQueryParams struct {
// Value is the prefix value to look for
Value string `structs:"value"`
// Rewrite is the method used to rewrite the query
Rewrite string `structs:"rewrite,omitempty"`
}
// Prefix creates a new query of type "prefix", on the provided field and using
// the provided prefix value.
func Prefix(field, value string) *PrefixQuery {
return &PrefixQuery{
field: field,
params: prefixQueryParams{Value: value},
}
}
// Rewrite sets the rewrite method for the query
func (q *PrefixQuery) Rewrite(s string) *PrefixQuery {
q.params.Rewrite = s
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *PrefixQuery) Map() map[string]interface{} {
return map[string]interface{}{
"prefix": map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}
//----------------------------------------------------------------------------//
// RangeQuery represents a query of type "range", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
type RangeQuery struct {
field string
params rangeQueryParams
}
type rangeQueryParams struct {
Gt interface{} `structs:"gt,omitempty"`
Gte interface{} `structs:"gte,omitempty"`
Lt interface{} `structs:"lt,omitempty"`
Lte interface{} `structs:"lte,omitempty"`
Format string `structs:"format,omitempty"`
Relation RangeRelation `structs:"relation,string,omitempty"`
TimeZone string `structs:"time_zone,omitempty"`
Boost float32 `structs:"boost,omitempty"`
}
// Range creates a new query of type "range" on the provided field
func Range(field string) *RangeQuery {
return &RangeQuery{field: field}
}
// Gt sets that the value of field must be greater than the provided value
func (a *RangeQuery) Gt(val interface{}) *RangeQuery {
a.params.Gt = val
return a
}
// Gt sets that the value of field must be greater than or equal to the provided
// value
func (a *RangeQuery) Gte(val interface{}) *RangeQuery {
a.params.Gte = val
return a
}
// Lt sets that the value of field must be lower than the provided value
func (a *RangeQuery) Lt(val interface{}) *RangeQuery {
a.params.Lt = val
return a
}
// Lte sets that the value of field must be lower than or equal to the provided
// value
func (a *RangeQuery) Lte(val interface{}) *RangeQuery {
a.params.Lte = val
return a
}
// Format sets the date format for date values
func (a *RangeQuery) Format(f string) *RangeQuery {
a.params.Format = f
return a
}
// Relation sets how the query matches values for range fields
func (a *RangeQuery) Relation(r RangeRelation) *RangeQuery {
a.params.Relation = r
return a
}
// TimeZone sets the time zone used for date values.
func (a *RangeQuery) TimeZone(zone string) *RangeQuery {
a.params.TimeZone = zone
return a
}
// Boost sets the boost value of the query.
func (a *RangeQuery) Boost(b float32) *RangeQuery {
a.params.Boost = b
return a
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (a *RangeQuery) Map() map[string]interface{} {
return map[string]interface{}{
"range": map[string]interface{}{
a.field: structs.Map(a.params),
},
}
}
// RangeRelation is an enumeration type for a range query's "relation" field
type RangeRelation uint8
const (
_ RangeRelation = iota
// RangeIntersects is the "INTERSECTS" relation
RangeIntersects
// RangeContains is the "CONTAINS" relation
RangeContains
// RangeWithin is the "WITHIN" relation
RangeWithin
)
// String returns a string representation of the RangeRelation value, as
// accepted by ElasticSearch
func (a RangeRelation) String() string {
switch a {
case RangeIntersects:
return "INTERSECTS"
case RangeContains:
return "CONTAINS"
case RangeWithin:
return "WITHIN"
default:
return ""
}
}
//----------------------------------------------------------------------------//
// RegexpQuery represents a query of type "regexp", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
type RegexpQuery struct {
field string
wildcard bool
params regexpQueryParams
}
type regexpQueryParams struct {
Value string `structs:"value"`
Flags string `structs:"flags,omitempty"`
MaxDeterminizedStates uint16 `structs:"max_determinized_states,omitempty"`
Rewrite string `structs:"rewrite,omitempty"`
}
// Regexp creates a new query of type "regexp" on the provided field and using
// the provided regular expression.
func Regexp(field, value string) *RegexpQuery {
return &RegexpQuery{
field: field,
params: regexpQueryParams{
Value: value,
},
}
}
// Value changes the regular expression value of the query.
func (q *RegexpQuery) Value(v string) *RegexpQuery {
q.params.Value = v
return q
}
// Flags sets the regular expression's optional flags.
func (q *RegexpQuery) Flags(f string) *RegexpQuery {
if !q.wildcard {
q.params.Flags = f
}
return q
}
// MaxDeterminizedStates sets the maximum number of automaton states required
// for the query.
func (q *RegexpQuery) MaxDeterminizedStates(m uint16) *RegexpQuery {
if !q.wildcard {
q.params.MaxDeterminizedStates = m
}
return q
}
// Rewrite sets the method used to rewrite the query.
func (q *RegexpQuery) Rewrite(r string) *RegexpQuery {
q.params.Rewrite = r
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *RegexpQuery) Map() map[string]interface{} {
var qType string
if q.wildcard {
qType = "wildcard"
} else {
qType = "regexp"
}
return map[string]interface{}{
qType: map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}
//----------------------------------------------------------------------------//
// Wildcard creates a new query of type "wildcard" on the provided field and
// using the provided regular expression value. Internally, wildcard queries
// are simply specialized RegexpQuery values.
// Wildcard queries are described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html
func Wildcard(field, value string) *RegexpQuery {
return &RegexpQuery{
field: field,
wildcard: true,
params: regexpQueryParams{
Value: value,
},
}
}
//----------------------------------------------------------------------------//
// FuzzyQuery represents a query of type "fuzzy", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
type FuzzyQuery struct {
field string
params fuzzyQueryParams
}
type fuzzyQueryParams struct {
Value string `structs:"value"`
Fuzziness string `structs:"fuzziness,omitempty"`
MaxExpansions uint16 `structs:"max_expansions,omitempty"`
PrefixLength uint16 `structs:"prefix_length,omitempty"`
Transpositions *bool `structs:"transpositions,omitempty"`
Rewrite string `structs:"rewrite,omitempty"`
}
// Fuzzy creates a new query of type "fuzzy" on the provided field and using
// the provided value
func Fuzzy(field, value string) *FuzzyQuery {
return &FuzzyQuery{
field: field,
params: fuzzyQueryParams{
Value: value,
},
}
}
// Value sets the value of the query.
func (q *FuzzyQuery) Value(val string) *FuzzyQuery {
q.params.Value = val
return q
}
// Fuzziness sets the maximum edit distance allowed for matching.
func (q *FuzzyQuery) Fuzziness(fuzz string) *FuzzyQuery {
q.params.Fuzziness = fuzz
return q
}
// MaxExpansions sets the maximum number of variations created.
func (q *FuzzyQuery) MaxExpansions(m uint16) *FuzzyQuery {
q.params.MaxExpansions = m
return q
}
// PrefixLength sets the number of beginning characters left unchanged when
// creating expansions
func (q *FuzzyQuery) PrefixLength(l uint16) *FuzzyQuery {
q.params.PrefixLength = l
return q
}
// Transpositions sets whether edits include transpositions of two adjacent
// characters.
func (q *FuzzyQuery) Transpositions(b bool) *FuzzyQuery {
q.params.Transpositions = &b
return q
}
// Rewrite sets the method used to rewrite the query.
func (q *FuzzyQuery) Rewrite(s string) *FuzzyQuery {
q.params.Rewrite = s
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *FuzzyQuery) Map() map[string]interface{} {
return map[string]interface{}{
"fuzzy": map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}
//----------------------------------------------------------------------------//
// TermQuery represents a query of type "term", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
type TermQuery struct {
field string
params termQueryParams
}
type termQueryParams struct {
Value interface{} `structs:"value"`
Boost float32 `structs:"boost,omitempty"`
}
// Term creates a new query of type "term" on the provided field and using the
// provide value
func Term(field string, value interface{}) *TermQuery {
return &TermQuery{
field: field,
params: termQueryParams{
Value: value,
},
}
}
// Value sets the term value for the query.
func (q *TermQuery) Value(val interface{}) *TermQuery {
q.params.Value = val
return q
}
// Boost sets the boost value of the query.
func (q *TermQuery) Boost(b float32) *TermQuery {
q.params.Boost = b
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *TermQuery) Map() map[string]interface{} {
return map[string]interface{}{
"term": map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}
//----------------------------------------------------------------------------//
// TermsQuery represents a query of type "terms", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
type TermsQuery struct {
field string
values []interface{}
boost float32
}
// Terms creates a new query of type "terms" on the provided field, and
// optionally with the provided term values.
func Terms(field string, values ...interface{}) *TermsQuery {
return &TermsQuery{
field: field,
values: values,
}
}
// Values sets the term values for the query.
func (q *TermsQuery) Values(values ...interface{}) *TermsQuery {
q.values = values
return q
}
// Boost sets the boost value of the query.
func (q *TermsQuery) Boost(b float32) *TermsQuery {
q.boost = b
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q TermsQuery) Map() map[string]interface{} {
innerMap := map[string]interface{}{q.field: q.values}
if q.boost > 0 {
innerMap["boost"] = q.boost
}
return map[string]interface{}{"terms": innerMap}
}
//----------------------------------------------------------------------------//
// TermsQuery represents a query of type "terms" with string values, as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
type StringTermsQuery struct {
field string
values []string
boost float32
}
// Terms creates a new query of type "terms" on the provided field, and
// optionally with the provided term values.
func StringTerms(field string, values ...string) *StringTermsQuery {
return &StringTermsQuery{
field: field,
values: values,
}
}
// Values sets the term values for the query.
func (q *StringTermsQuery) Values(values ...string) *StringTermsQuery {
q.values = values
return q
}
// Boost sets the boost value of the query.
func (q *StringTermsQuery) Boost(b float32) *StringTermsQuery {
q.boost = b
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q *StringTermsQuery) Map() map[string]interface{} {
innerMap := map[string]any{q.field: q.values}
if q.boost > 0 {
innerMap["boost"] = q.boost
}
return map[string]interface{}{"terms": innerMap}
}
//----------------------------------------------------------------------------//
// TermsSetQuery represents a query of type "terms_set", as described in:
// https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
type TermsSetQuery struct {
field string
params termsSetQueryParams
}
type termsSetQueryParams struct {
Terms []string `structs:"terms"`
MinimumShouldMatchField string `structs:"minimum_should_match_field,omitempty"`
MinimumShouldMatchScript string `structs:"minimum_should_match_script,omitempty"`
}
// TermsSet creates a new query of type "terms_set" on the provided field and
// optionally using the provided terms.
func TermsSet(field string, terms ...string) *TermsSetQuery {
return &TermsSetQuery{
field: field,
params: termsSetQueryParams{
Terms: terms,
},
}
}
// Terms sets the terms for the query.
func (q *TermsSetQuery) Terms(terms ...string) *TermsSetQuery {
q.params.Terms = terms
return q
}
// MinimumShouldMatchField sets the name of the field containing the number of
// matching terms required to return a document.
func (q *TermsSetQuery) MinimumShouldMatchField(field string) *TermsSetQuery {
q.params.MinimumShouldMatchField = field
return q
}
// MinimumShouldMatchScript sets the custom script containing the number of
// matching terms required to return a document.
func (q *TermsSetQuery) MinimumShouldMatchScript(script string) *TermsSetQuery {
q.params.MinimumShouldMatchScript = script
return q
}
// Map returns a map representation of the query, thus implementing the
// Mappable interface.
func (q TermsSetQuery) Map() map[string]interface{} {
return map[string]interface{}{
"terms_set": map[string]interface{}{
q.field: structs.Map(q.params),
},
}
}