1. 操作ElasticSearch

1.1.1. elastic client

我们使用第三方库https://github.com/olivere/elastic 来连接ES并进行操作。

注意下载与你的ES相同版本的client,例如我们这里使用的ES是7.2.1的版本,那么我们下载的client也要与之对应为github.com/olivere/elastic/v7。

使用go.mod来管理依赖:

  1. require (
  2. github.com/olivere/elastic/v7 v7.0.4
  3. )

简单示例:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/olivere/elastic/v7"
  6. )
  7. // Elasticsearch demo
  8. type Person struct {
  9. Name string `json:"name"`
  10. Age int `json:"age"`
  11. Married bool `json:"married"`
  12. }
  13. func main() {
  14. client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"))
  15. if err != nil {
  16. // Handle error
  17. panic(err)
  18. }
  19. fmt.Println("connect to es success")
  20. p1 := Person{Name: "lmh", Age: 18, Married: false}
  21. put1, err := client.Index().
  22. Index("user").
  23. BodyJson(p1).
  24. Do(context.Background())
  25. if err != nil {
  26. // Handle error
  27. panic(err)
  28. }
  29. fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
  30. }

示例2:

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "reflect"
  8. "gopkg.in/olivere/elastic.v7" //这里使用的是版本5,最新的是6,有改动
  9. )
  10. var client *elastic.Client
  11. var host = "http://127.0.0.1:9200/"
  12. type Employee struct {
  13. FirstName string `json:"first_name"`
  14. LastName string `json:"last_name"`
  15. Age int `json:"age"`
  16. About string `json:"about"`
  17. Interests []string `json:"interests"`
  18. }
  19. //初始化
  20. func init() {
  21. errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
  22. var err error
  23. client, err = elastic.NewClient(elastic.SetErrorLog(errorlog), elastic.SetURL(host))
  24. if err != nil {
  25. panic(err)
  26. }
  27. info, code, err := client.Ping(host).Do(context.Background())
  28. if err != nil {
  29. panic(err)
  30. }
  31. fmt.Printf("Elasticsearch returned with code %d and version %s\n", code, info.Version.Number)
  32. esversion, err := client.ElasticsearchVersion(host)
  33. if err != nil {
  34. panic(err)
  35. }
  36. fmt.Printf("Elasticsearch version %s\n", esversion)
  37. }
  38. /*下面是简单的CURD*/
  39. //创建
  40. func create() {
  41. //使用结构体
  42. e1 := Employee{"Jane", "Smith", 32, "I like to collect rock albums", []string{"music"}}
  43. put1, err := client.Index().
  44. Index("megacorp").
  45. Type("employee").
  46. Id("1").
  47. BodyJson(e1).
  48. Do(context.Background())
  49. if err != nil {
  50. panic(err)
  51. }
  52. fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type)
  53. //使用字符串
  54. e2 := `{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests":["sports","music"]}`
  55. put2, err := client.Index().
  56. Index("megacorp").
  57. Type("employee").
  58. Id("2").
  59. BodyJson(e2).
  60. Do(context.Background())
  61. if err != nil {
  62. panic(err)
  63. }
  64. fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put2.Id, put2.Index, put2.Type)
  65. e3 := `{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests":["forestry"]}`
  66. put3, err := client.Index().
  67. Index("megacorp").
  68. Type("employee").
  69. Id("3").
  70. BodyJson(e3).
  71. Do(context.Background())
  72. if err != nil {
  73. panic(err)
  74. }
  75. fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put3.Id, put3.Index, put3.Type)
  76. }
  77. //删除
  78. func delete() {
  79. res, err := client.Delete().Index("megacorp").
  80. Type("employee").
  81. Id("1").
  82. Do(context.Background())
  83. if err != nil {
  84. println(err.Error())
  85. return
  86. }
  87. fmt.Printf("delete result %s\n", res.Result)
  88. }
  89. //修改
  90. func update() {
  91. res, err := client.Update().
  92. Index("megacorp").
  93. Type("employee").
  94. Id("2").
  95. Doc(map[string]interface{}{"age": 88}).
  96. Do(context.Background())
  97. if err != nil {
  98. println(err.Error())
  99. }
  100. fmt.Printf("update age %s\n", res.Result)
  101. }
  102. //查找
  103. func gets() {
  104. //通过id查找
  105. get1, err := client.Get().Index("megacorp").Type("employee").Id("2").Do(context.Background())
  106. if err != nil {
  107. panic(err)
  108. }
  109. if get1.Found {
  110. fmt.Printf("Got document %s in version %d from index %s, type %s\n", get1.Id, get1.Version, get1.Index, get1.Type)
  111. }
  112. }
  113. //搜索
  114. func query() {
  115. var res *elastic.SearchResult
  116. var err error
  117. //取所有
  118. res, err = client.Search("megacorp").Type("employee").Do(context.Background())
  119. printEmployee(res, err)
  120. //字段相等
  121. q := elastic.NewQueryStringQuery("last_name:Smith")
  122. res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background())
  123. if err != nil {
  124. println(err.Error())
  125. }
  126. printEmployee(res, err)
  127. //条件查询
  128. //年龄大于30岁的
  129. boolQ := elastic.NewBoolQuery()
  130. boolQ.Must(elastic.NewMatchQuery("last_name", "smith"))
  131. boolQ.Filter(elastic.NewRangeQuery("age").Gt(30))
  132. res, err = client.Search("megacorp").Type("employee").Query(q).Do(context.Background())
  133. printEmployee(res, err)
  134. //短语搜索 搜索about字段中有 rock climbing
  135. matchPhraseQuery := elastic.NewMatchPhraseQuery("about", "rock climbing")
  136. res, err = client.Search("megacorp").Type("employee").Query(matchPhraseQuery).Do(context.Background())
  137. printEmployee(res, err)
  138. //分析 interests
  139. aggs := elastic.NewTermsAggregation().Field("interests")
  140. res, err = client.Search("megacorp").Type("employee").Aggregation("all_interests", aggs).Do(context.Background())
  141. printEmployee(res, err)
  142. }
  143. //简单分页
  144. func list(size, page int) {
  145. if size < 0 || page < 1 {
  146. fmt.Printf("param error")
  147. return
  148. }
  149. res, err := client.Search("megacorp").
  150. Type("employee").
  151. Size(size).
  152. From((page - 1) * size).
  153. Do(context.Background())
  154. printEmployee(res, err)
  155. }
  156. //打印查询到的Employee
  157. func printEmployee(res *elastic.SearchResult, err error) {
  158. if err != nil {
  159. print(err.Error())
  160. return
  161. }
  162. var typ Employee
  163. for _, item := range res.Each(reflect.TypeOf(typ)) { //从搜索结果中取数据的方法
  164. t := item.(Employee)
  165. fmt.Printf("%#v\n", t)
  166. }
  167. }
  168. func main() {
  169. create()
  170. delete()
  171. update()
  172. gets()
  173. query()
  174. list(1, 3)
  175. }

更多使用详见文档:https://godoc.org/github.com/olivere/elastic