游标修正
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -191,16 +192,16 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
|
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
|
||||||
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
|
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
|
||||||
"after": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
|
"after": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
|
||||||
"before": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
|
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
|
||||||
},
|
},
|
||||||
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||||
// 定义参数结构体
|
// 定义参数结构体
|
||||||
var args struct {
|
var args struct {
|
||||||
First int
|
First int
|
||||||
Last int
|
Last int
|
||||||
After int
|
After string
|
||||||
Before int
|
Before string
|
||||||
Text string
|
Text string
|
||||||
}
|
}
|
||||||
mapstructure.Decode(p.Args, &args)
|
mapstructure.Decode(p.Args, &args)
|
||||||
@@ -273,7 +274,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
|
|||||||
"bool": map[string]interface{}{
|
"bool": map[string]interface{}{
|
||||||
"must": []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,
|
"size": 10,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("获取图像列表失败", err)
|
fmt.Println("ZincSearch 获取图像列表失败", err)
|
||||||
return nil, 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)
|
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)), ","), "[]")
|
id_list_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(id_list)), ","), "[]")
|
||||||
if id_list_str == "" {
|
if id_list_str == "" {
|
||||||
|
@@ -6,7 +6,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 查询参数
|
// 查询参数
|
||||||
@@ -63,16 +65,16 @@ type Response struct {
|
|||||||
} `json:"hits"`
|
} `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 {
|
for _, hit := range res.Hits.Hits {
|
||||||
|
fmt.Println(hit)
|
||||||
id_list = append(id_list, hit.ID)
|
id_list = append(id_list, hit.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果 after 不为 0, 从这个ID开始向后取切片
|
// 如果 after 不为 0, 从这个ID开始向后取切片
|
||||||
if after != 0 {
|
if after != "" {
|
||||||
after_str := fmt.Sprint(after)
|
|
||||||
for i, id := range id_list {
|
for i, id := range id_list {
|
||||||
if id == after_str {
|
if id == after {
|
||||||
id_list = id_list[i+1:]
|
id_list = id_list[i+1:]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -80,10 +82,9 @@ func (res Response) ToIDList(first, last, after, before int) (id_list []string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 如果 before 不为 0, 从这个ID开始向前取切片
|
// 如果 before 不为 0, 从这个ID开始向前取切片
|
||||||
if before != 0 {
|
if before != "" {
|
||||||
before_str := fmt.Sprint(before)
|
|
||||||
for i, id := range id_list {
|
for i, id := range id_list {
|
||||||
if id == before_str {
|
if id == before {
|
||||||
id_list = id_list[:i]
|
id_list = id_list[:i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -121,17 +122,39 @@ func ZincSearch(query map[string]interface{}) (rest Response, err error) {
|
|||||||
queryBytes, _ := json.Marshal(query)
|
queryBytes, _ := json.Marshal(query)
|
||||||
req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
|
req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("创建请求失败", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-type", "application/json")
|
req.Header.Set("Content-type", "application/json")
|
||||||
req.Header.Set("Authorization", "Basic "+bas64encodedCreds)
|
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 {
|
if err != nil {
|
||||||
|
log.Println("发起请求失败", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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)
|
err = json.NewDecoder(resp.Body).Decode(&rest)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("解析响应失败", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user