-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggreg.go
50 lines (45 loc) · 1.2 KB
/
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
46
47
48
49
50
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,
}
rawResult, err := redisClient.FTAggregateWithArgs(ctx, IndexName, "*", aggregOptions).RawResult()
if err != nil {
log.Printf("Error executing the aggregation: %v", err)
return
}
if rawResult != nil {
aggregationResults := rawResult.(map[interface{}]interface{})["results"].([]interface{})
if len(aggregationResults) > 0 {
fmt.Printf("🟥 Top 5 Genres and their Movie Count: \n")
for i, aggregResult := range aggregationResults {
entry := aggregResult.(map[interface{}]interface{})
extraAttribs := entry["extra_attributes"].(map[interface{}]interface{})
fmt.Printf(" %d)️ %s = %s\n", i+1, extraAttribs["genres"], extraAttribs["Count"])
}
}
}
}