fix: search ali

master
howell 4 years ago
parent 64fcb0fa7a
commit 693f6224d1

@ -4,6 +4,8 @@ import (
"archive/zip"
"bytes"
"fmt"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
"image"
"image/jpeg"
"image/png"
@ -20,6 +22,7 @@ import (
"recook/internal/libs/weibo"
"recook/internal/static_path"
"recook/internal/v2/model/recook/goods"
"recook/internal/v2/model/search_ali"
"recook/tools"
"strconv"
"strings"
@ -280,3 +283,62 @@ func (g *Goods) Publish(c *gin.Context) {
c.String(200, "suc")
}
func (o *Goods) OpenSearch(c *gin.Context) {
go func() {
page := 0
size := 1000
for {
var gs []goods.RecookGoodsInfoModel
mysql2.Db.Offset(page * size).Limit(size).Find(&gs)
if len(gs) == 0 {
break
}
config := &search_ali.Config{
Endpoint: tea.String(search_ali.EndPoint),
AccessKeyId: tea.String(search_ali.AccessKeyId),
AccessKeySecret: tea.String(search_ali.AccessKeySecret),
}
client, _clientErr := search_ali.NewClient(config)
// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
runTime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}
requestBody := make([]search_ali.Res, 0)
for _, v := range gs {
cmd := search_ali.ADD
if v.PublishStatus == 0 {
cmd = search_ali.DELETE
}
requestBody = append(requestBody, search_ali.Res{
Cmd: cmd,
Fields: search_ali.F{
ID: v.Id,
GoodsName: v.GoodsName,
},
})
}
// 发送请求的方法调用.
_, _ = client.Request(
tea.String("POST"),
tea.String("/v3/openapi/apps/"+search_ali.ADD+"/"+search_ali.Table+"/actions/bulk"),
nil,
nil,
requestBody,
runTime)
page += 1
}
}()
c.String(200, "sync")
}

@ -34,18 +34,18 @@ func CreateRes(cmd string, f F) Res {
}
var (
endPoint = "intranet.opensearch-cn-shanghai.aliyuncs.com"
aid = "LTAI5tR6hFZwtJLKFq4Sxibg"
ask = "pn0fKjGY7zoLzWdGjjcxJP95jrAhAn"
app = "zjyc"
table = "main"
EndPoint = "intranet.opensearch-cn-shanghai.aliyuncs.com"
AccessKeyId = "LTAI5tR6hFZwtJLKFq4Sxibg"
AccessKeySecret = "pn0fKjGY7zoLzWdGjjcxJP95jrAhAn"
App = "zjyc"
Table = "main"
)
func PublishMessage(res Res) {
config := &Config{
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
Endpoint: tea.String(EndPoint),
AccessKeyId: tea.String(AccessKeyId),
AccessKeySecret: tea.String(AccessKeySecret),
}
client, _clientErr := NewClient(config)
@ -65,7 +65,7 @@ func PublishMessage(res Res) {
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("POST"),
tea.String("/v3/openapi/apps/"+app+"/"+table+"/actions/bulk"),
tea.String("/v3/openapi/apps/"+App+"/"+Table+"/actions/bulk"),
nil,
nil,
[]interface{}{res},
@ -82,9 +82,9 @@ func PublishMessage(res Res) {
func PushMessage() {
config := &Config{
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
Endpoint: tea.String(EndPoint),
AccessKeyId: tea.String(AccessKeyId),
AccessKeySecret: tea.String(AccessKeySecret),
}
client, _clientErr := NewClient(config)
@ -116,7 +116,7 @@ func PushMessage() {
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("POST"),
tea.String("/v3/openapi/apps/"+app+"/"+table+"/actions/bulk"),
tea.String("/v3/openapi/apps/"+App+"/"+Table+"/actions/bulk"),
nil,
nil,
body,
@ -138,9 +138,9 @@ func SearchByAliES(keyWords string, limit, page int) (goodsList []uint, total ui
// Endpoint 为 要访问服务的区域域名.
// AccessKeyId 及AccessKeySecret 用于构造鉴权信息.
config := &Config{
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
Endpoint: tea.String(EndPoint),
AccessKeyId: tea.String(AccessKeyId),
AccessKeySecret: tea.String(AccessKeySecret),
}
// New 一个client, 用以发送请求.
@ -168,12 +168,12 @@ func SearchByAliES(keyWords string, limit, page int) (goodsList []uint, total ui
}
// 搜索接口需要提供 appName.
// App 可以是 app 的版本信息. 也可以是 app 名称.
// App 可以是 App 的版本信息. 也可以是 App 名称.
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("GET"),
tea.String("/v3/openapi/apps/"+app+"/search"),
tea.String("/v3/openapi/apps/"+App+"/search"),
requestParams,
nil,
nil,

@ -14,6 +14,7 @@ func routerTask(taskRouter *gin.RouterGroup) {
taskRouter.GET("goods/sync", goodsController.Sync) // jcook商品更新
taskRouter.GET("goods/fix", goodsController.Fix) // jcook图片修复
taskRouter.GET("goods/publish", goodsController.Publish) // 更新商品上下级状态
taskRouter.GET("goods/openSearch", goodsController.OpenSearch) // 更新openSearch
taskRouter.GET("goods/attr", goodsController.AttrSync) // jcook规格修复
taskRouter.GET("weibo/sync", goodsController.WeiboSync) // 微博数据同步

Loading…
Cancel
Save