@@ -2,20 +2,17 @@ package logic
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"github.com/redis/go-redis/v9"
8
7
"log"
9
- "redisjson4gophers/domain"
10
- "strconv"
11
8
)
12
9
13
10
const (
14
- indexName = "json_movies_index "
11
+ indexName = "movies_index "
15
12
keyPrefix = "movie:"
16
13
)
17
14
18
- func IndexMoviesAsDocuments (ctx context.Context , redisClient * redis.Client , movies []domain. Movie ) {
15
+ func CreateMoviesIndexOnRedis (ctx context.Context , redisClient * redis.Client ) {
19
16
redisClient .FTDropIndexWithArgs (ctx , indexName , & redis.FTDropIndexOptions {DeleteDocs : true })
20
17
21
18
titleField := & redis.FieldSchema {FieldName : "$.title" , FieldType : redis .SearchFieldTypeText , As : "title" }
@@ -28,24 +25,13 @@ func IndexMoviesAsDocuments(ctx context.Context, redisClient *redis.Client, movi
28
25
actorsField := & redis.FieldSchema {FieldName : "$.actors.*" , FieldType : redis .SearchFieldTypeTag , As : "actors" }
29
26
directorsField := & redis.FieldSchema {FieldName : "$.directors.*" , FieldType : redis .SearchFieldTypeTag , As : "directors" }
30
27
31
- redisClient .FTCreate (ctx , indexName ,
28
+ _ , err := redisClient .FTCreate (ctx , indexName ,
32
29
& redis.FTCreateOptions {OnJSON : true , Prefix : []interface {}{keyPrefix }},
33
30
titleField , yearField , plotField , runningTimeField , releaseDateField ,
34
31
ratingField , genresField , actorsField , directorsField ).Result ()
35
-
36
- pipeline := redisClient .Pipeline ()
37
- for movieID , movie := range movies {
38
- movieAsJSON , err := json .Marshal (movie )
39
- if err != nil {
40
- log .Printf ("Error marshaling movie into JSON: %v" , err )
41
- }
42
- pipeline .JSONSet (ctx , keyPrefix + strconv .Itoa (movieID ), "$" , string (movieAsJSON ))
43
- }
44
-
45
- _ , err := pipeline .Exec (ctx )
46
32
if err != nil {
47
- log .Printf ("Error writing JSON documents into Redis : %v" , err )
33
+ log .Printf ("Error creating the index : %v" , err )
48
34
}
49
35
50
- fmt .Printf ("🟥 Movies indexed on Redis: %d \n " , len ( movies ) )
36
+ fmt .Println ("🟥 Index created successfully" )
51
37
}
0 commit comments