forked from RediSearch/redisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
475 lines (421 loc) · 13.2 KB
/
schema.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
package redisearch
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
// FieldType is an enumeration of field/property types
type FieldType int
// PhoneticMatcherType is an enumeration of the phonetic algorithm and language used.
type PhoneticMatcherType string
// Options are flags passed to the the abstract Index call, which receives them as interface{}, allowing
// for implementation specific options
type Options struct {
// If set, we will not save the documents contents, just index them, for fetching ids only.
NoSave bool
// If set, we avoid saving field bits for each term.
// This saves memory, but does not allow filtering by specific fields.
// This is an option that is applied and index level.
NoFieldFlags bool
// If set, we avoid saving the term frequencies in the index.
// This saves memory but does not allow sorting based on the frequencies of a given term within the document.
// This is an option that is applied and index level.
NoFrequencies bool
// If set, we avoid saving the term offsets for documents.
// This saves memory but does not allow exact searches or highlighting. Implies NOHL
// This is an option that is applied and index level.
NoOffsetVectors bool
// Set the index with a custom stop-words list, to be ignored during indexing and search time
// This is an option that is applied and index level.
// If the list is nil the default stop-words list is used.
// See https://oss.redislabs.com/redisearch/Stopwords.html#default_stop-word_list
Stopwords []string
// If set to true, creates a lightweight temporary index which will expire after the specified period of inactivity.
// The internal idle timer is reset whenever the index is searched or added to.
// Because such indexes are lightweight, you can create thousands of such indexes without negative performance implications.
Temporary bool
TemporaryPeriod int
// For efficiency, RediSearch encodes indexes differently if they are created with less than 32 text fields.
// If set to true This option forces RediSearch to encode indexes as if there were more than 32 text fields,
// which allows you to add additional fields (beyond 32).
MaxTextFieldsFlag bool
// If set to true, conserves storage space and memory by disabling highlighting support.
// Also implied by NoOffsetVectors
NoHighlights bool
// If set to true, we do not scan and index.
SkipInitialScan bool
}
func NewOptions() *Options {
var opts = DefaultOptions
return &opts
}
// If set to true, creates a lightweight temporary index which will expire after the specified period of inactivity.
// The internal idle timer is reset whenever the index is searched or added to.
// To enable the temporary index creation, use SetTemporaryPeriod(). This method should be preferably used for disabling the flag
func (options *Options) SetTemporary(temporary bool) *Options {
options.Temporary = temporary
return options
}
// If set to a positive integer, creates a lightweight temporary index which will expire after the specified period of inactivity (in seconds).
// The internal idle timer is reset whenever the index is searched or added to.
func (options *Options) SetTemporaryPeriod(period int) *Options {
options.TemporaryPeriod = period
options.Temporary = true
return options
}
// Set the index with a custom stop-words list, to be ignored during indexing and search time
// This is an option that is applied and index level.
// If the list is nil the default stop-words list is used.
// See https://oss.redislabs.com/redisearch/Stopwords.html#default_stop-word_list
func (options *Options) SetStopWords(stopwords []string) *Options {
options.Stopwords = stopwords
return options
}
// For efficiency, RediSearch encodes indexes differently if they are created with less than 32 text fields.
// If set to true, this flag forces RediSearch to encode indexes as if there were more than 32 text fields,
// which allows you to add additional fields (beyond 32).
func (options *Options) SetMaxTextFieldsFlag(flag bool) *Options {
options.MaxTextFieldsFlag = flag
return options
}
// SetNoHighlight conserves storage space and memory by disabling highlighting support.
func (options *Options) SetNoHighlight(flag bool) *Options {
options.NoHighlights = flag
return options
}
// SetSkipInitialScan determines if scan the index on creation
func (options *Options) SetSkipInitialScan(flag bool) *Options {
options.SkipInitialScan = flag
return options
}
// DefaultOptions represents the default options
var DefaultOptions = Options{
NoSave: false,
NoFieldFlags: false,
NoFrequencies: false,
NoOffsetVectors: false,
Stopwords: nil,
Temporary: false,
TemporaryPeriod: 0,
MaxTextFieldsFlag: false,
NoHighlights: false,
SkipInitialScan: false,
}
// Field Types
const (
// TextField full-text field
TextField FieldType = iota
// NumericField numeric range field
NumericField
// GeoField geo-indexed point field
GeoField
// TagField is a field used for compact indexing of comma separated values
TagField
//VectorField allows vector similarity queries against the value in this attribute.
VectorField
)
// Phonetic Matchers
const (
PhoneticDoubleMetaphoneEnglish PhoneticMatcherType = "dm:en"
PhoneticDoubleMetaphoneFrench PhoneticMatcherType = "dm:fr"
PhoneticDoubleMetaphonePortuguese PhoneticMatcherType = "dm:pt"
PhoneticDoubleMetaphoneSpanish PhoneticMatcherType = "dm:es"
)
// Field represents a single field's Schema
type Field struct {
Name string
Type FieldType
Sortable bool
Options interface{}
}
// TextFieldOptions Options for text fields - weight and stemming enabled/disabled.
type TextFieldOptions struct {
Weight float32
Sortable bool
NoStem bool
NoIndex bool
PhoneticMatcher PhoneticMatcherType
As string
}
// TagFieldOptions options for indexing tag fields
type TagFieldOptions struct {
// Separator is the custom separator between tags. defaults to comma (,)
Separator byte
NoIndex bool
Sortable bool
CaseSensitive bool
As string
}
// NumericFieldOptions Options for numeric fields
type NumericFieldOptions struct {
Sortable bool
NoIndex bool
As string
}
// GeoFieldOptions Options for geo fields
type GeoFieldOptions struct {
NoIndex bool
As string
}
type algorithm string
// Supported algorithms for Vector field
const (
Flat algorithm = "FLAT"
HNSW algorithm = "HNSW"
)
// VectorFieldOptions Options for vector fields
type VectorFieldOptions struct {
Algorithm algorithm
Attributes map[string]interface{}
}
// NewTextField creates a new text field with the given weight
func NewTextField(name string) Field {
return Field{
Name: name,
Type: TextField,
Options: nil,
}
}
// NewTextFieldOptions creates a new text field with given options (weight/sortable)
func NewTextFieldOptions(name string, opts TextFieldOptions) Field {
f := NewTextField(name)
f.Options = opts
return f
}
// NewSortableTextField creates a text field with the sortable flag set
func NewSortableTextField(name string, weight float32) Field {
return NewTextFieldOptions(name, TextFieldOptions{
Weight: weight,
Sortable: true,
})
}
// NewTagField creates a new text field with default options (separator: ,)
func NewTagField(name string) Field {
return Field{
Name: name,
Type: TagField,
Options: TagFieldOptions{Separator: ',', NoIndex: false, CaseSensitive: false},
}
}
// NewTagFieldOptions creates a new tag field with the given options
func NewTagFieldOptions(name string, opts TagFieldOptions) Field {
return Field{
Name: name,
Type: TagField,
Options: opts,
}
}
// NewNumericField creates a new numeric field with the given name
func NewNumericField(name string) Field {
return Field{
Name: name,
Type: NumericField,
}
}
// NewNumericFieldOptions defines a numeric field with additional options
func NewNumericFieldOptions(name string, options NumericFieldOptions) Field {
f := NewNumericField(name)
f.Options = options
return f
}
// NewSortableNumericField creates a new numeric field with the given name and a sortable flag
func NewSortableNumericField(name string) Field {
f := NewNumericField(name)
f.Options = NumericFieldOptions{
Sortable: true,
}
return f
}
// NewGeoField creates a new geo field with the given name
func NewGeoField(name string) Field {
return Field{
Name: name,
Type: GeoField,
Options: nil,
}
}
// NewGeoFieldOptions creates a new geo field with the given name and additional options
func NewGeoFieldOptions(name string, options GeoFieldOptions) Field {
f := NewGeoField(name)
f.Options = options
return f
}
// NewVectorFieldOptions creates a new geo field with the given name and additional options
func NewVectorFieldOptions(name string, options VectorFieldOptions) Field {
return Field{
Name: name,
Type: VectorField,
Options: options,
}
}
// Schema represents an index schema Schema, or how the index would
// treat documents sent to it.
type Schema struct {
Fields []Field
Options Options
}
// NewSchema creates a new Schema object
func NewSchema(opts Options) *Schema {
return &Schema{
Fields: []Field{},
Options: opts,
}
}
// AddField adds a field to the Schema object
func (m *Schema) AddField(f Field) *Schema {
if m.Fields == nil {
m.Fields = []Field{}
}
m.Fields = append(m.Fields, f)
return m
}
func SerializeSchema(s *Schema, args redis.Args) (argsOut redis.Args, err error) {
argsOut = args
if s.Options.MaxTextFieldsFlag {
argsOut = append(argsOut, "MAXTEXTFIELDS")
}
if s.Options.NoOffsetVectors {
argsOut = append(argsOut, "NOOFFSETS")
}
if s.Options.Temporary {
argsOut = append(argsOut, "TEMPORARY", s.Options.TemporaryPeriod)
}
if s.Options.NoHighlights {
argsOut = append(argsOut, "NOHL")
}
if s.Options.NoFieldFlags {
argsOut = append(argsOut, "NOFIELDS")
}
if s.Options.NoFrequencies {
argsOut = append(argsOut, "NOFREQS")
}
if s.Options.SkipInitialScan {
argsOut = append(argsOut, "SKIPINITIALSCAN")
}
if s.Options.Stopwords != nil {
argsOut = argsOut.Add("STOPWORDS", len(s.Options.Stopwords))
if len(s.Options.Stopwords) > 0 {
argsOut = argsOut.AddFlat(s.Options.Stopwords)
}
}
argsOut = append(argsOut, "SCHEMA")
for _, f := range s.Fields {
argsOut, err = serializeField(f, argsOut)
if err != nil {
return nil, err
}
}
return
}
func serializeField(f Field, args redis.Args) (argsOut redis.Args, err error) {
argsOut = args
switch f.Type {
case TextField:
argsOut = append(argsOut, f.Name, "TEXT")
if f.Options != nil {
opts, ok := f.Options.(TextFieldOptions)
if !ok {
err = fmt.Errorf("Error on TextField serialization")
return
}
if opts.As != "" {
argsOut = append(argsOut[:len(argsOut)-1], "AS", opts.As, "TEXT")
}
if opts.Weight != 0 && opts.Weight != 1 {
argsOut = append(argsOut, "WEIGHT", opts.Weight)
}
if opts.NoStem {
argsOut = append(argsOut, "NOSTEM")
}
if opts.PhoneticMatcher != "" {
argsOut = append(argsOut, "PHONETIC", string(opts.PhoneticMatcher))
}
if opts.Sortable {
argsOut = append(argsOut, "SORTABLE")
}
if opts.NoIndex {
argsOut = append(argsOut, "NOINDEX")
}
}
case NumericField:
argsOut = append(argsOut, f.Name, "NUMERIC")
if f.Options != nil {
opts, ok := f.Options.(NumericFieldOptions)
if !ok {
err = fmt.Errorf("Error on NumericField serialization")
return
}
if opts.As != "" {
argsOut = append(argsOut[:len(argsOut)-1], "AS", opts.As, "NUMERIC")
}
if opts.Sortable {
argsOut = append(argsOut, "SORTABLE")
}
if opts.NoIndex {
argsOut = append(argsOut, "NOINDEX")
}
}
case TagField:
argsOut = append(argsOut, f.Name, "TAG")
if f.Options != nil {
opts, ok := f.Options.(TagFieldOptions)
if !ok {
err = fmt.Errorf("Error on TagField serialization")
return
}
if opts.As != "" {
argsOut = append(argsOut[:len(argsOut)-1], "AS", opts.As, "TAG")
}
if opts.Separator != 0 {
argsOut = append(argsOut, "SEPARATOR", fmt.Sprintf("%c", opts.Separator))
}
if opts.CaseSensitive {
argsOut = append(argsOut, "CASESENSITIVE")
}
if opts.Sortable {
argsOut = append(argsOut, "SORTABLE")
}
if opts.NoIndex {
argsOut = append(argsOut, "NOINDEX")
}
}
case GeoField:
argsOut = append(argsOut, f.Name, "GEO")
if f.Options != nil {
opts, ok := f.Options.(GeoFieldOptions)
if !ok {
err = fmt.Errorf("Error on GeoField serialization")
return
}
if opts.As != "" {
argsOut = append(argsOut[:len(argsOut)-1], "AS", opts.As, "GEO")
}
if opts.NoIndex {
argsOut = append(argsOut, "NOINDEX")
}
}
case VectorField:
argsOut = append(argsOut, f.Name, "VECTOR")
if f.Options != nil {
opts, ok := f.Options.(VectorFieldOptions)
if !ok {
err = fmt.Errorf("Error on VectorField serialization")
return
}
if opts.Algorithm != "" {
argsOut = append(argsOut, opts.Algorithm)
}
if opts.Attributes != nil {
var flat []interface{}
for attrName, attrValue := range opts.Attributes {
flat = append(flat, attrName, attrValue)
}
argsOut = append(argsOut, len(flat))
argsOut = append(argsOut, flat...)
}
}
default:
err = fmt.Errorf("Unrecognized field type %v serialization", f.Type)
return
}
return
}