elasticsearch 移除
This commit is contained in:
		
							
								
								
									
										54
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -285,47 +285,8 @@ func main() {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
			return list
 | 
								return list
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// 拼接基本查询条件
 | 
					
 | 
				
			||||||
		var addCondition = func(conditions *strings.Builder, key, column string) {
 | 
					 | 
				
			||||||
			if values := QueryConditions(key); len(values) > 0 {
 | 
					 | 
				
			||||||
				if conditions.Len() > 0 {
 | 
					 | 
				
			||||||
					conditions.WriteString(" AND")
 | 
					 | 
				
			||||||
				} else {
 | 
					 | 
				
			||||||
					conditions.WriteString(" WHERE")
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				conditions.WriteString(fmt.Sprintf(" %s IN (%s)", column, strings.Join(values, ",")))
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		var conditions strings.Builder
 | 
							var conditions strings.Builder
 | 
				
			||||||
		// 如果是查询 text, 直接从 Elasticsearch 返回结果
 | 
					 | 
				
			||||||
		var text_ids []string
 | 
					 | 
				
			||||||
		if text := QueryConditions("text"); len(text) > 0 {
 | 
					 | 
				
			||||||
			text_ids := models.ElasticsearchSearch(strings.Join(text, " ")).GetIDList(0, 0, 0, 0)
 | 
					 | 
				
			||||||
			if len(text_ids) > 0 {
 | 
					 | 
				
			||||||
				conditions.WriteString(fmt.Sprintf(" WHERE id IN (%s)", strings.Trim(strings.Replace(fmt.Sprint(text_ids), " ", ",", -1), "[]")))
 | 
					 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				// 直接返回空列表
 | 
					 | 
				
			||||||
				var images ListView
 | 
					 | 
				
			||||||
				images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 20)
 | 
					 | 
				
			||||||
				images.Total = 0
 | 
					 | 
				
			||||||
				images.Next = false
 | 
					 | 
				
			||||||
				images.List = make([]interface{}, 0)
 | 
					 | 
				
			||||||
				data, _ := json.MarshalIndent(images, "", "  ")
 | 
					 | 
				
			||||||
				w.Header().Set("Content-Type", "application/json; charset=UTF-8")
 | 
					 | 
				
			||||||
				w.Write(data)
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if conditions.Len() > 1024 {
 | 
					 | 
				
			||||||
				log.Println("查询条件过长")
 | 
					 | 
				
			||||||
				http.Error(w, err.Error(), http.StatusBadRequest)
 | 
					 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			addCondition(&conditions, "authors", "author")
 | 
					 | 
				
			||||||
			addCondition(&conditions, "tags", "tag")
 | 
					 | 
				
			||||||
			addCondition(&conditions, "categories", "categorie")
 | 
					 | 
				
			||||||
			addCondition(&conditions, "sets", "sets")
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 获取图片列表
 | 
							// 获取图片列表
 | 
				
			||||||
		var images ListView
 | 
							var images ListView
 | 
				
			||||||
@@ -393,19 +354,6 @@ func main() {
 | 
				
			|||||||
			image_list = image_list_sorted
 | 
								image_list = image_list_sorted
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// 如果使用了图像文字检索, 需要按照图像文字检索的相似度重新排序 text_ids
 | 
					 | 
				
			||||||
		if len(text_ids) > 0 {
 | 
					 | 
				
			||||||
			var image_list_sorted []Image
 | 
					 | 
				
			||||||
			for _, id := range text_ids {
 | 
					 | 
				
			||||||
				for _, image := range image_list {
 | 
					 | 
				
			||||||
					if id == strconv.Itoa(image.Id) {
 | 
					 | 
				
			||||||
						image_list_sorted = append(image_list_sorted, image)
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			image_list = image_list_sorted
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		// 用户ID, 文章ID
 | 
							// 用户ID, 文章ID
 | 
				
			||||||
		var user_ids []int
 | 
							var user_ids []int
 | 
				
			||||||
		var article_ids []int
 | 
							var article_ids []int
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								go.mod
									
									
									
									
									
								
							@@ -9,7 +9,6 @@ require (
 | 
				
			|||||||
	github.com/alibabacloud-go/tea v1.2.1
 | 
						github.com/alibabacloud-go/tea v1.2.1
 | 
				
			||||||
	github.com/alibabacloud-go/vod-20170321/v2 v2.16.10
 | 
						github.com/alibabacloud-go/vod-20170321/v2 v2.16.10
 | 
				
			||||||
	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/graphql-go/graphql v0.8.1
 | 
						github.com/graphql-go/graphql v0.8.1
 | 
				
			||||||
	github.com/graphql-go/handler v0.2.3
 | 
						github.com/graphql-go/handler v0.2.3
 | 
				
			||||||
	github.com/hashicorp/golang-lru/v2 v2.0.7
 | 
						github.com/hashicorp/golang-lru/v2 v2.0.7
 | 
				
			||||||
@@ -17,7 +16,6 @@ require (
 | 
				
			|||||||
	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.1-0.20231216201459-8508981c8b6c
 | 
						github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c
 | 
				
			||||||
	github.com/spf13/viper v1.15.0
 | 
						github.com/spf13/viper v1.15.0
 | 
				
			||||||
	github.com/stretchr/testify v1.9.0
 | 
					 | 
				
			||||||
	github.com/thoas/go-funk v0.9.3
 | 
						github.com/thoas/go-funk v0.9.3
 | 
				
			||||||
	gorm.io/driver/mysql v1.5.7
 | 
						gorm.io/driver/mysql v1.5.7
 | 
				
			||||||
	gorm.io/gorm v1.25.12
 | 
						gorm.io/gorm v1.25.12
 | 
				
			||||||
@@ -32,8 +30,6 @@ 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.7.0 // indirect
 | 
						github.com/clbanning/mxj/v2 v2.7.0 // indirect
 | 
				
			||||||
	github.com/davecgh/go-spew v1.1.1 // indirect
 | 
					 | 
				
			||||||
	github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
 | 
					 | 
				
			||||||
	github.com/fsnotify/fsnotify v1.8.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.8.0 // indirect
 | 
				
			||||||
	github.com/golang/protobuf v1.5.4 // indirect
 | 
						github.com/golang/protobuf v1.5.4 // indirect
 | 
				
			||||||
	github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
						github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
 | 
				
			||||||
@@ -46,13 +42,13 @@ require (
 | 
				
			|||||||
	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.2.3 // indirect
 | 
						github.com/pelletier/go-toml/v2 v2.2.3 // indirect
 | 
				
			||||||
	github.com/pmezard/go-difflib v1.0.0 // indirect
 | 
					 | 
				
			||||||
	github.com/rogpeppe/go-internal v1.12.0 // indirect
 | 
						github.com/rogpeppe/go-internal v1.12.0 // indirect
 | 
				
			||||||
	github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9 // indirect
 | 
						github.com/sizeofint/webp-animation v0.0.0-20190207194838-b631dc900de9 // indirect
 | 
				
			||||||
	github.com/spf13/afero v1.11.0 // indirect
 | 
						github.com/spf13/afero v1.11.0 // indirect
 | 
				
			||||||
	github.com/spf13/cast v1.7.0 // indirect
 | 
						github.com/spf13/cast v1.7.0 // indirect
 | 
				
			||||||
	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 | 
						github.com/spf13/jwalterweatherman v1.1.0 // indirect
 | 
				
			||||||
	github.com/spf13/pflag v1.0.5 // indirect
 | 
						github.com/spf13/pflag v1.0.5 // indirect
 | 
				
			||||||
 | 
						github.com/stretchr/objx v0.5.2 // indirect
 | 
				
			||||||
	github.com/subosito/gotenv v1.4.2 // indirect
 | 
						github.com/subosito/gotenv v1.4.2 // indirect
 | 
				
			||||||
	github.com/tjfoc/gmsm v1.3.2 // indirect
 | 
						github.com/tjfoc/gmsm v1.3.2 // indirect
 | 
				
			||||||
	golang.org/x/net v0.28.0 // indirect
 | 
						golang.org/x/net v0.28.0 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -43,10 +43,6 @@ 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=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,151 +0,0 @@
 | 
				
			|||||||
package models
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"context"
 | 
					 | 
				
			||||||
	"crypto/tls"
 | 
					 | 
				
			||||||
	"encoding/json"
 | 
					 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"net/http"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/elastic/go-elasticsearch/v8"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func elasticsearch_init() (es *elasticsearch.Client) {
 | 
					 | 
				
			||||||
	es, err := elasticsearch.NewClient(elasticsearch.Config{
 | 
					 | 
				
			||||||
		Addresses: []string{config.GetString("elasticsearch.host")},
 | 
					 | 
				
			||||||
		Username:  config.GetString("elasticsearch.user"),
 | 
					 | 
				
			||||||
		Password:  config.GetString("elasticsearch.password"),
 | 
					 | 
				
			||||||
		Transport: &http.Transport{
 | 
					 | 
				
			||||||
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
 | 
					 | 
				
			||||||
		},
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Printf("Error creating the client: %s", err)
 | 
					 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return es
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type SearchData struct {
 | 
					 | 
				
			||||||
	_shards struct {
 | 
					 | 
				
			||||||
		failed     int
 | 
					 | 
				
			||||||
		skipped    int
 | 
					 | 
				
			||||||
		successful int
 | 
					 | 
				
			||||||
		total      int
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	Hits struct {
 | 
					 | 
				
			||||||
		Hits []struct {
 | 
					 | 
				
			||||||
			ID     string  `json:"_id"`
 | 
					 | 
				
			||||||
			Index  string  `json:"_index"`
 | 
					 | 
				
			||||||
			Score  float64 `json:"_score"`
 | 
					 | 
				
			||||||
			Source struct {
 | 
					 | 
				
			||||||
				Content string `json:"content"`
 | 
					 | 
				
			||||||
			} `json:"_source"`
 | 
					 | 
				
			||||||
			Type string `json:"_type"`
 | 
					 | 
				
			||||||
		} `json:"hits"`
 | 
					 | 
				
			||||||
		max_score float64
 | 
					 | 
				
			||||||
		total     struct {
 | 
					 | 
				
			||||||
			relation string
 | 
					 | 
				
			||||||
			value    int
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} `json:"hits"`
 | 
					 | 
				
			||||||
	timed_out bool
 | 
					 | 
				
			||||||
	took      int
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 获取搜索结果的 ID 列表
 | 
					 | 
				
			||||||
func (sd SearchData) GetIDList(first, last, after, before int) (id_list []string) {
 | 
					 | 
				
			||||||
	for _, hit := range sd.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 id_list
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 获取搜索结果的内容列表
 | 
					 | 
				
			||||||
func ElasticsearchSearch(text string) (r SearchData) {
 | 
					 | 
				
			||||||
	// 通过字符串构建查询
 | 
					 | 
				
			||||||
	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.Printf("Error encoding query: %s", err)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	es := elasticsearch_init()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 执行查询(最大返回200条)
 | 
					 | 
				
			||||||
	res, err := es.Search(
 | 
					 | 
				
			||||||
		es.Search.WithContext(context.Background()),
 | 
					 | 
				
			||||||
		es.Search.WithIndex("web_images"),
 | 
					 | 
				
			||||||
		es.Search.WithBody(&buf),
 | 
					 | 
				
			||||||
		es.Search.WithTrackTotalHits(true),
 | 
					 | 
				
			||||||
		es.Search.WithPretty(),
 | 
					 | 
				
			||||||
		es.Search.WithSize(200),
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Printf("Error getting response: %s", err)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer res.Body.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 处理错误
 | 
					 | 
				
			||||||
	if res.IsError() {
 | 
					 | 
				
			||||||
		log.Printf("Error: %s", res.String())
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 转换返回结果
 | 
					 | 
				
			||||||
	if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
 | 
					 | 
				
			||||||
		log.Printf("Error parsing the response body: %s", err)
 | 
					 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return r
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@@ -1,16 +0,0 @@
 | 
				
			|||||||
package models
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					 | 
				
			||||||
	"testing"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestElasticsearchSearch(t *testing.T) {
 | 
					 | 
				
			||||||
	// 创建一个测试用例
 | 
					 | 
				
			||||||
	expected := "植物学家 阿尔法 可复活一次 技能:召唤豌豆射手 转到设置"
 | 
					 | 
				
			||||||
	actual := ElasticsearchSearch("豌豆")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// 使用 assert 包中的函数来验证函数的输出
 | 
					 | 
				
			||||||
	assert.Equal(t, expected, actual.Hits.Hits[0].Source.Content)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user