-
Notifications
You must be signed in to change notification settings - Fork 6
/
graph_recall.go
172 lines (154 loc) · 4.81 KB
/
graph_recall.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package recall
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
igraph "github.com/aliyun/aliyun-igraph-go-sdk"
"github.com/alibaba/pairec/v2/context"
"github.com/alibaba/pairec/v2/datasource/graph"
"github.com/alibaba/pairec/v2/log"
"github.com/alibaba/pairec/v2/module"
"github.com/alibaba/pairec/v2/recconf"
"github.com/alibaba/pairec/v2/utils"
)
type GraphRecall struct {
*BaseRecall
graphClient *graph.GraphClient
ItemId string
QueryString string
Params []string
}
func NewGraphRecall(config recconf.RecallConfig) *GraphRecall {
graphClient, err := graph.GetGraphClient(config.GraphConf.GraphName)
if err != nil {
panic(err)
}
Graph := GraphRecall{
BaseRecall: NewBaseRecall(config),
graphClient: graphClient,
QueryString: config.GraphConf.QueryString,
ItemId: config.GraphConf.ItemId,
Params: config.GraphConf.Params,
}
return &Graph
}
func (i *GraphRecall) GetCandidateItems(user *module.User, context *context.RecommendContext) (ret []*module.Item) {
start := time.Now()
if i.cache != nil {
key := i.cachePrefix + string(user.Id)
cacheRet := i.cache.Get(key)
if itemStr, ok := cacheRet.([]uint8); ok {
itemIds := strings.Split(string(itemStr), ",")
for _, id := range itemIds {
var item *module.Item
if strings.Contains(id, ":") {
vars := strings.Split(id, ":")
item = module.NewItem(vars[0])
f, _ := strconv.ParseFloat(vars[1], 64)
item.Score = f
} else {
item = module.NewItem(id)
}
item.RetrieveId = i.modelName
ret = append(ret, item)
}
log.Info(fmt.Sprintf("requestId=%s\tmodule=GraphRecall\tfrom=cache\tcount=%d\tcost=%d", context.RecommendId, len(ret), utils.CostTime(start)))
return
}
}
queryString, err := i.getQueryString(user, context)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tevent=GraphRecall\terr=%s", context.RecommendId, err.Error()))
return
}
//log query_string
if context.Debug {
log.Info(fmt.Sprintf("event=GraphRecall\tqueryString=%s", queryString))
}
queryParam := make(map[string]string)
request := igraph.ReadRequest{
QueryString: queryString,
QueryParams: queryParam,
}
resp, err := i.graphClient.GraphClient.Read(&request)
if err != nil {
log.Error(fmt.Sprintf("requestId=%s\tevent=GraphRecall\terr=%s", context.RecommendId, err.Error()))
return
}
result := resp.Result
for _, res := range result {
for _, data := range res.Data {
itemId := data[i.ItemId]
score := data["score"]
if itemId != nil {
item := module.NewItem(itemId.(string))
item.RetrieveId = i.modelName
if score != nil {
item.Score = score.(float64)
}
ret = append(ret, item)
}
}
}
if i.cache != nil && len(ret) > 0 {
go func() {
key := i.cachePrefix + string(user.Id)
var itemIds string
for _, item := range ret {
itemIds += fmt.Sprintf("%s:%v", string(item.Id), item.Score) + ","
}
itemIds = itemIds[:len(itemIds)-1]
if err = i.cache.Put(key, itemIds, time.Duration(i.cacheTime)*time.Second); err != nil {
log.Error(fmt.Sprintf("requestId=%s\tmodule=GraphRecall\terror=%v", context.RecommendId, err))
}
}()
}
log.Info(fmt.Sprintf("requestId=%s\tmodule=GraphRecall\tcount=%d\tcost=%d", context.RecommendId, len(ret), utils.CostTime(start)))
return
}
func (i *GraphRecall) getQueryString(user *module.User, context *context.RecommendContext) (string, error) {
paramResults := make([]string, len(i.Params))
for x, param := range i.Params {
paramArr := strings.Split(param, ".")
switch len(paramArr) {
case 1:
return "", fmt.Errorf("Params(%s) type is error, its type should be a.b or a.b.c", param)
case 2:
if paramArr[0] == "user" {
val := user.StringProperty(paramArr[1])
paramResults[x] = val
} else if paramArr[0] == "context" {
val := utils.ToString(context.GetParameter(paramArr[1]), "")
paramResults[x] = val
}
case 3: //get value from context features
if paramArr[0] == "context" {
if paramArr[1] == "features" {
var featureMap map[string]interface{}
features := context.GetParameter("features")
if features != nil {
featureMap = features.(map[string]interface{})
val := utils.ToString(featureMap[paramArr[2]], "")
paramResults[x] = val
} else {
return "", errors.New("context.feature is null")
}
} else {
return "", fmt.Errorf("Params(%s) only support context.features.xxx", param)
}
} else {
return "", fmt.Errorf("Params(%s) only support context.features.xxx", param)
}
default:
return "", fmt.Errorf("Params(%s) type is error, its type should be a.b or a.b.c", param)
}
}
queryString := i.QueryString
for x, result := range paramResults {
replaceStr := fmt.Sprintf("$%d", x+1) // query string 中是从 $1 开始
queryString = strings.Replace(queryString, replaceStr, result, 1)
}
return queryString, nil
}