1. 项目地址:
    https://github.com/elastic/go-elasticsearch/
  2. 使用:
    package main
    import (
     "bytes"
     "context"
     "encoding/json"
     "fmt"
     "github.com/elastic/go-elasticsearch/v8"
    )
    /**
    * class ES
    */
    type ES struct {
     EsObj *elasticsearch.Client
    }
    /**
    * 初始化方法:确定 elasticsearch 服务器地址、用户名、密码
    */
    func (this *ES) Init(hosts []string,username string,password string)  {
     config := elasticsearch.Config{
         Addresses: hosts,
         Username: username,
         Password: password,
     }
     es, err := elasticsearch.NewClient(config)
     if err != nil{
         fmt.Println(err)
     }
     this.EsObj = es
    }
    /**
    * 添加/更新 index
    * @param index index
    * @param id id
    * @param body 内容
    * @return [0,xx]失败 [1,xx]成功
    */
    func (this *ES) Index(index string,id string,body map[string]interface{}) []interface{} {
     ret := []interface{}{}
     var buf bytes.Buffer
     if err := json.NewEncoder(&buf).Encode(body); err != nil {
         ret = []interface{}{0,err}
     }
     res, err := this.EsObj.Index(index, &buf, this.EsObj.Index.WithDocumentID(id))
     if err != nil {
         ret = []interface{}{0,err}
     }
     defer res.Body.Close()
     r := map[string]interface{}{}
     json.NewDecoder(res.Body).Decode(&r)
     if res.StatusCode == 200 || res.StatusCode == 201{
         ret = []interface{}{1,r["result"]}
     }else{
         ret = []interface{}{0,r}
     }
     return ret
    }
    /**
    * 根据 id 获取内容
    * @param index index
    * @param id id
    * @return [0,xx]失败 [1,xx]成功
    */
    func (this *ES) Get(index string,id string) []interface{} {
     ret := []interface{}{}
     res, err := this.EsObj.Get(index, id)
     if err != nil {
         ret = []interface{}{0, err}
     }
     defer res.Body.Close()
     r := map[string]interface{}{}
     json.NewDecoder(res.Body).Decode(&r)
     if res.StatusCode == 200 || res.StatusCode == 201{
         ret = []interface{}{1,r["_source"]}
     }else{
         ret = []interface{}{0,r}
     }
     return ret
    }
    /**
    * 根据 关键词 在指定 key 中 搜索内容
    * @param index index
    * @param keywords 关键词
    * @param keys keys
    * @param page 从第几页开始获取
    * @param size 一次获取多少条数据
    * @return [0,xx]失败 [1,xx]成功
    */
    func (this *ES) Search(index string,keywords string,keys []string,page int,size int) []interface{} {
     from := (page - 1) * size
     ret := []interface{}{}
     res, err := this.EsObj.Info()
     if err != nil{
         ret = []interface{}{0,err}
     }
     var buf bytes.Buffer
     query := map[string]interface{}{
         "query": map[string]interface{}{
             "multi_match": map[string]interface{}{
                 "query": keywords,
                 "fields": keys,
             },
         },
     }
     if err := json.NewEncoder(&buf).Encode(query); err != nil {
         ret = []interface{}{0,err}
     }
     res, err = this.EsObj.Search(
         this.EsObj.Search.WithContext(context.Background()),
         this.EsObj.Search.WithIndex(index),
         this.EsObj.Search.WithBody(&buf),
         this.EsObj.Search.WithTrackTotalHits(true),
         this.EsObj.Search.WithPretty(),
         this.EsObj.Search.WithSize(size),
         this.EsObj.Search.WithFrom(from),
     )
     if err != nil {
         ret = []interface{}{0,err}
     }
     defer res.Body.Close()
     r := map[string]interface{}{}
     json.NewDecoder(res.Body).Decode(&r)
     if res.StatusCode == 200 || res.StatusCode == 201{
         ret = []interface{}{1,r["hits"].(map[string]interface{})["hits"]}
     }else{
         ret = []interface{}{0,r}
     }
     return ret
    }
    /**
    * 删除指定 ID
    * @param index index
    * @param id id
    * @return [0,xx]失败 [1,xx]成功
    */
    func (this *ES) DeleteId(index string,id string) []interface{} {
     ret := []interface{}{}
     res, err := this.EsObj.Delete(index, id)
     if err != nil {
         ret = []interface{}{0,err}
     }
     defer res.Body.Close()
     r := map[string]interface{}{}
     json.NewDecoder(res.Body).Decode(&r)
     if res.StatusCode == 200 || res.StatusCode == 201{
         ret = []interface{}{1,r}
     }else{
         ret = []interface{}{0,r}
     }
     return ret
    }
    /**
    * 辅助函数:返回实例化后的 ES 对象(自动执行 ES.Init())
    */
    func NewES(hosts []string,username string,password string) ES {
     es := ES{}
     es.Init(hosts,username,password)
     return es
    }
    func main() {
     hosts := []string{"http://192.168.0.11:9200","http://192.168.0.12:9200"}
     username := "elastic"
     password := "123456"
     es := NewES(hosts,username,password)
     // 添加/更新 index
     ret1 := es.Index("mydb","1", map[string]interface{}{"k1": "早起的鸟儿有虫吃!","k2": "太阳从东方升起!"})
     fmt.Println(ret1)
     // 根据 id 获取内容
     ret2 := es.Get("mydb","1")
     fmt.Println(ret2)
     // 根据 关键词 在指定 key 中 搜索内容
     ret3 := es.Search("mydb","鸟",[]string{"k1","k2"},1,5)
     fmt.Println(ret3)
     // 删除指定 ID
     ret4 := es.DeleteId("mydb","1")
     fmt.Println(ret4)
    }
    //// 输出
    //[1 updated]
    //[1 map[k1:早起的鸟儿有虫吃! k2:太阳从东方升起!]]
    //[1 [map[_id:1 _index:mydb _score:0.08701137 _source:map[k1:早起的鸟儿有虫吃! k2:太阳从东方升起!] _type:_doc]]]
    //[1 map[_id:1 _index:mydb _primary_term:1 _seq_no:9 _shards:map[failed:0 successful:2 total:2] _type:_doc _version:10 result:deleted]]
文档更新时间: 2021-05-31 08:56   作者:lee