-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingest.go
117 lines (89 loc) · 2.71 KB
/
ingest.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package recon
import (
"context"
"errors"
milvus "github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/pulpchat/pulpspot/db/models"
"go.uber.org/zap"
)
type Recommendation struct{}
func RecommendationIngest(ctx context.Context, logger *zap.SugaredLogger, neoSess neo4j.SessionWithContext, milvusClient milvus.Client, user *models.User) ([]*models.Post, error) {
tx, _ := user.GetUserWithTracks(ctx)
res, err := neoSess.ExecuteRead(ctx, tx)
if err != nil {
logger.Errorf("error getting user with tracks: %v", err)
return nil, err
}
user = res.(*models.User)
recommendedPosts := make([]*models.Post, 0)
for _, track := range user.TopTracks {
err := milvusClient.LoadCollection(
ctx,
"lyric_collection",
false,
)
if err != nil {
logger.Errorf("error loading collection: %v", err)
return nil, err
}
sp, _ := entity.NewIndexFlatSearchParam()
sr, err := milvusClient.Search(ctx, "lyric_collection", []string{}, "", []string{"ID, Name, PostID"}, []entity.Vector{entity.FloatVector(track.LyricsEmbeddings)}, "Vector",
entity.L2, 45, sp)
if err != nil {
logger.Errorf("error searching: %v", err)
return nil, err
}
for _, result := range sr {
var idColumn *entity.ColumnVarChar
var nameColumn *entity.ColumnVarChar
var postIDColumn *entity.ColumnVarChar
for _, field := range result.Fields {
if field.Name() == "ID" {
c, ok := field.(*entity.ColumnVarChar)
if ok {
idColumn = c
} else {
return nil, errors.New("id column not found")
}
} else if field.Name() == "Name" {
c, ok := field.(*entity.ColumnVarChar)
if ok {
nameColumn = c
} else {
return nil, errors.New("name column not found")
}
} else if field.Name() == "PostID" {
c, ok := field.(*entity.ColumnVarChar)
if ok {
postIDColumn = c
} else {
return nil, errors.New("post id column not found")
}
}
}
if idColumn == nil {
return nil, errors.New("id column not found")
}
if nameColumn == nil {
return nil, errors.New("name column not found")
}
if postIDColumn == nil {
return nil, errors.New("post id column not found")
}
post := models.DefaultPost()
post.ID = postIDColumn.Data()[0]
tx, _ := post.GetPostWithTrack(ctx)
res, err := neoSess.ExecuteRead(ctx, tx)
if err != nil {
logger.Errorf("error getting post with track: %v", err)
return nil, err
}
post = res.(*models.Post)
recommendedPosts = append(recommendedPosts, post)
logger.Infof("Found post %s with name %s", postIDColumn.Data()[0], nameColumn.Data()[0])
}
}
return recommendedPosts, nil
}