游标修正

This commit is contained in:
2023-12-07 02:19:34 +08:00
parent 04e1456eaa
commit 5af7f0d5c1
2 changed files with 42 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"encoding/json"
"fmt"
"log"
"strings"
@@ -191,16 +192,16 @@ func NewSchema(config Config) (graphql.Schema, error) {
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
"before": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
// 定义参数结构体
var args struct {
First int
Last int
After int
Before int
After string
Before string
Text string
}
mapstructure.Decode(p.Args, &args)
@@ -273,7 +274,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
"bool": map[string]interface{}{
"must": []map[string]interface{}{
{
"query_string": map[string]string{"query": "content:" + args.Text},
"query_string": map[string]string{"query": "City:" + args.Text},
},
},
},
@@ -283,10 +284,14 @@ func NewSchema(config Config) (graphql.Schema, error) {
"size": 10,
})
if err != nil {
fmt.Println("获取图像列表失败", err)
fmt.Println("ZincSearch 获取图像列表失败", err)
return nil, err
}
// 格式化为带缩进的JSON打印
data, _ := json.MarshalIndent(resp, "", " ")
fmt.Println("JSON", string(data))
id_list = resp.ToIDList(args.First, args.Last, args.After, args.Before)
fmt.Println("id_list:", id_list)
id_list_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(id_list)), ","), "[]")
if id_list_str == "" {

View File

@@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
)
// 查询参数
@@ -63,16 +65,16 @@ type Response struct {
} `json:"hits"`
}
func (res Response) ToIDList(first, last, after, before int) (id_list []string) {
func (res Response) ToIDList(first, last int, after, before string) (id_list []string) {
for _, hit := range res.Hits.Hits {
fmt.Println(hit)
id_list = append(id_list, hit.ID)
}
// 如果 after 不为 0, 从这个ID开始向后取切片
if after != 0 {
after_str := fmt.Sprint(after)
if after != "" {
for i, id := range id_list {
if id == after_str {
if id == after {
id_list = id_list[i+1:]
break
}
@@ -80,10 +82,9 @@ func (res Response) ToIDList(first, last, after, before int) (id_list []string)
}
// 如果 before 不为 0, 从这个ID开始向前取切片
if before != 0 {
before_str := fmt.Sprint(before)
if before != "" {
for i, id := range id_list {
if id == before_str {
if id == before {
id_list = id_list[:i]
break
}
@@ -121,17 +122,39 @@ func ZincSearch(query map[string]interface{}) (rest Response, err error) {
queryBytes, _ := json.Marshal(query)
req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
if err != nil {
log.Println("创建请求失败", err)
return
}
req.Header.Set("Content-type", "application/json")
req.Header.Set("Authorization", "Basic "+bas64encodedCreds)
resp, err := http.DefaultClient.Do(req)
// 创建一个新的 HTTP 客户端
customClient := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return nil, nil
},
},
}
resp, err := customClient.Do(req)
if err != nil {
log.Println("发起请求失败", err)
return
}
defer resp.Body.Close()
// 检查状态码
if resp.StatusCode != http.StatusOK {
log.Println("请求未成功,状态码:", resp.StatusCode)
return rest, fmt.Errorf("请求未成功,状态码:%d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(&rest)
if err != nil {
log.Println("解析响应失败", err)
return
}
return
}