-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
37 lines (31 loc) · 1.73 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
package logic
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
"log"
)
const (
IndexName = "movies_index"
KeyPrefix = "movie:"
)
func CreateMoviesIndexOnRedis(ctx context.Context, redisClient *redis.Client) {
redisClient.FTDropIndexWithArgs(ctx, IndexName, &redis.FTDropIndexOptions{DeleteDocs: true})
titleField := &redis.FieldSchema{FieldName: "$.title", FieldType: redis.SearchFieldTypeText, As: "title"}
yearField := &redis.FieldSchema{FieldName: "$.year", FieldType: redis.SearchFieldTypeNumeric, As: "year"}
plotField := &redis.FieldSchema{FieldName: "$.plot", FieldType: redis.SearchFieldTypeText, As: "plot"}
runningTimeField := &redis.FieldSchema{FieldName: "$.runningTime", FieldType: redis.SearchFieldTypeNumeric, As: "runningTime"}
releaseDateField := &redis.FieldSchema{FieldName: "$.releaseDate", FieldType: redis.SearchFieldTypeText, As: "releaseDate"}
ratingField := &redis.FieldSchema{FieldName: "$.rating", FieldType: redis.SearchFieldTypeNumeric, As: "rating"}
genresField := &redis.FieldSchema{FieldName: "$.genres.*", FieldType: redis.SearchFieldTypeTag, As: "genres", Separator: ","}
actorsField := &redis.FieldSchema{FieldName: "$.actors.*", FieldType: redis.SearchFieldTypeTag, As: "actors", Separator: ","}
directorsField := &redis.FieldSchema{FieldName: "$.directors.*", FieldType: redis.SearchFieldTypeTag, As: "directors", Separator: ","}
_, err := redisClient.FTCreate(ctx, IndexName,
&redis.FTCreateOptions{OnJSON: true, Prefix: []interface{}{KeyPrefix}},
titleField, yearField, plotField, runningTimeField, releaseDateField,
ratingField, genresField, actorsField, directorsField).Result()
if err != nil {
log.Printf("Error creating the index: %v", err)
}
fmt.Println("🟥 Index created successfully")
}