680 Star 7.8K Fork 3.4K

陌溪/LearningNotes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
陌溪 提交于 2020-09-17 18:38 +08:00 . docs:增加Gin框架学习笔记和代码
package main
import (
"context"
"fmt"
"github.com/olivere/elastic/v7"
)
// 构造一个student结构体
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
Married bool `json:"married"`
}
func (s *Student)run() *Student {
fmt.Printf("%s在跑...", s.Name)
return s
}
func (s *Student)play()*Student {
fmt.Printf("%s在玩...", s.Name)
return s
}
func main() {
moxi := Student{
Name: "陌溪",
Age: 9000,
Married: true,
}
moxi.run()
moxi.run()
moxi.play().run()
// 使用 elastic库中的NewClient方法
client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"))
if err != nil {
// 抛出异常
panic(err)
}
fmt.Println("connect to es success")
// 构造一条数据
p1 := Student{Name: "rion", Age: 22, Married: false}
put1, err := client.Index().
Index("user"). // 拿到索引库
BodyJson(p1). // 将对象转换成json
Do(context.Background()) // 插入,同时可以设置context的超时
if err != nil {
// Handle error
panic(err)
}
fmt.Printf("Indexed student %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/moxi159753/LearningNotes.git
git@gitee.com:moxi159753/LearningNotes.git
moxi159753
LearningNotes
LearningNotes
master

搜索帮助