大家好,又见面了,我是你们的朋友全栈君。
golang操作elasticsearch详解
直接上代码
package main
import (
"bytes"
"context"
"fmt"
"github.com/olivere/elastic/v7"
"log"
)
const IndexName = "test_index"
func main() {
IsDocExists("xxx", IndexName)
}
//获取Es客户端
func GetEsClient() *elastic.Client {
var buf bytes.Buffer
client, err := elastic.NewClient(
elastic.SetURL("http://127.0.0.1:9200/"),
//docker
elastic.SetSniff(false),
elastic.SetInfoLog(log.New(&buf, "ES-INFO: ", 0)),
elastic.SetTraceLog(log.New(&buf, "ES-TRACE: ", 0)),
elastic.SetErrorLog(log.New(&buf, "ES-ERROR: ", 0)),
)
if err != nil {
return nil
}
return client
}
//查看某文档是否存在,给定文档ID查询
func IsDocExists(id, index string) bool {
client := GetEsClient()
defer client.Stop()
exist, _ := client.Exists().Index(index).Id(id).Do(context.Background())
if !exist {
log
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。