-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
60 lines (47 loc) · 1.43 KB
/
redis.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
package main
import (
"log"
"github.com/RediSearch/redisearch-go/redisearch"
)
type redisClient interface {
CreateIndex(*redisearch.Schema) error
IndexOptions(redisearch.IndexingOptions, ...redisearch.Document) error
Info() (*redisearch.IndexInfo, error)
}
type Redis struct {
client redisClient
}
func NewRedis(master, index string) (r Redis, err error) {
r.client = redisearch.NewClient(master, index)
info, _ := r.client.Info()
if info != nil {
return
}
sc := redisearch.NewSchema(redisearch.DefaultOptions).
AddField(redisearch.NewTextField("body")).
AddField(redisearch.NewTextField("author")).
AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
AddField(redisearch.NewNumericField("date"))
err = r.client.CreateIndex(sc)
return
}
func (r Redis) WriteLoop(c chan MessageWithEnvelope) (err error) {
indexOpts := redisearch.DefaultIndexingOptions
indexOpts.Replace = true
for message := range c {
if message.Create() || message.Update() {
doc := redisearch.NewDocument(message.Message.Slug, 1.0)
doc.Set("body", message.Message.Body).
Set("author", message.Message.Author).
Set("title", message.Message.Title).
Set("date", message.Message.Date.Unix())
err = r.client.IndexOptions(indexOpts, []redisearch.Document{doc}...)
if err != nil {
return
}
} else if message.Delete() {
log.Print("Redis Loop: deletion NOP")
}
}
return
}