ES(Elasticsearch)–elastic包
Go语言操作ES,使用第三方库,不用官方库 [https://github.com/olivere/elastic]
github.com/olivere/elastic/v7: 版本必须与ES版本一致
package main
import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
)
// Elasticsearch demo
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Married bool `json:"married"`
}
func main() {
client, err := elastic.NewClient(elastic.SetURL("http://192.168.42.133:9200"))
if err != nil {
// Handle error
panic(err)
}
fmt.Println("connect to es success")
//创建一条数据
p1 := Person{Name: "rion", Age: 22, Married: false}
put1, err := client.Index().
Index("user"). //Index 数据库
BodyJson(p1).
Do(context.Background())
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}