参考网址
blog.csdn.net/ydl1128/art…
demo
依赖的库包: github.com/olivere/elastic
demo
package main
import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
)
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.1.7:9200"))
if err != nil {
panic(err)
} else {
fmt.Println("connect to es success")
}
p1 := Person{Name: "rion", Age: 22, Married: false}
put1, err := client.Index().
Index("user").
BodyJson(p1).
Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Indexed user %s to index %s, type %s\n",
put1.Id, put1.Index, put1.Type)
}