github.com/jtzjtz/kit@v1.0.2/conn/es_pool/es_pool.go (about)

     1  package es_pool
     2  
     3  import (
     4  	"github.com/olivere/elastic"
     5  	"github.com/olivere/elastic/config"
     6  )
     7  
     8  type clientPool struct {
     9  	client *elastic.Client
    10  	err    error
    11  }
    12  
    13  func NewPool(options *Options) *clientPool {
    14  	pool := new(clientPool)
    15  	conf := &config.Config{
    16  		URL:         options.URL,
    17  		Index:       options.Index,
    18  		Username:    options.Username,
    19  		Password:    options.Password,
    20  		Shards:      options.Shards,
    21  		Replicas:    options.Replicas,
    22  		Sniff:       options.Sniff,
    23  		Healthcheck: options.Healthcheck,
    24  		Infolog:     options.Infolog,
    25  		Errorlog:    options.Errorlog,
    26  		Tracelog:    options.Tracelog,
    27  	}
    28  	client, err := elastic.NewClientFromConfig(conf)
    29  	pool.client = client
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	return pool
    34  }
    35  func (cl *clientPool) GetClient() *elastic.Client {
    36  	return cl.client
    37  }
    38  func (cl *clientPool) Close() {
    39  	cl.client.Stop()
    40  }