/
main.go
197 lines (150 loc) · 4.71 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/RediSearch/redisearch-go/redisearch"
"github.com/gomodule/redigo/redis"
"github.com/nitishm/go-rejson/v4"
)
type InputSpeed struct {
Ts int64 `json:"ts"`
Value int `json:"value"`
}
type InputCourse struct {
Ts int64 `json:"ts"`
Value int `json:"value"`
}
type Input struct {
ID string `json:"id"`
ResourceID string `json:"resourceId"`
EntityID string `json:"entityId"`
Description string `json:"description"`
Speed InputSpeed `json:"speed"`
Course InputCourse `json:"course"`
Sensor []int `json:"sensor"`
}
func main() {
addr := flag.String("address", "localhost:6379", "redis server address")
useTLS := flag.Bool("tls", false, "use TLS connection")
user := flag.String("user", "default", "redis user")
password := flag.String("password", "", "redis password")
flag.Parse()
var opts []redis.DialOption
if *useTLS {
opts = append(opts, redis.DialUseTLS(true))
}
if *user != "" {
opts = append(opts, redis.DialUsername(*user))
}
if *password != "" {
opts = append(opts, redis.DialPassword(*password))
}
conn, err := redis.Dial("tcp", *addr, opts...)
if err != nil {
fmt.Println("Unable to connect to redis.")
flag.PrintDefaults()
log.Fatalln(err)
}
// conn.Close()
_, err = conn.Do("PING")
if err != nil {
fmt.Println("Unable to ping redis.")
flag.PrintDefaults()
log.Fatalln(err)
}
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", *addr, opts...)
}}
c := redisearch.NewClientFromPool(pool, "idx:input")
// Create a schema
sc := redisearch.NewSchema(redisearch.DefaultOptions).
AddField(redisearch.NewTagFieldOptions("$.resourceId", redisearch.TagFieldOptions{Separator: ';', As: "resourceId"})).
AddField(redisearch.NewTagFieldOptions("$.entityId", redisearch.TagFieldOptions{Separator: ';', As: "entityId"})).
AddField(redisearch.NewNumericFieldOptions("$.speed.value", redisearch.NumericFieldOptions{As: "speed"})).
AddField(redisearch.NewNumericFieldOptions("$.course.value", redisearch.NumericFieldOptions{As: "course"}))
// Drop an existing index. If the index does not exist an error is returned
err = c.Drop()
if err != nil {
log.Println(err)
}
// Create the index with the given schema and definition
id := &redisearch.IndexDefinition{IndexOn: "JSON", Score: -1.0}
if err := c.CreateIndexWithIndexDefinition(sc, id.AddPrefix("input")); err != nil {
log.Fatalln(err)
}
rh := rejson.NewReJSONHandler()
rh.SetRedigoClient(pool.Get())
ret, err := rh.JSONSet("input:5e98612a9a72a30010ec0001", "$", &Input{
ID: "5e98612a9a72a30010ec0001",
ResourceID: "5e98612a9a72a30010ec03f5", // could be a secondary index
EntityID: "5e9572de2b4aae0010433600", // could be a secondary index,
Speed: InputSpeed{
Ts: time.Now().Unix(),
Value: 50,
},
Course: InputCourse{
Ts: time.Now().Unix(),
Value: 100,
},
})
if err != nil {
log.Fatalln(err)
}
fmt.Println("WRITE ->", ret, err)
ret, err = rh.JSONSet("input:5e98612a9a72a30010ec0003", "$", &Input{
ID: "5e98612a9a72a30010ec0003",
ResourceID: "5e98612a9a72a30010ec03ba", // could be a secondary index
EntityID: "5e9572de2b4aae0010433600", // could be a secondary index,
Speed: InputSpeed{
Ts: time.Now().Unix(),
Value: 75,
},
Course: InputCourse{
Ts: time.Now().Unix(),
Value: 20,
},
})
if err != nil {
log.Fatalln(err)
}
fmt.Println("WRITE ->", ret, err)
ret, err = rh.JSONSet("input:5e98612a9a72a30010ec0004", "$", &Input{
ID: "5e98612a9a72a30010ec0004",
ResourceID: "5e98612a9a72a30010ec03ba", // could be a secondary index
EntityID: "5e9572de2b4aae0010433600", // could be a secondary index,
Speed: InputSpeed{
Ts: time.Now().Unix(),
Value: 18,
},
Course: InputCourse{
Ts: time.Now().Unix(),
Value: 45,
},
})
if err != nil {
log.Fatalln(err)
}
fmt.Println("WRITE ->", ret, err)
docs, total, err := c.Search(redisearch.NewQuery("@resourceId:{5e98612a9a72a30010ec03f5}"))
if err != nil {
log.Fatalln(err)
}
fmt.Println("READ ONE ->", docs, total, err)
docs, total, err = c.Search(redisearch.NewQuery("@resourceId:{5e98612a9a72a30010ec03ba | 5e98612a9a72a30010ec03f5}"))
if err != nil {
log.Fatalln(err)
}
fmt.Println("READ MULTIPLE ->", docs, total, err)
docs, total, err = c.Search(redisearch.NewQuery("@resourceId:{5e98612a9a72a30010ec03f4} @speed:[0 70]"))
if err != nil {
log.Fatalln(err)
}
fmt.Println("READ BETWEEN VALUES 1 ->", docs, total, err)
docs, total, err = c.Search(redisearch.NewQuery("@speed:[0 70] @course:[0 60]"))
if err != nil {
log.Fatalln(err)
}
fmt.Println("READ BETWEEN VALUES 2 ->", docs, total, err)
}