更换搜索引擎并防止milvus使用系统代理
This commit is contained in:
		@@ -267,7 +267,27 @@ func NewSchema(config Config) (graphql.Schema, error) {
 | 
				
			|||||||
				var id_list []string
 | 
									var id_list []string
 | 
				
			||||||
				if args.Text != "" {
 | 
									if args.Text != "" {
 | 
				
			||||||
					fmt.Println("args:", args)
 | 
										fmt.Println("args:", args)
 | 
				
			||||||
					id_list = models.ElasticsearchSearch(args.Text).GetIDList(args.First, args.Last, args.After, args.Before)
 | 
										//id_list = models.ElasticsearchSearch(args.Text).GetIDList(args.First, args.Last, args.After, args.Before)
 | 
				
			||||||
 | 
										resp, err := models.ZincSearch(map[string]interface{}{
 | 
				
			||||||
 | 
											"query": map[string]interface{}{
 | 
				
			||||||
 | 
												"bool": map[string]interface{}{
 | 
				
			||||||
 | 
													"must": []map[string]interface{}{
 | 
				
			||||||
 | 
														{
 | 
				
			||||||
 | 
															"query_string": map[string]string{"query": "content:" + args.Text},
 | 
				
			||||||
 | 
														},
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
											"sort": []string{},
 | 
				
			||||||
 | 
											"from": 0,
 | 
				
			||||||
 | 
											"size": 10,
 | 
				
			||||||
 | 
										})
 | 
				
			||||||
 | 
										if err != nil {
 | 
				
			||||||
 | 
											fmt.Println("获取图像列表失败", err)
 | 
				
			||||||
 | 
											return nil, err
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										id_list = resp.ToIDList(args.First, args.Last, args.After, args.Before)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					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 == "" {
 | 
				
			||||||
						return map[string]interface{}{
 | 
											return map[string]interface{}{
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -9,8 +9,10 @@ require (
 | 
				
			|||||||
	github.com/disintegration/imaging v1.6.2
 | 
						github.com/disintegration/imaging v1.6.2
 | 
				
			||||||
	github.com/elastic/go-elasticsearch/v8 v8.11.0
 | 
						github.com/elastic/go-elasticsearch/v8 v8.11.0
 | 
				
			||||||
	github.com/graphql-go/graphql v0.8.1
 | 
						github.com/graphql-go/graphql v0.8.1
 | 
				
			||||||
 | 
						github.com/graphql-go/handler v0.2.3
 | 
				
			||||||
	github.com/jmoiron/sqlx v1.3.5
 | 
						github.com/jmoiron/sqlx v1.3.5
 | 
				
			||||||
	github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
 | 
						github.com/milvus-io/milvus-sdk-go/v2 v2.2.1
 | 
				
			||||||
 | 
						github.com/mitchellh/mapstructure v1.5.0
 | 
				
			||||||
	github.com/spf13/viper v1.15.0
 | 
						github.com/spf13/viper v1.15.0
 | 
				
			||||||
	github.com/stretchr/testify v1.8.1
 | 
						github.com/stretchr/testify v1.8.1
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -28,13 +30,11 @@ require (
 | 
				
			|||||||
	github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
 | 
						github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
 | 
				
			||||||
	github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
						github.com/golang/protobuf v1.5.2 // indirect
 | 
				
			||||||
	github.com/graphql-go/handler v0.2.3 // indirect
 | 
					 | 
				
			||||||
	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
						github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
				
			||||||
	github.com/hashicorp/hcl v1.0.0 // indirect
 | 
						github.com/hashicorp/hcl v1.0.0 // indirect
 | 
				
			||||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
						github.com/json-iterator/go v1.1.12 // indirect
 | 
				
			||||||
	github.com/magiconair/properties v1.8.7 // indirect
 | 
						github.com/magiconair/properties v1.8.7 // indirect
 | 
				
			||||||
	github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd // indirect
 | 
						github.com/milvus-io/milvus-proto/go-api v0.0.0-20230301092744-7efc6eec15fd // indirect
 | 
				
			||||||
	github.com/mitchellh/mapstructure v1.5.0 // indirect
 | 
					 | 
				
			||||||
	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
						github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 | 
				
			||||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
						github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
				
			||||||
	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
 | 
						github.com/pelletier/go-toml/v2 v2.0.6 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/milvus-io/milvus-sdk-go/v2/client"
 | 
						"github.com/milvus-io/milvus-sdk-go/v2/client"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -18,6 +19,7 @@ func (m *MilvusConnection) GetClient() client.Client {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (m *MilvusConnection) Init() (err error) {
 | 
					func (m *MilvusConnection) Init() (err error) {
 | 
				
			||||||
	log.Println("Milvus connection init")
 | 
						log.Println("Milvus connection init")
 | 
				
			||||||
 | 
						os.Setenv("NO_PROXY", config.GetString("milvus.host"))
 | 
				
			||||||
	m.Client, err = client.NewGrpcClient(context.Background(), fmt.Sprintf(
 | 
						m.Client, err = client.NewGrpcClient(context.Background(), fmt.Sprintf(
 | 
				
			||||||
		"%s:%d",
 | 
							"%s:%d",
 | 
				
			||||||
		config.GetString("milvus.host"),
 | 
							config.GetString("milvus.host"),
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										166
									
								
								models/zincsearch.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										166
									
								
								models/zincsearch.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,166 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bytes"
 | 
				
			||||||
 | 
						"encoding/base64"
 | 
				
			||||||
 | 
						"encoding/json"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 查询参数
 | 
				
			||||||
 | 
					//type Query struct {
 | 
				
			||||||
 | 
					//	Query struct {
 | 
				
			||||||
 | 
					//		Bool struct {
 | 
				
			||||||
 | 
					//			Must []struct {
 | 
				
			||||||
 | 
					//				QueryString struct {
 | 
				
			||||||
 | 
					//					Query string `json:"query"`
 | 
				
			||||||
 | 
					//				} `json:"query_string"`
 | 
				
			||||||
 | 
					//			} `json:"must"`
 | 
				
			||||||
 | 
					//		} `json:"bool"`
 | 
				
			||||||
 | 
					//	} `json:"query"`
 | 
				
			||||||
 | 
					//	Sort []string `json:"sort"`
 | 
				
			||||||
 | 
					//	From int      `json:"from"`
 | 
				
			||||||
 | 
					//	Size int      `json:"size"`
 | 
				
			||||||
 | 
					//}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 返回结果
 | 
				
			||||||
 | 
					type Response struct {
 | 
				
			||||||
 | 
						Took     int  `json:"took"`
 | 
				
			||||||
 | 
						TimedOut bool `json:"timed_out"`
 | 
				
			||||||
 | 
						Shards   struct {
 | 
				
			||||||
 | 
							Total      int `json:"total"`
 | 
				
			||||||
 | 
							Successful int `json:"successful"`
 | 
				
			||||||
 | 
							Skipped    int `json:"skipped"`
 | 
				
			||||||
 | 
							Failed     int `json:"failed"`
 | 
				
			||||||
 | 
						} `json:"_shards"`
 | 
				
			||||||
 | 
						Hits struct {
 | 
				
			||||||
 | 
							Total struct {
 | 
				
			||||||
 | 
								Value int `json:"value"`
 | 
				
			||||||
 | 
							} `json:"total"`
 | 
				
			||||||
 | 
							MaxScore float64 `json:"max_score"`
 | 
				
			||||||
 | 
							Hits     []struct {
 | 
				
			||||||
 | 
								Index     string  `json:"_index"`
 | 
				
			||||||
 | 
								Type      string  `json:"_type"`
 | 
				
			||||||
 | 
								ID        string  `json:"_id"`
 | 
				
			||||||
 | 
								Score     float64 `json:"_score"`
 | 
				
			||||||
 | 
								Timestamp string  `json:"@timestamp"`
 | 
				
			||||||
 | 
								Source    struct {
 | 
				
			||||||
 | 
									Timestamp  string `json:"@timestamp"`
 | 
				
			||||||
 | 
									Athlete    string `json:"Athlete"`
 | 
				
			||||||
 | 
									City       string `json:"City"`
 | 
				
			||||||
 | 
									Country    string `json:"Country"`
 | 
				
			||||||
 | 
									Discipline string `json:"Discipline"`
 | 
				
			||||||
 | 
									Event      string `json:"Event"`
 | 
				
			||||||
 | 
									Gender     string `json:"Gender"`
 | 
				
			||||||
 | 
									Medal      string `json:"Medal"`
 | 
				
			||||||
 | 
									Season     string `json:"Season"`
 | 
				
			||||||
 | 
									Sport      string `json:"Sport"`
 | 
				
			||||||
 | 
									Year       int    `json:"Year"`
 | 
				
			||||||
 | 
								} `json:"_source"`
 | 
				
			||||||
 | 
							} `json:"hits"`
 | 
				
			||||||
 | 
						} `json:"hits"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (res Response) ToIDList(first, last, after, before int) (id_list []string) {
 | 
				
			||||||
 | 
						for _, hit := range res.Hits.Hits {
 | 
				
			||||||
 | 
							id_list = append(id_list, hit.ID)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果 after 不为 0, 从这个ID开始向后取切片
 | 
				
			||||||
 | 
						if after != 0 {
 | 
				
			||||||
 | 
							after_str := fmt.Sprint(after)
 | 
				
			||||||
 | 
							for i, id := range id_list {
 | 
				
			||||||
 | 
								if id == after_str {
 | 
				
			||||||
 | 
									id_list = id_list[i+1:]
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果 before 不为 0, 从这个ID开始向前取切片
 | 
				
			||||||
 | 
						if before != 0 {
 | 
				
			||||||
 | 
							before_str := fmt.Sprint(before)
 | 
				
			||||||
 | 
							for i, id := range id_list {
 | 
				
			||||||
 | 
								if id == before_str {
 | 
				
			||||||
 | 
									id_list = id_list[:i]
 | 
				
			||||||
 | 
									break
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果 first 不为 0, 取切片的前 first 个元素
 | 
				
			||||||
 | 
						if first != 0 {
 | 
				
			||||||
 | 
							if first > len(id_list) {
 | 
				
			||||||
 | 
								first = len(id_list)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							id_list = id_list[:first]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 如果 last 不为 0, 取切片的后 last 个元素
 | 
				
			||||||
 | 
						if last != 0 {
 | 
				
			||||||
 | 
							if last > len(id_list) {
 | 
				
			||||||
 | 
								last = len(id_list)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							id_list = id_list[len(id_list)-last:]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ZincSearch(query map[string]interface{}) (rest Response, err error) {
 | 
				
			||||||
 | 
						const (
 | 
				
			||||||
 | 
							user     = "admin"
 | 
				
			||||||
 | 
							password = "Complexpass#123"
 | 
				
			||||||
 | 
							index    = "olympics"
 | 
				
			||||||
 | 
							zincHost = "https://zincsearch.gameui.net"
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						bas64encodedCreds := base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
 | 
				
			||||||
 | 
						zincURL := zincHost + "/es/" + index + "/_search"
 | 
				
			||||||
 | 
						queryBytes, _ := json.Marshal(query)
 | 
				
			||||||
 | 
						req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						req.Header.Set("Content-type", "application/json")
 | 
				
			||||||
 | 
						req.Header.Set("Authorization", "Basic "+bas64encodedCreds)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resp, err := http.DefaultClient.Do(req)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
						err = json.NewDecoder(resp.Body).Decode(&rest)
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ZincPut(data map[string]interface{}) (err error) {
 | 
				
			||||||
 | 
						const (
 | 
				
			||||||
 | 
							user     = "admin"
 | 
				
			||||||
 | 
							password = "Complexpass#123"
 | 
				
			||||||
 | 
							index    = "olympics"
 | 
				
			||||||
 | 
							zincHost = "https://zincsearch.gameui.net"
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						bas64encodedCreds := base64.StdEncoding.EncodeToString([]byte(user + ":" + password))
 | 
				
			||||||
 | 
						zincURL := zincHost + "/api/" + index + "/_doc"
 | 
				
			||||||
 | 
						queryBytes, _ := json.Marshal(data)
 | 
				
			||||||
 | 
						req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						req.Header.Set("Content-type", "application/json")
 | 
				
			||||||
 | 
						req.Header.Set("Authorization", "Basic "+bas64encodedCreds)
 | 
				
			||||||
 | 
						resp, err := http.DefaultClient.Do(req)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer resp.Body.Close()
 | 
				
			||||||
 | 
						// 直接作为文本打印 body
 | 
				
			||||||
 | 
						bodyBytes, err := io.ReadAll(resp.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(string(bodyBytes))
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user