comment-1

Signed-off-by: huanggze <loganhuang@yunify.com>
This commit is contained in:
huanggze
2019-08-21 15:57:52 +08:00
parent bfed3a6baa
commit 429573c837
5 changed files with 31 additions and 27 deletions

View File

@@ -561,7 +561,7 @@ func Query(param QueryParameters) *QueryResult {
return queryResult return queryResult
} }
body, err := client.Search(query, config.Index) body, err := client.Search(query)
if err != nil { if err != nil {
glog.Errorln(err) glog.Errorln(err)
queryResult = new(QueryResult) queryResult = new(QueryResult)

View File

@@ -18,19 +18,20 @@ const (
type Client interface { type Client interface {
// Perform Search API // Perform Search API
Search(body []byte, indexPrefix string) ([]byte, error) Search(body []byte) ([]byte, error)
GetTotalHitCount(v interface{}) int64 GetTotalHitCount(v interface{}) int64
} }
func NewForConfig(cfg *Config) Client { func NewForConfig(cfg *Config) Client {
address := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port) address := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port)
index := cfg.Index
switch cfg.VersionMajor { switch cfg.VersionMajor {
case ElasticV5: case ElasticV5:
return v5.New(address) return v5.New(address, index)
case ElasticV6: case ElasticV6:
return v6.New(address) return v6.New(address, index)
case ElasticV7: case ElasticV7:
return v7.New(address) return v7.New(address, index)
default: default:
return nil 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 // 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) address := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port)
es := v6.New(address) es := v6.New(address, "")
res, err := es.Client.Info( res, err := es.Client.Info(
es.Client.Info.WithContext(context.Background()), es.Client.Info.WithContext(context.Background()),
) )

View File

@@ -10,24 +10,25 @@ import (
) )
type Elastic struct { 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{ client, _ := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, 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( response, err := e.client.Search(
e.Client.Search.WithContext(context.Background()), 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)), e.client.Search.WithBody(bytes.NewBuffer(body)),
) )
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -11,22 +11,23 @@ import (
type Elastic struct { 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{ client, _ := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, 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( response, err := e.Client.Search(
e.Client.Search.WithContext(context.Background()), 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)), e.Client.Search.WithBody(bytes.NewBuffer(body)),
) )
if err != nil { if err != nil {

View File

@@ -10,25 +10,26 @@ import (
) )
type Elastic struct { 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{ client, _ := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{address}, 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( response, err := e.client.Search(
e.Client.Search.WithContext(context.Background()), 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.WithTrackTotalHits(true), e.client.Search.WithTrackTotalHits(true),
e.Client.Search.WithBody(bytes.NewBuffer(body)), e.client.Search.WithBody(bytes.NewBuffer(body)),
) )
if err != nil { if err != nil {
return nil, err return nil, err