-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.go
315 lines (277 loc) · 6.02 KB
/
expr.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
package lucener
import "github.com/gocql/gocql"
import "encoding/json"
type Rule interface {
}
// type Query struct {
// }
// type Filter struct {
// field string
// Type string
// value interface{}
// }
type Sort struct {
Field string `json:"field,omitempty"`
Reverse bool `json:"reverse,omitempty"`
}
type Expr struct {
Q []Rule `json:"query,omitempty"`
F []Rule `json:"filter,omitempty"`
S []*Sort `json:"sort,omitempty"`
R bool `json:"refresh,omitempty"`
}
type value struct {
Type string `json:"type"`
Field string `json:"field"`
Value interface{} `json:"value"`
}
type values struct {
Type string `json:"type"`
Field string `json:"field"`
Values interface{} `json:"values"`
}
// Match query
func Match(f string, v interface{}) Rule {
return &value{
Type: "match",
Field: f,
Value: v,
}
}
// Prefix query
func Prefix(f string, v interface{}) Rule {
return &value{
Type: "prefix",
Field: f,
Value: v,
}
}
type allQuery struct {
Type string `json:"type"`
}
// All selects all indexed rows
func All() Rule {
return &allQuery{
Type: "all",
}
}
type fuzzyQuery struct {
Type string `json:"type"`
Field string `json:"field"`
Value interface{} `json:"value"`
}
// Fuzzy query
// https://github.com/Stratio/cassandra-lucene-index/blob/branch-3.0.10/doc/documentation.rst#fuzzy-search
func Fuzzy(f string, v interface{}) Rule {
return &fuzzyQuery{
Type: "fuzzy",
Field: f,
Value: v,
}
}
// Regexp query
// https://github.com/Stratio/cassandra-lucene-index/blob/branch-3.0.10/doc/documentation.rst#regexp-search
func Regexp(f string, v string) Rule {
return &value{
Type: "regexp",
Field: f,
Value: v,
}
}
// Wildcard query <field> <value>
// https://github.com/Stratio/cassandra-lucene-index/blob/branch-3.0.10/doc/documentation.rst#wildcard-search
func Wildcard(f string, v string) Rule {
return &value{
Type: "wildcard",
Field: f,
Value: v,
}
}
type phraseQuery struct {
Type string `json:"type"`
Field string `json:"field"`
Values interface{} `json:"values"`
Slop int `json:"slop,omitempty"`
}
// Phrase query
// see: https://github.com/Stratio/cassandra-lucene-index/blob/branch-3.0.10/doc/documentation.rst#phrase-search
// slop (default = 0): number of words permitted between words.
func Phrase(f string, v interface{}, slop int) Rule {
return &phraseQuery{
Type: "phrase",
Field: f,
Values: v,
Slop: slop,
}
}
// Contains query
func Contains(f string, v interface{}) Rule {
return &values{
Type: "contains",
Field: f,
Values: v,
}
}
// rangeValue struct
type rangeValue struct {
Type string `json:"type"`
Field string `json:"field"`
Lower interface{} `json:"lower,omitempty"`
Upper interface{} `json:"upper,omitempty"`
IncludeLower bool `json:"include_lower,omitempty"`
IncludeUpper bool `json:"include_upper,omitempty"`
}
// RangeAll query
func RangeAll(f string, lv, uv interface{}, incl, incu bool) Rule {
return &rangeValue{
Type: "range",
Field: f,
Lower: lv,
Upper: uv,
IncludeLower: incl,
IncludeUpper: incu,
}
}
// RangeLower query
func RangeLower(f string, v interface{}, inc bool) Rule {
return &rangeValue{
Type: "range",
Field: f,
Lower: v,
IncludeLower: inc,
}
}
// RangeUpper query
func RangeUpper(f string, v interface{}, inc bool) Rule {
return &rangeValue{
Type: "range",
Field: f,
Upper: v,
IncludeUpper: inc,
}
}
// BooleanQuery struct
type BooleanQuery struct {
Type string `json:"type"`
Must []Rule `json:"must,omitempty"`
Should []Rule `json:"should,omitempty"`
Not []Rule `json:"not,omitempty"`
}
// // Lucene to match Rule interface
// func (v *BooleanQuery) Lucene() bool {
// return true
// }
// BooleanMust query
func BooleanMust(rs ...Rule) Rule {
v := &BooleanQuery{
Type: "boolean",
Must: make([]Rule, len(rs)),
}
for i, r := range rs {
v.Must[i] = r
}
return v
}
// BooleanShould query
func BooleanShould(rs ...Rule) Rule {
v := &BooleanQuery{
Type: "boolean",
Should: make([]Rule, len(rs)),
}
for i, r := range rs {
v.Should[i] = r
}
return v
}
// BooleanNot query
func BooleanNot(rs ...Rule) Rule {
v := &BooleanQuery{
Type: "boolean",
Not: make([]Rule, len(rs)),
}
for i, r := range rs {
v.Not[i] = r
}
return v
}
// Refresh enable / disable refreshing index
func (x *Expr) Refresh(e bool) *Expr {
x.R = e
return x
}
// MarshalCQL enables gocql library marshal into cql
func (x *Expr) MarshalCQL(_ gocql.TypeInfo) ([]byte, error) {
return json.Marshal(x)
}
// String for stringer interface
func (x *Expr) String() string {
r, err := json.Marshal(x)
if err != nil {
return ""
}
return string(r)
}
// NewExpr creates a new expression
func NewExpr() *Expr {
return &Expr{
F: make([]Rule, 0),
Q: make([]Rule, 0),
S: make([]*Sort, 0),
}
}
// ResetQuery removes pre-configured query expression
func (x *Expr) ResetQuery() *Expr {
x.Q = make([]Rule, 0)
return x
}
// ResetFilter removes pre-configured filter expression
func (x *Expr) ResetFilter() *Expr {
x.F = make([]Rule, 0)
return x
}
// ResetSort removes pre-configured sort expression
func (x *Expr) ResetSort() *Expr {
x.S = make([]*Sort, 0)
return x
}
// Reset all pre-configured options
func (x *Expr) Reset() *Expr {
return x.ResetFilter().ResetQuery().ResetSort().Refresh(false)
}
// Query add queries to expression
func (x *Expr) Query(q ...Rule) *Expr {
for _, o := range q {
switch {
case o == nil:
continue
default:
x.Q = append(x.Q, o)
}
}
return x
}
// Filter add filters to expression
func (x *Expr) Filter(q ...Rule) *Expr {
for _, o := range q {
switch {
case o == nil:
continue
default:
x.F = append(x.F, o)
}
}
return x
}
// SortBy with field
func (x *Expr) SortBy(f string, r bool) *Expr {
if len(x.S) > 0 {
for _, s := range x.S {
if s.Field == f { // modify if exists
s.Reverse = r
return x
}
}
}
x.S = append(x.S, &Sort{Field: f, Reverse: r})
return x
}