From ce21b97dba5d000a2e1fa3a49b1eca9f9f0d9874 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Mon, 8 Feb 2021 10:16:24 +0800 Subject: [PATCH] =?UTF-8?q?es=E9=85=8D=E7=BD=AE=E6=B7=BB=E5=8A=A0xoath=20b?= =?UTF-8?q?asic=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 ++ db/elasticEngine.go | 32 +++++++++++++++++++++++--------- db/sqlManager.go | 5 +++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index a628535..0aa882b 100644 --- a/config/config.go +++ b/config/config.go @@ -29,6 +29,8 @@ type ConfAPI struct { } type ElasticSearch struct{ Address string `yaml:"address"` + User string `yaml:"user"` + Password string `yaml:"password"` } type EntityRedis struct { diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 9a02cd8..1fd4481 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -61,16 +61,16 @@ func (p *ElkEngine) Delete(query elastic.Query, index string) error { } func (p *ElkEngine) Query(index string, query elastic.Query, v interface{}, - limit int, offset int) ([]string, error) { + limit int, offset int) ( []string, error) { if reflect.ValueOf(v).Kind() != reflect.Ptr { return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Ptr") } if reflect.ValueOf(v).Elem().Kind() != reflect.Slice { - return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Slice") + return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Slice") } if reflect.ValueOf(v).Elem().Type().Elem().Kind() != reflect.Struct { - return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Struct") + return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Struct") } eletype := reflect.ValueOf(v).Elem().Type().Elem() obj := reflect.ValueOf(v).Elem() @@ -81,19 +81,34 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{}, res, err := p.cli.Search(index).Query(query).Do(context.Background()) if err != nil { print(err) - return nil, err + return nil, err } id := []string{} for _, vs := range res.Hits.Hits { id = append(id, vs.Id) + data, e := vs.Source.MarshalJSON() + if nil != e { + log.Print(e.Error()) + } + mapobj := map[string]interface{}{} + e = json.Unmarshal(data, &mapobj) + if nil != e { + log.Print(e.Error()) + } + obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj) + log.Print(obj) + if nil != e { + log.Print(e.Error()) + } + objAdd = append(objAdd, reflect.ValueOf(obj)) } - return id, nil + return id, nil } else { res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background()) if err != nil { print(err) - return nil, err + return nil, err } id := []string{} for _, vs := range res.Hits.Hits { @@ -113,14 +128,13 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{}, log.Print(e.Error()) } objAdd = append(objAdd, reflect.ValueOf(obj)) - //v = append(v, obj) } addOp := reflect.Append(obj, objAdd...) obj.Set(addOp) - return id, nil + return id, nil } } else { - return nil, errors.New(ERROR_PTR) + return nil, errors.New(ERROR_PTR) } } diff --git a/db/sqlManager.go b/db/sqlManager.go index e95a63b..660cfd0 100644 --- a/db/sqlManager.go +++ b/db/sqlManager.go @@ -115,11 +115,12 @@ func Init() { func InitELK() { var e error elkconf := config.GetElkConfig() - log.Print(elkconf) gElkEngine.cli, e = elastic.NewClient( elastic.SetURL(elkconf.Address), // Must turn off sniff in docker - elastic.SetSniff(false)) + elastic.SetSniff(false), + elastic.SetBasicAuth(elkconf.User, elkconf.Password), + ) if nil != e { logs.Error(e.Error()) gElkEngine.cli = nil