分享
Golang操作elasticsearch(一)
洛杉矶银河 · · 7786 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
使用第三方包:olivere github。总结一下olivere操作ES的常用功能
说明:以下例子用到的 es address: "http://127.0.0.1:9200"
-
获取Es客户端
func GetEsClient() *elastic.Client { file := "./eslog.log" logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) // 应该判断error,此处简略 client,err:=elastic.NewClient( elastic.SetURL("http://127.0.0.1:9200/"), //docker elastic.SetSniff(false), elastic.SetInfoLog(log.New(logFile, "ES-INFO: ", 0)), elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)), elastic.SetErrorLog(log.New(logFile, "ES-ERROR: ", 0)), ) if err!=nil{ return nil } return client } -
查看某文档是否存在,给定文档ID查询
func IsDocExists(id int,index string) bool { client := GetEsClient() defer client.Stop() exist,_ := client.Exists().Index(index).Id(strconv.Itoa(id)).Do(context.Background()) if !exist{ log.Println("ID may be incorrect! ",id) return false } return true } -
获取文档
func GetDoc(id int,index string)( *elastic.GetResult ,error){ client := GetEsClient() defer client.Stop() if !IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } esResponse,err := client.Get().Index(index).Id( strconv.Itoa(id) ).Do(context.Background()) if err != nil { return nil,err } return esResponse,nil } -
添加文档
func AddDoc(id int,doc string,index string)( *elastic.IndexResponse,error){ client := GetEsClient() defer client.Stop() if IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } rsp ,err:=client.Index().Index(index).Id( strconv.Itoa(id) ).BodyJson(doc).Do(context.Background()) if err!=nil{ return nil,err } return rsp,nil } -
更新文档
func UpdateDoc(updateField *map[string]interface{},id int,index string)(*elastic.UpdateResponse,error){ client := GetEsClient() defer client.Stop() if !IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } rsp,err:=client.Update().Index(index).Id(strconv.Itoa(id)).Doc(updateField).Do(context.Background()) if err != nil{ fmt.Println(err) return nil,err } return rsp,nil } -
删除文档
func DeleteDoc(id int,index string)(*elastic.DeleteResponse, error){ client := GetEsClient() defer client.Stop() rsp,err:=client.Delete().Index(index).Id(strconv.Itoa(id)).Do(context.Background()) if err != nil{ return nil,err } return rsp,nil }
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信7786 次点击
被以下专栏收入,发现更多相似内容
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
使用第三方包:olivere github。总结一下olivere操作ES的常用功能
说明:以下例子用到的 es address: "http://127.0.0.1:9200"
-
获取Es客户端
func GetEsClient() *elastic.Client { file := "./eslog.log" logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) // 应该判断error,此处简略 client,err:=elastic.NewClient( elastic.SetURL("http://127.0.0.1:9200/"), //docker elastic.SetSniff(false), elastic.SetInfoLog(log.New(logFile, "ES-INFO: ", 0)), elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)), elastic.SetErrorLog(log.New(logFile, "ES-ERROR: ", 0)), ) if err!=nil{ return nil } return client } -
查看某文档是否存在,给定文档ID查询
func IsDocExists(id int,index string) bool { client := GetEsClient() defer client.Stop() exist,_ := client.Exists().Index(index).Id(strconv.Itoa(id)).Do(context.Background()) if !exist{ log.Println("ID may be incorrect! ",id) return false } return true } -
获取文档
func GetDoc(id int,index string)( *elastic.GetResult ,error){ client := GetEsClient() defer client.Stop() if !IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } esResponse,err := client.Get().Index(index).Id( strconv.Itoa(id) ).Do(context.Background()) if err != nil { return nil,err } return esResponse,nil } -
添加文档
func AddDoc(id int,doc string,index string)( *elastic.IndexResponse,error){ client := GetEsClient() defer client.Stop() if IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } rsp ,err:=client.Index().Index(index).Id( strconv.Itoa(id) ).BodyJson(doc).Do(context.Background()) if err!=nil{ return nil,err } return rsp,nil } -
更新文档
func UpdateDoc(updateField *map[string]interface{},id int,index string)(*elastic.UpdateResponse,error){ client := GetEsClient() defer client.Stop() if !IsDocExists(id,index){ return nil,fmt.Errorf("id不存在") } rsp,err:=client.Update().Index(index).Id(strconv.Itoa(id)).Doc(updateField).Do(context.Background()) if err != nil{ fmt.Println(err) return nil,err } return rsp,nil } -
删除文档
func DeleteDoc(id int,index string)(*elastic.DeleteResponse, error){ client := GetEsClient() defer client.Stop() rsp,err:=client.Delete().Index(index).Id(strconv.Itoa(id)).Do(context.Background()) if err != nil{ return nil,err } return rsp,nil }