package elastic

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
	"hongze/hongze_edb_lib/models"
	"hongze/hongze_edb_lib/utils"
)

func NewClient() (client *elastic.Client, err error) {
	client, err = elastic.NewClient(
		elastic.SetURL(ES_URL),
		elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
		elastic.SetSniff(false))
	return
}

// EsAddOrEditEdbInfoData 新增/修改es中的指标数据
func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
	defer func() {
		if err != nil {
			fmt.Println("EsAddOrEditData Err:", err.Error())
		}
	}()
	client := utils.EsClient
	if err != nil {
		return
	}

	resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
	if err != nil {
		fmt.Println("新增失败:", err.Error())
		return err
	}
	fmt.Println(resp)
	if resp.Status == 0 {
		fmt.Println("新增成功", resp.Result)
		err = nil
	} else {
		fmt.Println("AddData", resp.Status, resp.Result)
	}
	return
}
