package main

import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
)

type stu struct {
Name string `json:"name"`
Age int `json:"age"`
Married bool `json:"married"`
}

func main(){
clinet,err :=elastic.NewClient(elastic.SetURL("http://192.168.199.76:9200"))
if err !=nil{
fmt.Printf("Connect es failed, error:%#v\n",err)
return
}
fmt.Println("Connect es successfully!!")
s1 := &stu{
Name:"xiaohua",
Age:16,
Married:false,
}
//链式操作
put1, err := clinet.Index().Index("per").BodyJson(s1).Do(context.Background())
if err !=nil {
fmt.Printf("put data failed!!,error:%#v\n",err)
return
}
fmt.Printf("Indexed user %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}

golang操作Es_golang