From 5f0e76c0bbb935fa243e512b1d08bec72e39111b Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Tue, 12 Nov 2024 12:41:57 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=86=95real=20link=20es=20#1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/global/my_errors/my_errors.go | 3 +++ app/global/variable/variable.go | 5 ++++- app/http/controller/web/encounter_controller.go | 4 +++- bootstrap/init.go | 9 +++++++++ config/config.yml | 5 +++++ test/es/init.go | 2 +- 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/global/my_errors/my_errors.go b/app/global/my_errors/my_errors.go index c3efa00..6e5ee35 100644 --- a/app/global/my_errors/my_errors.go +++ b/app/global/my_errors/my_errors.go @@ -71,4 +71,7 @@ const ( // GLM 部分 ErrorsGlmClientInitFail string = "GLM Client 初始化失败" + + // ES 部分 + ErrorsInitConnFail string = "初始化 Elasticsearch 连接失败" ) diff --git a/app/global/variable/variable.go b/app/global/variable/variable.go index 08d8beb..c963c74 100644 --- a/app/global/variable/variable.go +++ b/app/global/variable/variable.go @@ -9,10 +9,10 @@ import ( "strings" "github.com/casbin/casbin/v2" + "github.com/elastic/go-elasticsearch/v8" "github.com/yankeguo/zhipu" "go.uber.org/zap" "gorm.io/gorm" - ) var ( @@ -46,6 +46,9 @@ var ( // GLM 全局客户端 GlmClient *zhipu.Client + + // ES 全局客户端 + ElasticClient *elasticsearch.Client ) func init() { diff --git a/app/http/controller/web/encounter_controller.go b/app/http/controller/web/encounter_controller.go index 30d161a..45bbb6f 100644 --- a/app/http/controller/web/encounter_controller.go +++ b/app/http/controller/web/encounter_controller.go @@ -74,7 +74,9 @@ func (e *Encounters) Create(context *gin.Context) { return } // 3. ES speed - // TODO + if level := int(context.GetFloat64(consts.ValidatorPrefix + "level")); level > 1 { + // TODO + } response.Success(context, consts.CurdStatusOkMsg, gin.H{ "encounter_id": encounter_id, diff --git a/bootstrap/init.go b/bootstrap/init.go index 5be6992..862fddd 100644 --- a/bootstrap/init.go +++ b/bootstrap/init.go @@ -16,6 +16,7 @@ import ( "log" "os" + "github.com/elastic/go-elasticsearch/v8" "github.com/yankeguo/zhipu" ) @@ -119,4 +120,12 @@ func init() { if err != nil { log.Fatal(my_errors.ErrorsGlmClientInitFail + err.Error()) } + + // 12. ES 客户端启动 + variable.ElasticClient, err = elasticsearch.NewClient(elasticsearch.Config{ + Addresses: []string{variable.ConfigYml.GetString("ElasticSearch.Addr")}, + }) + if err != nil { + log.Fatal(my_errors.ErrorsInitConnFail + err.Error()) + } } diff --git a/config/config.yml b/config/config.yml index 4769dbc..c5dddae 100644 --- a/config/config.yml +++ b/config/config.yml @@ -155,4 +155,9 @@ Weixin: Glm: ApiKey: "0cf510ebc01599dba2a593069c1bdfbc.nQBQ4skP8xBh7ijU" DefaultModel: "glm-4-flash" + +ElasticSearch: + Addr: "http://localhost:9200" + UserName: "elastic" + Password: "" \ No newline at end of file diff --git a/test/es/init.go b/test/es/init.go index 00560d4..9890b1f 100644 --- a/test/es/init.go +++ b/test/es/init.go @@ -13,7 +13,7 @@ func InitElastic() { var err error ElasticClient, err = elasticsearch.NewClient(elasticsearch.Config{ // Addresses: []string{"http://113.44.68.213:9200"}, - Addresses: []string{"http://127.0.0.1:9200"}, + Addresses: []string{"http://localhost:9200"}, // TIP 用 localhost 可以,127.0.0.1 无效... // Username: "elastic", // Password: "U8n61yn*Sp4Kvbuqo_K8", })