/
queries.go
1029 lines (880 loc) · 25.7 KB
/
queries.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package search
import (
"encoding/json"
)
// Query represents a search query.
type Query interface {
}
// MatchOperator defines how the individual match terms should be logically concatenated.
type MatchOperator string
const (
// MatchOperatorOr specifies that individual match terms are concatenated with a logical OR - this is the default if not provided.
MatchOperatorOr MatchOperator = "or"
// MatchOperatorAnd specifies that individual match terms are concatenated with a logical AND.
MatchOperatorAnd MatchOperator = "and"
)
// MatchQuery represents a search match query.
type MatchQuery struct {
match string
field *string
analyzer *string
prefixLength *uint64
fuzziness *uint64
boost *float32
operator *MatchOperator
}
// marshal's query to JSON for use with search REST API.
func (q MatchQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Match string `json:"match"`
Field *string `json:"field,omitempty"`
Analyzer *string `json:"analyzer,omitempty"`
PrefixLength *uint64 `json:"prefix_length,omitempty"`
Fuzziness *uint64 `json:"fuzziness,omitempty"`
Boost *float32 `json:"boost,omitempty"`
Operator *string `json:"operator,omitempty"`
}{
Match: q.match,
Field: q.field,
Analyzer: q.analyzer,
PrefixLength: q.prefixLength,
Fuzziness: q.fuzziness,
Boost: q.boost,
}
if q.operator != nil {
operator := string(*q.operator)
outStruct.Operator = &operator
}
return json.Marshal(outStruct)
}
// NewMatchQuery creates a new MatchQuery.
func NewMatchQuery(match string) *MatchQuery {
q := &MatchQuery{match: match}
return q
}
// Field specifies the field for this query.
func (q *MatchQuery) Field(field string) *MatchQuery {
q.field = &field
return q
}
// Analyzer specifies the analyzer to use for this query.
func (q *MatchQuery) Analyzer(analyzer string) *MatchQuery {
q.analyzer = &analyzer
return q
}
// PrefixLength specifies the prefix length from this query.
func (q *MatchQuery) PrefixLength(length uint64) *MatchQuery {
q.prefixLength = &length
return q
}
// Fuzziness specifies the fuziness for this query.
func (q *MatchQuery) Fuzziness(fuzziness uint64) *MatchQuery {
q.fuzziness = &fuzziness
return q
}
// Boost specifies the boost for this query.
func (q *MatchQuery) Boost(boost float32) *MatchQuery {
q.boost = &boost
return q
}
// Operator defines how the individual match terms should be logically concatenated.
func (q *MatchQuery) Operator(operator MatchOperator) *MatchQuery {
// q.options["operator"] = string(operator)
q.operator = &operator
return q
}
// MatchPhraseQuery represents a search match phrase query.
type MatchPhraseQuery struct {
matchPhrase string
field *string
analyzer *string
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q MatchPhraseQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
MatchPhrase string `json:"match_phrase"`
Field *string `json:"field,omitempty"`
Analyzer *string `json:"analyzer,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
MatchPhrase: q.matchPhrase,
Field: q.field,
Analyzer: q.analyzer,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewMatchPhraseQuery creates a new MatchPhraseQuery
func NewMatchPhraseQuery(phrase string) *MatchPhraseQuery {
q := &MatchPhraseQuery{matchPhrase: phrase}
return q
}
// Field specifies the field for this query.
func (q *MatchPhraseQuery) Field(field string) *MatchPhraseQuery {
q.field = &field
return q
}
// Analyzer specifies the analyzer to use for this query.
func (q *MatchPhraseQuery) Analyzer(analyzer string) *MatchPhraseQuery {
q.analyzer = &analyzer
return q
}
// Boost specifies the boost for this query.
func (q *MatchPhraseQuery) Boost(boost float32) *MatchPhraseQuery {
q.boost = &boost
return q
}
// RegexpQuery represents a search regular expression query.
type RegexpQuery struct {
regexp string
field *string
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q RegexpQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Regexp string `json:"regexp"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost"`
}{
Regexp: q.regexp,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewRegexpQuery creates a new RegexpQuery.
func NewRegexpQuery(regexp string) *RegexpQuery {
q := &RegexpQuery{regexp: regexp}
return q
}
// Field specifies the field for this query.
func (q *RegexpQuery) Field(field string) *RegexpQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *RegexpQuery) Boost(boost float32) *RegexpQuery {
q.boost = &boost
return q
}
// QueryStringQuery represents a search string query.
type QueryStringQuery struct {
query string
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q QueryStringQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Query string `json:"query"`
Boost *float32 `json:"boost"`
}{
Query: q.query,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewQueryStringQuery creates a new StringQuery.
func NewQueryStringQuery(query string) *QueryStringQuery {
q := &QueryStringQuery{query: query}
return q
}
// Boost specifies the boost for this query.
func (q *QueryStringQuery) Boost(boost float32) *QueryStringQuery {
q.boost = &boost
return q
}
// NumericRangeQuery represents a search numeric range query.
type NumericRangeQuery struct {
min *float32
inclusiveMin *bool
max *float32
inclusiveMax *bool
field *string
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q NumericRangeQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Min *float32 `json:"min,omitempty"`
InclusiveMin *bool `json:"inclusive_min,omitempty"`
Max *float32 `json:"max,omitempty"`
InclusiveMax *bool `json:"inclusive_max,omitempty"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Min: q.min,
InclusiveMin: q.inclusiveMin,
Max: q.max,
InclusiveMax: q.inclusiveMax,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewNumericRangeQuery creates a new NumericRangeQuery.
func NewNumericRangeQuery() *NumericRangeQuery {
q := &NumericRangeQuery{}
return q
}
// Min specifies the minimum value and inclusiveness for this range query.
func (q *NumericRangeQuery) Min(min float32, inclusive bool) *NumericRangeQuery {
q.min = &min
q.inclusiveMin = &inclusive
return q
}
// Max specifies the maximum value and inclusiveness for this range query.
func (q *NumericRangeQuery) Max(max float32, inclusive bool) *NumericRangeQuery {
q.max = &max
q.inclusiveMax = &inclusive
return q
}
// Field specifies the field for this query.
func (q *NumericRangeQuery) Field(field string) *NumericRangeQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *NumericRangeQuery) Boost(boost float32) *NumericRangeQuery {
q.boost = &boost
return q
}
// DateRangeQuery represents a search date range query.
type DateRangeQuery struct {
start *string
inclusiveStart *bool
end *string
inclusiveEnd *bool
dateTimeParser *string
field *string
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q DateRangeQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Start *string `json:"start,omitempty"`
InclusiveStart *bool `json:"inclusive_start,omitempty"`
End *string `json:"end,omitempty"`
InclusiveEnd *bool `json:"inclusive_end,omitempty"`
DateTimeParser *string `json:"datetime_parser,omitempty"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Start: q.start,
InclusiveStart: q.inclusiveStart,
End: q.end,
InclusiveEnd: q.inclusiveEnd,
DateTimeParser: q.dateTimeParser,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewDateRangeQuery creates a new DateRangeQuery.
func NewDateRangeQuery() *DateRangeQuery {
q := &DateRangeQuery{}
return q
}
// Start specifies the start value and inclusiveness for this range query.
func (q *DateRangeQuery) Start(start string, inclusive bool) *DateRangeQuery {
q.start = &start
q.inclusiveStart = &inclusive
return q
}
// End specifies the end value and inclusiveness for this range query.
func (q *DateRangeQuery) End(end string, inclusive bool) *DateRangeQuery {
q.end = &end
q.inclusiveEnd = &inclusive
return q
}
// DateTimeParser specifies which date time string parser to use.
func (q *DateRangeQuery) DateTimeParser(parser string) *DateRangeQuery {
q.dateTimeParser = &parser
return q
}
// Field specifies the field for this query.
func (q *DateRangeQuery) Field(field string) *DateRangeQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *DateRangeQuery) Boost(boost float32) *DateRangeQuery {
q.boost = &boost
return q
}
// ConjunctionQuery represents a search conjunction query.
type ConjunctionQuery struct {
conjuncts []Query
boost *float32
}
// marshal's query to JSON for use with search REST API.
func (q ConjunctionQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Conjuncts []Query `json:"conjuncts"`
Boost *float32 `json:"boost,omitempty"`
}{
Conjuncts: q.conjuncts,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewConjunctionQuery creates a new ConjunctionQuery.
func NewConjunctionQuery(queries ...Query) *ConjunctionQuery {
q := &ConjunctionQuery{
conjuncts: make([]Query, 0),
}
return q.And(queries...)
}
// And adds new predicate queries to this conjunction query.
func (q *ConjunctionQuery) And(queries ...Query) *ConjunctionQuery {
q.conjuncts = append(q.conjuncts, queries...)
return q
}
// Boost specifies the boost for this query.
func (q *ConjunctionQuery) Boost(boost float32) *ConjunctionQuery {
q.boost = &boost
return q
}
// DisjunctionQuery represents a search disjunction query.
type DisjunctionQuery struct {
disjuncts []Query
boost *float32
min *uint32
}
// marshal's query to JSON for use with search REST API.
func (q DisjunctionQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Disjuncts []Query `json:"disjuncts"`
Boost *float32 `json:"boost,omitempty"`
Min *uint32 `json:"min,omitempty"`
}{
Disjuncts: q.disjuncts,
Boost: q.boost,
Min: q.min,
}
return json.Marshal(outStruct)
}
// NewDisjunctionQuery creates a new DisjunctionQuery.
func NewDisjunctionQuery(queries ...Query) *DisjunctionQuery {
q := &DisjunctionQuery{
disjuncts: make([]Query, 0),
}
return q.Or(queries...)
}
// Or adds new predicate queries to this disjunction query.
func (q *DisjunctionQuery) Or(queries ...Query) *DisjunctionQuery {
q.disjuncts = append(q.disjuncts, queries...)
return q
}
// Boost specifies the boost for this query.
func (q *DisjunctionQuery) Boost(boost float32) *DisjunctionQuery {
q.boost = &boost
return q
}
// Min specifies the minimum number of queries that a document must satisfy.
func (q *DisjunctionQuery) Min(min uint32) *DisjunctionQuery {
q.min = &min
return q
}
type booleanQueryData struct {
Must *ConjunctionQuery `json:"must,omitempty"`
Should *DisjunctionQuery `json:"should,omitempty"`
MustNot *DisjunctionQuery `json:"must_not,omitempty"`
Boost float32 `json:"boost,omitempty"`
}
// BooleanQuery represents a search boolean query.
type BooleanQuery struct {
data booleanQueryData
shouldMin uint32 // minimum value before the should query will boost.
}
// NewBooleanQuery creates a new BooleanQuery.
func NewBooleanQuery() *BooleanQuery {
q := &BooleanQuery{}
return q
}
// Must specifies a query which must match.
func (q *BooleanQuery) Must(query Query) *BooleanQuery {
switch val := query.(type) {
case ConjunctionQuery:
q.data.Must = &val
case *ConjunctionQuery:
q.data.Must = val
default:
q.data.Must = NewConjunctionQuery(val)
}
return q
}
// Should specifies a query which should match.
func (q *BooleanQuery) Should(query Query) *BooleanQuery {
switch val := query.(type) {
case DisjunctionQuery:
q.data.Should = &val
case *DisjunctionQuery:
q.data.Should = val
default:
q.data.Should = NewDisjunctionQuery(val)
}
return q
}
// MustNot specifies a query which must not match.
func (q *BooleanQuery) MustNot(query Query) *BooleanQuery {
switch val := query.(type) {
case DisjunctionQuery:
q.data.MustNot = &val
case *DisjunctionQuery:
q.data.MustNot = val
default:
q.data.MustNot = NewDisjunctionQuery(val)
}
return q
}
// ShouldMin specifies the minimum value before the should query will boost.
func (q *BooleanQuery) ShouldMin(min uint32) *BooleanQuery {
q.shouldMin = min
return q
}
// Boost specifies the boost for this query.
func (q *BooleanQuery) Boost(boost float32) *BooleanQuery {
q.data.Boost = boost
return q
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q BooleanQuery) MarshalJSON() ([]byte, error) {
if q.data.Should != nil {
q.data.Should.min = &q.shouldMin
}
bytes, err := json.Marshal(q.data)
if q.data.Should != nil {
q.data.Should.min = nil
}
return bytes, err
}
// WildcardQuery represents a search wildcard query.
type WildcardQuery struct {
wildcard string
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q WildcardQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Wildcard string `json:"wildcard"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Wildcard: q.wildcard,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewWildcardQuery creates a new WildcardQuery.
func NewWildcardQuery(wildcard string) *WildcardQuery {
q := &WildcardQuery{wildcard: wildcard}
return q
}
// Field specifies the field for this query.
func (q *WildcardQuery) Field(field string) *WildcardQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *WildcardQuery) Boost(boost float32) *WildcardQuery {
q.boost = &boost
return q
}
// DocIDQuery represents a search document id query.
type DocIDQuery struct {
ids []string
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q DocIDQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Ids []string `json:"ids"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Ids: q.ids,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewDocIDQuery creates a new DocIdQuery.
func NewDocIDQuery(ids ...string) *DocIDQuery {
q := &DocIDQuery{ids: make([]string, 0)}
return q.AddDocIds(ids...)
}
// AddDocIds adds addition document ids to this query.
func (q *DocIDQuery) AddDocIds(ids ...string) *DocIDQuery {
q.ids = append(q.ids, ids...)
return q
}
// Field specifies the field for this query.
func (q *DocIDQuery) Field(field string) *DocIDQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *DocIDQuery) Boost(boost float32) *DocIDQuery {
q.boost = &boost
return q
}
// BooleanFieldQuery represents a search boolean field query.
type BooleanFieldQuery struct {
val bool // required field
field *string // optional field
boost *float32 // optional field
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q BooleanFieldQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Val bool `json:"bool"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Val: q.val,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewBooleanFieldQuery creates a new BooleanFieldQuery.
func NewBooleanFieldQuery(val bool) *BooleanFieldQuery {
q := &BooleanFieldQuery{val: val}
return q
}
// Field specifies the field for this query.
func (q *BooleanFieldQuery) Field(field string) *BooleanFieldQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *BooleanFieldQuery) Boost(boost float32) *BooleanFieldQuery {
q.boost = &boost
return q
}
// TermQuery represents a search term query.
type TermQuery struct {
term string // required field
field *string
boost *float32
prefixLength *uint64
fuzziness *uint64
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q TermQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Term string `json:"term"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
PrefixLength *uint64 `json:"prefix_length,omitempty"`
Fuzziness *uint64 `json:"fuzziness,omitempty"`
}{
Term: q.term,
Field: q.field,
Boost: q.boost,
PrefixLength: q.prefixLength,
Fuzziness: q.fuzziness,
}
return json.Marshal(outStruct)
}
// NewTermQuery creates a new TermQuery.
func NewTermQuery(term string) *TermQuery {
q := &TermQuery{term: term}
return q
}
// Field specifies the field for this query.
func (q *TermQuery) Field(field string) *TermQuery {
q.field = &field
return q
}
// PrefixLength specifies the prefix length from this query.
func (q *TermQuery) PrefixLength(length uint64) *TermQuery {
q.prefixLength = &length
return q
}
// Fuzziness specifies the fuziness for this query.
func (q *TermQuery) Fuzziness(fuzziness uint64) *TermQuery {
q.fuzziness = &fuzziness
return q
}
// Boost specifies the boost for this query.
func (q *TermQuery) Boost(boost float32) *TermQuery {
q.boost = &boost
return q
}
// PhraseQuery represents a search phrase query.
type PhraseQuery struct {
terms []string
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q PhraseQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Terms []string `json:"terms"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Terms: q.terms,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewPhraseQuery creates a new PhraseQuery.
func NewPhraseQuery(terms ...string) *PhraseQuery {
q := &PhraseQuery{terms: terms}
return q
}
// Field specifies the field for this query.
func (q *PhraseQuery) Field(field string) *PhraseQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *PhraseQuery) Boost(boost float32) *PhraseQuery {
q.boost = &boost
return q
}
// PrefixQuery represents a search prefix query.
type PrefixQuery struct {
prefix string
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q PrefixQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Prefix string `json:"prefix"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Prefix: q.prefix,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewPrefixQuery creates a new PrefixQuery.
func NewPrefixQuery(prefix string) *PrefixQuery {
q := &PrefixQuery{prefix: prefix}
return q
}
// Field specifies the field for this query.
func (q *PrefixQuery) Field(field string) *PrefixQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *PrefixQuery) Boost(boost float32) *PrefixQuery {
q.boost = &boost
return q
}
// MatchAllQuery represents a search match all query.
type MatchAllQuery struct{}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q MatchAllQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
MatchAll *struct{} `json:"match_all"`
}{}
return json.Marshal(outStruct)
}
// NewMatchAllQuery creates a new MatchAllQuery.
func NewMatchAllQuery() *MatchAllQuery {
q := &MatchAllQuery{}
return q
}
// MatchNoneQuery represents a search match none query.
type MatchNoneQuery struct{}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q MatchNoneQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
MatchAll *struct{} `json:"match_none"`
}{}
return json.Marshal(outStruct)
}
// NewMatchNoneQuery creates a new MatchNoneQuery.
func NewMatchNoneQuery() *MatchNoneQuery {
q := &MatchNoneQuery{}
return q
}
// TermRangeQuery represents a search term range query.
type TermRangeQuery struct {
term string
field *string
min *string
inclusiveMin *bool
max *string
inclusiveMax *bool
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q TermRangeQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Term string `json:"term"`
Field *string `json:"field,omitempty"`
Min *string `json:"min,omitempty"`
InclusiveMin *bool `json:"inclusive_min,omitempty"`
Max *string `json:"max,omitempty"`
InclusiveMax *bool `json:"inclusive_max,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Term: q.term,
Field: q.field,
Min: q.min,
InclusiveMin: q.inclusiveMin,
Max: q.max,
InclusiveMax: q.inclusiveMax,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewTermRangeQuery creates a new TermRangeQuery.
func NewTermRangeQuery(term string) *TermRangeQuery {
q := &TermRangeQuery{term: term}
return q
}
// Field specifies the field for this query.
func (q *TermRangeQuery) Field(field string) *TermRangeQuery {
q.field = &field
return q
}
// Min specifies the minimum value and inclusiveness for this range query.
func (q *TermRangeQuery) Min(min string, inclusive bool) *TermRangeQuery {
q.min = &min
q.inclusiveMin = &inclusive
return q
}
// Max specifies the maximum value and inclusiveness for this range query.
func (q *TermRangeQuery) Max(max string, inclusive bool) *TermRangeQuery {
q.max = &max
q.inclusiveMax = &inclusive
return q
}
// Boost specifies the boost for this query.
func (q *TermRangeQuery) Boost(boost float32) *TermRangeQuery {
q.boost = &boost
return q
}
// GeoDistanceQuery represents a search geographical distance query.
type GeoDistanceQuery struct {
location []float64
distance string
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q GeoDistanceQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
Location []float64 `json:"location"`
Distance string `json:"distance"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
Location: q.location,
Distance: q.distance,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewGeoDistanceQuery creates a new GeoDistanceQuery.
func NewGeoDistanceQuery(lon, lat float64, distance string) *GeoDistanceQuery {
q := &GeoDistanceQuery{location: []float64{lon, lat}, distance: distance}
return q
}
// Field specifies the field for this query.
func (q *GeoDistanceQuery) Field(field string) *GeoDistanceQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *GeoDistanceQuery) Boost(boost float32) *GeoDistanceQuery {
q.boost = &boost
return q
}
// GeoBoundingBoxQuery represents a search geographical bounding box query.
type GeoBoundingBoxQuery struct {
topLeft []float64
bottomRight []float64
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q GeoBoundingBoxQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
TopLeft []float64 `json:"top_left"`
BottomRight []float64 `json:"bottom_right"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{
TopLeft: q.topLeft,
BottomRight: q.bottomRight,
Field: q.field,
Boost: q.boost,
}
return json.Marshal(outStruct)
}
// NewGeoBoundingBoxQuery creates a new GeoBoundingBoxQuery.
func NewGeoBoundingBoxQuery(tlLon, tlLat, brLon, brLat float64) *GeoBoundingBoxQuery {
q := &GeoBoundingBoxQuery{topLeft: []float64{tlLon, tlLat}, bottomRight: []float64{brLon, brLat}}
return q
}
// Field specifies the field for this query.
func (q *GeoBoundingBoxQuery) Field(field string) *GeoBoundingBoxQuery {
q.field = &field
return q
}
// Boost specifies the boost for this query.
func (q *GeoBoundingBoxQuery) Boost(boost float32) *GeoBoundingBoxQuery {
q.boost = &boost
return q
}
// Coordinate is a tuple of a latitude and a longitude.
type Coordinate struct {
Lon float64
Lat float64
}
// GeoPolygonQuery represents a search query which allows to match inside a geo polygon.
type GeoPolygonQuery struct {
polyPoints [][]float64
field *string
boost *float32
}
// MarshalJSON marshal's this query to JSON for the search REST API.
func (q GeoPolygonQuery) MarshalJSON() ([]byte, error) {
outStruct := &struct {
PolyPoints [][]float64 `json:"polygon_points"`
Field *string `json:"field,omitempty"`
Boost *float32 `json:"boost,omitempty"`
}{