1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| type User struct { Name string `json:"name"` Age int8 `json:"age"` Sex int8 `json:"sex"` }
func main() { const urls = "http://127.0.0.1:9200" client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(urls)) if err != nil { panic(err) } fmt.Println("连接ES成功") user := User{ Name: "测试1", Age: 20, Sex: 1, } do, err := client.Index(). Index("user"). BodyJson(user). Do(context.Background()) if err != nil { panic(err) } fmt.Printf("插入数据成功 id %s,索引 %s,类型 %s", do.Id, do.Index, do.Type) }
|