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.
213 lines
5.4 KiB
213 lines
5.4 KiB
package search_ali
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
util "github.com/alibabacloud-go/tea-utils/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
)
|
|
|
|
var (
|
|
// DELETE = "DELETE"
|
|
ADD = "ADD"
|
|
)
|
|
|
|
func CreateRes(cmd string, f F) Res {
|
|
return Res{
|
|
Cmd: cmd,
|
|
Fields: f,
|
|
}
|
|
}
|
|
|
|
var (
|
|
EndPoint = "opensearch-cn-shanghai.aliyuncs.com"
|
|
AccessKeyId = "LTAI5tBeJdJBq2R9quhwGeQH"
|
|
AccessKeySecret = "sKvHrPwNWbVeDgUARAiGTluW06AQ4c"
|
|
App = "zjyc"
|
|
Table = "main"
|
|
)
|
|
|
|
func PublishMessage(res Res) {
|
|
config := &Config{
|
|
Endpoint: tea.String(EndPoint),
|
|
AccessKeyId: tea.String(AccessKeyId),
|
|
AccessKeySecret: tea.String(AccessKeySecret),
|
|
}
|
|
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)
|
|
}
|
|
|
|
type F struct {
|
|
ID uint `json:"id"`
|
|
GoodsName string `json:"goods_name"`
|
|
Cate2 string `json:"cate2"`
|
|
Cate2ID uint `json:"cate2_id"`
|
|
PublishStatus uint `json:"publish_status"`
|
|
SalePublish uint `json:"sale_publish"`
|
|
Version uint `json:"version"`
|
|
IsSale int `json:"is_sale"`
|
|
}
|
|
|
|
type Res struct {
|
|
Cmd string `json:"cmd"`
|
|
Fields F `json:"fields"`
|
|
}
|
|
|
|
func PushMessage() {
|
|
config := &Config{
|
|
Endpoint: tea.String(EndPoint),
|
|
AccessKeyId: tea.String(AccessKeyId),
|
|
AccessKeySecret: tea.String(AccessKeySecret),
|
|
}
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
func SearchByAliES(keyWords string, isSale bool, limit, page int) (goodsList []uint, total uint, err error) {
|
|
// 创建请求用客户端实例
|
|
// Endpoint 为 要访问服务的区域域名.
|
|
// AccessKeyId 及AccessKeySecret 用于构造鉴权信息.
|
|
config := &Config{
|
|
Endpoint: tea.String(EndPoint),
|
|
AccessKeyId: tea.String(AccessKeyId),
|
|
AccessKeySecret: tea.String(AccessKeySecret),
|
|
}
|
|
|
|
// New 一个client, 用以发送请求.
|
|
client, _clientErr := NewClient(config)
|
|
|
|
// 如果 NewClient 过程中出现异常. 则 返回 _clientErr 且输出 错误信息.
|
|
if _clientErr != nil {
|
|
fmt.Println(_clientErr)
|
|
return
|
|
}
|
|
|
|
query := fmt.Sprintf("config=start:%v,hit:%v,format:fulljson&&query=default:'%s'&&filter=publish_status=1", limit*(page), limit, keyWords)
|
|
if isSale {
|
|
query = fmt.Sprintf("config=start:%v,hit:%v,format:fulljson&&query=default:'%s'&&filter=sale_publish=1", limit*(page), limit, keyWords)
|
|
}
|
|
// requestParams 信息
|
|
requestParams := map[string]interface{}{
|
|
"query": query,
|
|
"raw_query": keyWords,
|
|
}
|
|
|
|
// 请求发送的配置参数. 用以请求配置及连接池配置.
|
|
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"),
|
|
tea.String("/v3/openapi/apps/"+App+"/search"),
|
|
requestParams,
|
|
nil,
|
|
nil,
|
|
runtime)
|
|
|
|
// 如果 发送请求 过程中出现异常. 则 返回 _requestErr 且输出 错误信息.
|
|
if _requestErr != nil {
|
|
fmt.Println(_requestErr)
|
|
return []uint{}, 0, errors.New("网络异常")
|
|
}
|
|
|
|
// 输出正常返回的 response 内容.
|
|
body, _ := json.MarshalIndent(response, "", " ")
|
|
var gl RecookSearchModel
|
|
fmt.Println(string(body))
|
|
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
|
|
}
|