直接上代码
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)