引入标签搜索
This commit is contained in:
		
							
								
								
									
										31
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -191,21 +191,11 @@ func main() {
 | 
				
			|||||||
		return
 | 
							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) {
 | 
						http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
		w.Write([]byte("Hello World!"))
 | 
							w.Write([]byte("Hello World!"))
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 屏蔽 /favicon.ico
 | 
					 | 
				
			||||||
	http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 | 
						http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
		defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
							defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
 | 
				
			||||||
		http.Error(w, "Not Found", http.StatusNotFound)
 | 
							http.Error(w, "Not Found", http.StatusNotFound)
 | 
				
			||||||
@@ -288,6 +278,27 @@ func main() {
 | 
				
			|||||||
			return list
 | 
								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) {
 | 
							var addCondition = func(conditions *strings.Builder, key, column string) {
 | 
				
			||||||
			if values := QueryConditions(key); len(values) > 0 {
 | 
								if values := QueryConditions(key); len(values) > 0 {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -20,6 +20,8 @@ require (
 | 
				
			|||||||
	github.com/alibabacloud-go/tea-xml v1.1.2 // indirect
 | 
						github.com/alibabacloud-go/tea-xml v1.1.2 // indirect
 | 
				
			||||||
	github.com/aliyun/credentials-go v1.1.2 // indirect
 | 
						github.com/aliyun/credentials-go v1.1.2 // indirect
 | 
				
			||||||
	github.com/clbanning/mxj/v2 v2.5.6 // 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/fsnotify/fsnotify v1.6.0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
						github.com/golang/protobuf v1.5.2 // indirect
 | 
				
			||||||
	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
						github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								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/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 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
 | 
				
			||||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
 | 
					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.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.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
 | 
				
			||||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
 | 
					github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										89
									
								
								models/elasticsearch.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								models/elasticsearch.go
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -20,9 +20,10 @@ func (m *MilvusConnection) Init() (err error) {
 | 
				
			|||||||
	log.Println("Milvus connection init")
 | 
						log.Println("Milvus connection init")
 | 
				
			||||||
	host := Viper.Get("milvus.host").(string)
 | 
						host := Viper.Get("milvus.host").(string)
 | 
				
			||||||
	port := Viper.Get("milvus.port").(int)
 | 
						port := Viper.Get("milvus.port").(int)
 | 
				
			||||||
	address := fmt.Sprintf("%s:%d", host, port)
 | 
						m.Client, err = client.NewGrpcClient(
 | 
				
			||||||
	log.Println("Milvus address:", address)
 | 
							context.Background(),
 | 
				
			||||||
	m.Client, err = client.NewGrpcClient(context.Background(), address)
 | 
							fmt.Sprintf("%s:%d", host, port),
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println("Milvus connection failed:", err)
 | 
							log.Println("Milvus connection failed:", err)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user