未验证 提交 429573c8 编写于 作者: H huanggze

comment-1

Signed-off-by: Nhuanggze <loganhuang@yunify.com>
上级 bfed3a6b
......@@ -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)
......
......@@ -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()),
)
......
......@@ -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
......
......@@ -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 {
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册