package init_serve

import (
	"github.com/olivere/elastic/v7"
	"hongze/hongze_yb/global"
	"log"
	"os"
)

func NewEsClient()  {
	esConfig := global.CONFIG.EsClient
	client, err := elastic.NewClient(
		elastic.SetURL(esConfig.Endpoints),                         // 支持多个地址，用逗号分隔即可
		elastic.SetBasicAuth(esConfig.Username, esConfig.Password), // 基于http base auth 验证机制的账号密码
		elastic.SetSniff(false),                                    // 是否应该定期检查集群
		elastic.SetHealthcheck(false),                              // 是否开启健康检查
	//	elastic.SetInfoLog(newEsLog(esConfig.Log+"/es_info.log", "Info")),
	//	elastic.SetErrorLog(newEsLog(esConfig.Log+"/es_error.log", "Error")),
	//	elastic.SetTraceLog(newEsLog(esConfig.Log+"/es_trace.log", "trace")),
	)
	if err != nil {
		global.LOG.Error("newEsClient init error,err=", err)
		panic("newEsClient 初始化失败:" + err.Error())
	}

	//全局赋值redis链接
	global.EsClient = client
}

func newEsLog(file string, level string) *log.Logger {
	logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	return log.New(logFile, "ES-"+level+": ", 0)
}