-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggreg.go
45 lines (40 loc) · 968 Bytes
/
aggreg.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
package logic
import (
"context"
"fmt"
"log"
"github.com/redis/go-redis/v9"
)
func MovieCountPerGenreAgg(ctx context.Context, redisClient *redis.Client) {
aggregOptions := &redis.FTAggregateOptions{
GroupBy: []redis.FTAggregateGroupBy{
{
Fields: []interface{}{"@genres"},
Reduce: []redis.FTAggregateReducer{
{
Reducer: redis.SearchCount,
As: "Count",
},
},
},
},
SortBy: []redis.FTAggregateSortBy{
{
FieldName: "@Count",
Desc: true,
},
},
SortByMax: 5,
}
aggregResult, err := redisClient.FTAggregateWithArgs(ctx, IndexName, "*", aggregOptions).Result()
if err != nil {
log.Printf("Error executing the aggregation: %v", err)
return
}
if aggregResult.Total > 0 {
fmt.Printf("🟥 Top 5 Genres and their Movie Count: \n")
for i, aggregRow := range aggregResult.Rows {
fmt.Printf(" %d)️ %s = %s\n", i+1, aggregRow.Fields["genres"], aggregRow.Fields["Count"])
}
}
}