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 !e
本文详细介绍了如何使用Golang与Elasticsearch进行交互,涵盖了连接设置、索引管理、文档增删改查等核心操作,助你深入理解Go在搜索引擎开发中的应用。
订阅专栏 解锁全文
2286

被折叠的 条评论
为什么被折叠?



