You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
4.8 KiB

4 years ago
package search_ali
import (
"encoding/json"
"errors"
"fmt"
4 years ago
"os"
"strconv"
4 years ago
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/alibabacloud-go/tea/tea"
)
4 years ago
type F struct {
ID uint `json:"id"`
GoodsName string `json:"goods_name"`
}
type Res struct {
Cmd string `json:"cmd"`
Fields F `json:"fields"`
}
4 years ago
var (
DELETE = "DELETE"
ADD = "ADD"
)
func CreateRes(cmd string, f F) Res {
return Res{
Cmd: cmd,
Fields: f,
}
}
4 years ago
var (
endPoint = "intranet.opensearch-cn-shanghai.aliyuncs.com"
aid = "LTAI5tR6hFZwtJLKFq4Sxibg"
ask = "pn0fKjGY7zoLzWdGjjcxJP95jrAhAn"
app = "zjyc"
table = "main"
)
4 years ago
func PublishMessage(res Res) {
config := &Config{
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
}
client, _clientErr := 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),
}
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("POST"),
tea.String("/v3/openapi/apps/"+app+"/"+table+"/actions/bulk"),
nil,
nil,
[]interface{}{res},
runTime)
// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// 输出正常返回的 response 内容.
fmt.Println(response)
}
4 years ago
func PushMessage() {
config := &Config{
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
}
client, _clientErr := NewClient(config)
// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
data, _ := os.ReadFile("goods_info.json")
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([]Res, 0)
json.Unmarshal(data, &requestBody)
for i := 0; i <= len(requestBody); i += 1000 {
body := make([]Res, 0)
if i+1000 > len(requestBody) {
body = requestBody[i:]
} else {
body = requestBody[i : i+1000]
}
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("POST"),
tea.String("/v3/openapi/apps/"+app+"/"+table+"/actions/bulk"),
nil,
nil,
body,
runTime)
// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
if _requestErr != nil {
fmt.Println(_requestErr)
return
}
// 输出正常返回的 response 内容.
fmt.Println(response)
}
}
4 years ago
func SearchByAliES(keyWords string, limit, page int) (goodsList []uint, total uint, err error) {
// 创建请求用客户端实例
// Endpoint 为 要访问服务的区域域名.
// AccessKeyId 及AccessKeySecret 用于构造鉴权信息.
config := &Config{
4 years ago
Endpoint: tea.String(endPoint),
AccessKeyId: tea.String(aid),
AccessKeySecret: tea.String(ask),
4 years ago
}
// New 一个client, 用以发送请求.
client, _clientErr := NewClient(config)
// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
if _clientErr != nil {
fmt.Println(_clientErr)
return
}
// requestParams 信息
requestParams := map[string]interface{}{
4 years ago
"query": fmt.Sprintf("config=start:%v,hit:%v,format:fulljson&&query=default:'%s'", 20*(page-1), limit, keyWords),
4 years ago
}
// 请求发送的配置参数. 用以请求配置及连接池配置.
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(5000),
ReadTimeout: tea.Int(10000),
Autoretry: tea.Bool(false),
IgnoreSSL: tea.Bool(false),
MaxIdleConns: tea.Int(50),
}
// 搜索接口需要提供 appName.
// App 可以是 app 的版本信息. 也可以是 app 名称.
// 发送请求的方法调用.
response, _requestErr := client.Request(
tea.String("GET"),
4 years ago
tea.String("/v3/openapi/apps/"+app+"/search"),
4 years ago
requestParams,
nil,
nil,
runtime)
// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
if _requestErr != nil {
fmt.Println(_requestErr)
return []uint{}, 0, errors.New("网络异常")
}
// 输出正常返回的 response 内容.
body, _ := json.MarshalIndent(response, "", " ")
var gl RecookSearchModel
err = json.Unmarshal(body, &gl)
if err != nil {
return []uint{}, 0, errors.New("网络异常2")
}
for _, item := range gl.Body.Result.Items {
n, _ := strconv.Atoi(item.Fields.Id)
goodsList = append(goodsList, uint(n))
}
total = gl.Body.Result.Total
return
}