This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fts.go
334 lines (288 loc) · 8.46 KB
/
fts.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
package bucket
import (
"context"
"fmt"
"reflect"
"time"
"github.com/couchbase/gocb"
"github.com/couchbase/gocb/cbft"
)
// Available facet types
const (
FacetDate = iota
FacetNumeric
FacetTerm
)
// SearchQuery is a representation of the available
// search option for a SimpleSearch
type SearchQuery struct {
Query string `json:"query,omitempty"`
Match string `json:"match,omitempty"`
MatchPhrase string `json:"match_phrase,omitempty"`
Term string `json:"term,omitempty"`
Prefix string `json:"prefix,omitempty"`
Regexp string `json:"regexp,omitempty"`
Wildcard string `json:"wildcard,omitempty"`
Field string `json:"field,omitempty"`
Analyzer string `json:"analyzer,omitempty"`
Fuzziness int64 `json:"fuzziness,omitempty"`
PrefixLength int64 `json:"prefix_length,omitempty"`
Limit int `json:"-"`
Offset int `json:"-"`
}
// FacetDef is a configuration helper for the search's facets
type FacetDef struct {
Name string
Type int
Field string
Size int
}
// CompoundQueries is a representation of the available
// search option for a CompoundSearch
type CompoundQueries struct {
Conjunction []SearchQuery `json:"conjuncts,omitempty"`
Disjunction []SearchQuery `json:"disjuncts,omitempty"`
Limit int `json:"-"`
Offset int `json:"-"`
}
// RangeQuery is a representation of the available
// search option for a RangeSearch
type RangeQuery struct {
StartAsTime time.Time `json:"-"`
EndAsTime time.Time `json:"-"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
Min int64 `json:"min,omitempty"`
Max int64 `json:"max,omitempty"`
InclusiveStart bool `json:"inclusive_start,omitempty"`
InclusiveEnd bool `json:"inclusive_end,omitempty"`
InclusiveMin bool `json:"inclusive_min,omitempty"`
InclusiveMax bool `json:"inclusive_max,omitempty"`
Field string `json:"field,omitempty"`
Limit int `json:"-"`
Offset int `json:"-"`
}
// SimpleSearch apply the configuration of SearchQuery
// and do the search then returns the hits
func (h *Handler) SimpleSearch(ctx context.Context, index string, q *SearchQuery) ([]gocb.SearchResultHit, error) {
if err := q.setup(); err != nil {
return nil, err
}
if index == "" {
return nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
status, result, _, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, fmt.Errorf("%+v", status.Errors)
}
return result, err
}
// SimpleSearchWithFacets apply the configuration of SearchQuery
// and do the search then returns the hits and facets
func (h *Handler) SimpleSearchWithFacets(ctx context.Context, index string, q *SearchQuery, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
if err := q.setup(); err != nil {
return nil, nil, err
}
if index == "" {
return nil, nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
h.addFacets(ctx, query, facets)
status, result, facetResult, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, nil, fmt.Errorf("%+v", status.Errors)
}
return result, facetResult, err
}
// CompoundSearch apply the configuration of CompoundQueries
// and do the search then returns the hits
func (h *Handler) CompoundSearch(ctx context.Context, index string, q *CompoundQueries) ([]gocb.SearchResultHit, error) {
if err := q.setup(); err != nil {
return nil, err
}
if index == "" {
return nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
status, result, _, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, fmt.Errorf("%+v", status.Errors)
}
return result, err
}
// CompoundSearchWithFacets apply the configuration of CompoundQueries
// and do the search then returns the hits and facets
func (h *Handler) CompoundSearchWithFacets(ctx context.Context, index string, q *CompoundQueries, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
if err := q.setup(); err != nil {
return nil, nil, err
}
if index == "" {
return nil, nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
h.addFacets(ctx, query, facets)
status, result, facetResult, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, nil, fmt.Errorf("%+v", status.Errors)
}
return result, facetResult, err
}
// RangeSearch apply the configuration of RangeQuery
// and do the search then returns the hits
func (h *Handler) RangeSearch(ctx context.Context, index string, q *RangeQuery) ([]gocb.SearchResultHit, error) {
if err := q.setup(); err != nil {
return nil, err
}
if index == "" {
return nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
status, result, _, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, fmt.Errorf("%+v", status.Errors)
}
return result, err
}
// RangeSearchWithFacets apply the configuration of RangeQuery
// and do the search then returns the hits and facets
func (h *Handler) RangeSearchWithFacets(ctx context.Context, index string, q *RangeQuery, facets []FacetDef) ([]gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
if err := q.setup(); err != nil {
return nil, nil, err
}
if index == "" {
return nil, nil, ErrEmptyIndex
}
query := gocb.NewSearchQuery(index, q).Limit(q.Limit).Skip(q.Offset)
h.addFacets(ctx, query, facets)
status, result, facetResult, err := h.doSearch(ctx, query)
if status.Errors != nil && !reflect.ValueOf(status.Errors).IsNil() {
return nil, nil, fmt.Errorf("%+v", status.Errors)
}
return result, facetResult, err
}
func (h *Handler) doSearch(ctx context.Context, query *gocb.SearchQuery) (gocb.SearchResultStatus, []gocb.SearchResultHit, map[string]gocb.SearchResultFacet, error) {
res, err := h.state.bucket.ExecuteSearchQuery(query)
if err != nil {
if res != nil {
return res.Status(), nil, nil, err
}
return gocb.SearchResultStatus{}, nil, nil, err
}
//fmt.Printf("%+v\n", res.Status())
//for i, v := range res.Hits() {
// fmt.Printf("%d ---- %+v\n", i, v)
//}
return res.Status(), res.Hits(), res.Facets(), nil
}
func (h *Handler) addFacets(ctx context.Context, query *gocb.SearchQuery, facets []FacetDef) {
for _, facet := range facets {
switch facet.Type {
case FacetDate:
query.AddFacet(facet.Name, cbft.NewDateFacet(facet.Field, facet.Size))
case FacetNumeric:
query.AddFacet(facet.Name, cbft.NewNumericFacet(facet.Field, facet.Size))
case FacetTerm:
query.AddFacet(facet.Name, cbft.NewTermFacet(facet.Field, facet.Size))
}
}
}
func (s *SearchQuery) setup() error {
if s.Query != "" {
s.Match = ""
s.MatchPhrase = ""
s.Term = ""
s.Prefix = ""
s.Regexp = ""
s.Wildcard = ""
return nil
}
if s.Field == "" {
return ErrEmptyField
}
switch {
case s.Match != "":
s.Query = ""
s.MatchPhrase = ""
s.Term = ""
s.Prefix = ""
s.Regexp = ""
s.Wildcard = ""
case s.MatchPhrase != "":
s.Query = ""
s.Match = ""
s.Term = ""
s.Prefix = ""
s.Regexp = ""
s.Wildcard = ""
case s.Term != "":
s.Query = ""
s.Match = ""
s.MatchPhrase = ""
s.Prefix = ""
s.Regexp = ""
s.Wildcard = ""
case s.Prefix != "":
s.Query = ""
s.Match = ""
s.MatchPhrase = ""
s.Term = ""
s.Regexp = ""
s.Wildcard = ""
case s.Regexp != "":
s.Query = ""
s.Match = ""
s.MatchPhrase = ""
s.Term = ""
s.Prefix = ""
s.Wildcard = ""
case s.Wildcard != "":
s.Query = ""
s.Match = ""
s.MatchPhrase = ""
s.Term = ""
s.Prefix = ""
s.Regexp = ""
}
return nil
}
func (c *CompoundQueries) setup() error {
if c.Conjunction == nil && c.Disjunction == nil {
return ErrConjunctionAndDisjunctionIsNil
}
if c.Conjunction != nil {
c.Disjunction = nil
for _, sq := range c.Conjunction {
err := sq.setup()
if err != nil {
return err
}
}
} else {
for _, sq := range c.Disjunction {
err := sq.setup()
if err != nil {
return err
}
}
}
return nil
}
func (d *RangeQuery) setup() error {
if d.Field == "" {
return ErrEmptyField
}
if !d.StartAsTime.IsZero() {
if d.EndAsTime.IsZero() {
return ErrEndAsTimeZero
}
d.Start = d.StartAsTime.Format(time.RFC3339)
d.End = d.EndAsTime.Format(time.RFC3339)
d.Min = 0
d.Max = 0
return nil
}
d.Start = ""
d.End = ""
return nil
}