/
index.go
164 lines (144 loc) · 4.65 KB
/
index.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
package redisearch
import (
"github.com/gomodule/redigo/redis"
)
// IndexInfo - Structure showing information about an existing index
type IndexInfo struct {
Schema Schema
Name string `redis:"index_name"`
DocCount uint64 `redis:"num_docs"`
RecordCount uint64 `redis:"num_records"`
TermCount uint64 `redis:"num_terms"`
MaxDocID uint64 `redis:"max_doc_id"`
InvertedIndexSizeMB float64 `redis:"inverted_sz_mb"`
OffsetVectorSizeMB float64 `redis:"offset_vector_sz_mb"`
DocTableSizeMB float64 `redis:"doc_table_size_mb"`
KeyTableSizeMB float64 `redis:"key_table_size_mb"`
RecordsPerDocAvg float64 `redis:"records_per_doc_avg"`
BytesPerRecordAvg float64 `redis:"bytes_per_record_avg"`
OffsetsPerTermAvg float64 `redis:"offsets_per_term_avg"`
OffsetBitsPerTermAvg float64 `redis:"offset_bits_per_record_avg"`
IsIndexing bool `redis:"indexing"`
PercentIndexed float64 `redis:"percent_indexed"`
HashIndexingFailures uint64 `redis:"hash_indexing_failures"`
}
// IndexDefinition is used to define a index definition for automatic indexing on Hash update
// This is only valid for >= RediSearch 2.0
type IndexDefinition struct {
IndexOn string
Async bool
Prefix []string
FilterExpression string
Language string
LanguageField string
Score float64
ScoreField string
PayloadField string
}
// This is only valid for >= RediSearch 2.0
func NewIndexDefinition() *IndexDefinition {
prefixArray := make([]string, 0)
return &IndexDefinition{"HASH", false, prefixArray, "", "", "", -1, "", ""}
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetAsync(value bool) (outDef *IndexDefinition) {
outDef = defintion
outDef.Async = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) AddPrefix(prefix string) (outDef *IndexDefinition) {
outDef = defintion
outDef.Prefix = append(outDef.Prefix, prefix)
return
}
func (defintion *IndexDefinition) SetFilterExpression(value string) (outDef *IndexDefinition) {
outDef = defintion
outDef.FilterExpression = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetLanguage(value string) (outDef *IndexDefinition) {
outDef = defintion
outDef.Language = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetLanguageField(value string) (outDef *IndexDefinition) {
outDef = defintion
outDef.LanguageField = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetScore(value float64) (outDef *IndexDefinition) {
outDef = defintion
outDef.Score = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetScoreField(value string) (outDef *IndexDefinition) {
outDef = defintion
outDef.ScoreField = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) SetPayloadField(value string) (outDef *IndexDefinition) {
outDef = defintion
outDef.PayloadField = value
return
}
// This is only valid for >= RediSearch 2.0
func (defintion *IndexDefinition) Serialize(args redis.Args) redis.Args {
args = append(args, "ON", defintion.IndexOn)
if defintion.Async {
args = append(args, "ASYNC")
}
if len(defintion.Prefix) > 0 {
args = append(args, "PREFIX", len(defintion.Prefix))
for _, p := range defintion.Prefix {
args = append(args, p)
}
}
if defintion.FilterExpression != "" {
args = append(args, "FILTER", defintion.FilterExpression)
}
if defintion.Language != "" {
args = append(args, "LANGUAGE", defintion.Language)
}
if defintion.LanguageField != "" {
args = append(args, "LANGUAGE_FIELD", defintion.LanguageField)
}
if defintion.Score >= 0.0 && defintion.Score <= 1.0 {
args = append(args, "SCORE", defintion.Score)
}
if defintion.ScoreField != "" {
args = append(args, "SCORE_FIELD", defintion.ScoreField)
}
if defintion.PayloadField != "" {
args = append(args, "PAYLOAD_FIELD", defintion.PayloadField)
}
return args
}
func SerializeIndexingOptions(opts IndexingOptions, args redis.Args) redis.Args {
// apply options
// As of RediSearch 2.0 and above NOSAVE is no longer supported.
if opts.NoSave {
args = append(args, "NOSAVE")
}
if opts.Language != "" {
args = append(args, "LANGUAGE", opts.Language)
}
if opts.Partial {
opts.Replace = true
}
if opts.Replace {
args = append(args, "REPLACE")
if opts.Partial {
args = append(args, "PARTIAL")
}
if opts.ReplaceCondition != "" {
args = append(args, "IF", opts.ReplaceCondition)
}
}
return args
}