您当前的位置: 首页 >  ar

少林码僧

暂无认证

  • 3浏览

    0关注

    317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

golang操作elasticsearch详解

少林码僧 发布时间:2021-10-27 19:34:36 ,浏览量:3

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 {
关注
打赏
1661398670
查看更多评论
立即登录/注册

微信扫码登录

0.0377s