github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/elastic/elastic.go (about)

     1  package elastic
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/olivere/elastic"
     7  )
     8  
     9  // Search es组件
    10  type Search struct {
    11  	Host  []string
    12  	Conn  *elastic.Client
    13  	Index string
    14  	Type  string
    15  }
    16  
    17  //ESConfigOption 配置文件
    18  type ESConfigOption struct {
    19  	Host  []string `json:"hosts"`
    20  	Index string
    21  	Type  string
    22  	Sniff bool
    23  	Log   elastic.Logger
    24  }
    25  
    26  // New 创建elastic 实例
    27  func New(conf ESConfigOption) (es *Search, err error) {
    28  	es = &Search{}
    29  	es.Host = conf.Host
    30  	es.Index = conf.Index
    31  	es.Type = conf.Type
    32  	es.Conn, err = elastic.NewClient(elastic.SetErrorLog(conf.Log), elastic.SetURL(es.Host...), elastic.SetSniff(conf.Sniff))
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	_, _, err = es.Conn.Ping(es.Host[0]).Do(context.Background())
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	return
    41  }