golang *** 作elasticsearch详解

golang *** 作elasticsearch详解,第1张

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/995055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存