更换搜索引擎并防止milvus使用系统代理
This commit is contained in:
		@@ -4,6 +4,7 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"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) {
 | 
			
		||||
	log.Println("Milvus connection init")
 | 
			
		||||
	os.Setenv("NO_PROXY", config.GetString("milvus.host"))
 | 
			
		||||
	m.Client, err = client.NewGrpcClient(context.Background(), fmt.Sprintf(
 | 
			
		||||
		"%s:%d",
 | 
			
		||||
		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