From 429573c8379f6b699386c4bb04a3ce1968651d6a Mon Sep 17 00:00:00 2001 From: huanggze Date: Wed, 21 Aug 2019 15:57:52 +0800 Subject: [PATCH] comment-1 Signed-off-by: huanggze --- pkg/simple/client/elasticsearch/esclient.go | 2 +- pkg/simple/client/elasticsearch/interface.go | 11 ++++++----- .../versions/v5/elasticsearch.go | 17 +++++++++-------- .../versions/v6/elasticsearch.go | 9 +++++---- .../versions/v7/elasticsearch.go | 19 ++++++++++--------- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/pkg/simple/client/elasticsearch/esclient.go b/pkg/simple/client/elasticsearch/esclient.go index 5eef2bf10..4568048b0 100644 --- a/pkg/simple/client/elasticsearch/esclient.go +++ b/pkg/simple/client/elasticsearch/esclient.go @@ -561,7 +561,7 @@ func Query(param QueryParameters) *QueryResult { return queryResult } - body, err := client.Search(query, config.Index) + body, err := client.Search(query) if err != nil { glog.Errorln(err) queryResult = new(QueryResult) diff --git a/pkg/simple/client/elasticsearch/interface.go b/pkg/simple/client/elasticsearch/interface.go index d441fa322..7f7c93ca9 100644 --- a/pkg/simple/client/elasticsearch/interface.go +++ b/pkg/simple/client/elasticsearch/interface.go @@ -18,19 +18,20 @@ const ( type Client interface { // Perform Search API - Search(body []byte, indexPrefix string) ([]byte, error) + Search(body []byte) ([]byte, error) GetTotalHitCount(v interface{}) int64 } func NewForConfig(cfg *Config) Client { address := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port) + index := cfg.Index switch cfg.VersionMajor { case ElasticV5: - return v5.New(address) + return v5.New(address, index) case ElasticV6: - return v6.New(address) + return v6.New(address, index) case ElasticV7: - return v7.New(address) + return v7.New(address, index) default: return nil } @@ -40,7 +41,7 @@ func detectVersionMajor(cfg *Config) error { // Info APIs are backward compatible with versions of v5.x, v6.x and v7.x address := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port) - es := v6.New(address) + es := v6.New(address, "") res, err := es.Client.Info( es.Client.Info.WithContext(context.Background()), ) diff --git a/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go index 218e096b4..9dc395cb2 100644 --- a/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v5/elasticsearch.go @@ -10,24 +10,25 @@ import ( ) type Elastic struct { - Client *elasticsearch.Client + client *elasticsearch.Client + index string } -func New(address string) Elastic { +func New(address string, index string) Elastic { client, _ := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) - return Elastic{Client: client} + return Elastic{client: client, index: index} } -func (e Elastic) Search(body []byte, indexPrefix string) ([]byte, error) { +func (e Elastic) Search(body []byte) ([]byte, error) { - response, err := e.Client.Search( - e.Client.Search.WithContext(context.Background()), - e.Client.Search.WithIndex(fmt.Sprintf("%s*", indexPrefix)), - e.Client.Search.WithBody(bytes.NewBuffer(body)), + response, err := e.client.Search( + e.client.Search.WithContext(context.Background()), + e.client.Search.WithIndex(fmt.Sprintf("%s*", e.index)), + e.client.Search.WithBody(bytes.NewBuffer(body)), ) if err != nil { return nil, err diff --git a/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go index 423a2a4e7..2919f3788 100644 --- a/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v6/elasticsearch.go @@ -11,22 +11,23 @@ import ( type Elastic struct { Client *elasticsearch.Client + index string } -func New(address string) Elastic { +func New(address string, index string) Elastic { client, _ := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) - return Elastic{Client: client} + return Elastic{Client: client, index: index} } -func (e Elastic) Search(body []byte, indexPrefix string) ([]byte, error) { +func (e Elastic) Search(body []byte) ([]byte, error) { response, err := e.Client.Search( e.Client.Search.WithContext(context.Background()), - e.Client.Search.WithIndex(fmt.Sprintf("%s*", indexPrefix)), + e.Client.Search.WithIndex(fmt.Sprintf("%s*", e.index)), e.Client.Search.WithBody(bytes.NewBuffer(body)), ) if err != nil { diff --git a/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go b/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go index c83f71fec..f91a2094c 100644 --- a/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go +++ b/pkg/simple/client/elasticsearch/versions/v7/elasticsearch.go @@ -10,25 +10,26 @@ import ( ) type Elastic struct { - Client *elasticsearch.Client + client *elasticsearch.Client + index string } -func New(address string) Elastic { +func New(address string, index string) Elastic { client, _ := elasticsearch.NewClient(elasticsearch.Config{ Addresses: []string{address}, }) - return Elastic{Client: client} + return Elastic{client: client, index: index} } -func (e Elastic) Search(body []byte, indexPrefix string) ([]byte, error) { +func (e Elastic) Search(body []byte) ([]byte, error) { - response, err := e.Client.Search( - e.Client.Search.WithContext(context.Background()), - e.Client.Search.WithIndex(fmt.Sprintf("%s*", indexPrefix)), - e.Client.Search.WithTrackTotalHits(true), - e.Client.Search.WithBody(bytes.NewBuffer(body)), + response, err := e.client.Search( + e.client.Search.WithContext(context.Background()), + e.client.Search.WithIndex(fmt.Sprintf("%s*", e.index)), + e.client.Search.WithTrackTotalHits(true), + e.client.Search.WithBody(bytes.NewBuffer(body)), ) if err != nil { return nil, err