From fdb3edfb61924923429df88efd423e6947832528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Mon, 20 Nov 2023 03:26:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E6=A0=87=E7=AD=BE=E6=90=9C?= =?UTF-8?q?=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/main.go | 31 +++++++++----- go.mod | 2 + go.sum | 4 ++ models/elasticsearch.go | 89 +++++++++++++++++++++++++++++++++++++++++ models/milvus.go | 7 ++-- 5 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 models/elasticsearch.go diff --git a/bin/main.go b/bin/main.go index 8c20814..1d3ebfd 100644 --- a/bin/main.go +++ b/bin/main.go @@ -191,21 +191,11 @@ func main() { return } - // test ocr - //client := gosseract.NewClient() - //defer client.Close() - //client.SetLanguage("chi_sim") - //client.SetImage("/home/stori/webp/data/tt.jpeg") - //text, _ := client.Text() - //fmt.Println(text) - //fmt.Println("=======================================") - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志 w.Write([]byte("Hello World!")) }) - // 屏蔽 /favicon.ico http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志 http.Error(w, "Not Found", http.StatusNotFound) @@ -288,6 +278,27 @@ func main() { return list } + // 如果是查询 text, 直接从 Elasticsearch 返回结果 + if text := QueryConditions("text"); len(text) > 0 { + + rest := models.ElasticsearchSearch(strings.Join(text, " ")) + + // 获取图片列表 + // 是否有下一页 + //images.Next = images.Total > images.Page*images.PageSize + + // 将对象转换为有缩进的JSON输出 + data, err := json.MarshalIndent(rest, "", " ") + if err != nil { + log.Println("转换图片列表失败", err) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.Write(data) + return + } + // 拼接查询条件 var addCondition = func(conditions *strings.Builder, key, column string) { if values := QueryConditions(key); len(values) > 0 { diff --git a/go.mod b/go.mod index 2878fad..8642605 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,8 @@ require ( github.com/alibabacloud-go/tea-xml v1.1.2 // indirect github.com/aliyun/credentials-go v1.1.2 // indirect github.com/clbanning/mxj/v2 v2.5.6 // indirect + github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect + github.com/elastic/go-elasticsearch/v8 v8.11.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect diff --git a/go.sum b/go.sum index 9567222..7ae2da6 100644 --- a/go.sum +++ b/go.sum @@ -85,6 +85,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/elastic/elastic-transport-go/v8 v8.3.0 h1:DJGxovyQLXGr62e9nDMPSxRyWION0Bh6d9eCFBriiHo= +github.com/elastic/elastic-transport-go/v8 v8.3.0/go.mod h1:87Tcz8IVNe6rVSLdBux1o/PEItLtyabHU3naC7IoqKI= +github.com/elastic/go-elasticsearch/v8 v8.11.0 h1:gUazf443rdYAEAD7JHX5lSXRgTkG4N4IcsV8dcWQPxM= +github.com/elastic/go-elasticsearch/v8 v8.11.0/go.mod h1:GU1BJHO7WeamP7UhuElYwzzHtvf9SDmeVpSSy9+o6Qg= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= diff --git a/models/elasticsearch.go b/models/elasticsearch.go new file mode 100644 index 0000000..41e15fc --- /dev/null +++ b/models/elasticsearch.go @@ -0,0 +1,89 @@ +package models + +import ( + "bytes" + "context" + "crypto/tls" + "encoding/json" + "log" + "net/http" + + "github.com/elastic/go-elasticsearch/v8" +) + +func elasticsearch_init() (es *elasticsearch.Client) { + es, err := elasticsearch.NewClient(elasticsearch.Config{ + Addresses: []string{ + Viper.Get("elasticsearch.host").(string), + }, + Username: Viper.Get("elasticsearch.user").(string), + Password: Viper.Get("elasticsearch.password").(string), + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + }, + }) + if err != nil { + log.Printf("Error creating the client: %s", err) + return nil + } + return es +} + +func ElasticsearchSearch(text string) interface{} { + var ( + r map[string]interface{} + ) + + // 通过字符串构建查询 + var buf bytes.Buffer + query := map[string]interface{}{ + "query": map[string]interface{}{ + "match": map[string]interface{}{ + "content": text, + }, + }, + } + if err := json.NewEncoder(&buf).Encode(query); err != nil { + log.Fatalf("Error encoding query: %s", err) + } + + es := elasticsearch_init() + + // Perform the search request. + res, err := es.Search( + es.Search.WithContext(context.Background()), + es.Search.WithIndex("news"), + es.Search.WithBody(&buf), + es.Search.WithTrackTotalHits(true), + es.Search.WithPretty(), + ) + if err != nil { + log.Fatalf("Error getting response: %s", err) + } + defer res.Body.Close() + + // Check response status + if res.IsError() { + log.Fatalf("Error: %s", res.String()) + } + + // Deserialize the response into a map. + if err := json.NewDecoder(res.Body).Decode(&r); err != nil { + log.Fatalf("Error parsing the response body: %s", err) + } + + // Print the response status, number of results, and request duration. + log.Printf( + "[%s] %d hits; took: %dms", + res.Status(), + int(r["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64)), + int(r["took"].(float64)), + ) + + // Print the ID and document source for each hit. + for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) { + log.Printf(" * ID=%s, %s", hit.(map[string]interface{})["_id"], hit.(map[string]interface{})["_source"]) + } + + return r +} diff --git a/models/milvus.go b/models/milvus.go index f94c985..4f3a782 100644 --- a/models/milvus.go +++ b/models/milvus.go @@ -20,9 +20,10 @@ func (m *MilvusConnection) Init() (err error) { log.Println("Milvus connection init") host := Viper.Get("milvus.host").(string) port := Viper.Get("milvus.port").(int) - address := fmt.Sprintf("%s:%d", host, port) - log.Println("Milvus address:", address) - m.Client, err = client.NewGrpcClient(context.Background(), address) + m.Client, err = client.NewGrpcClient( + context.Background(), + fmt.Sprintf("%s:%d", host, port), + ) if err != nil { log.Println("Milvus connection failed:", err) return