elastic.go 994 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package elastic
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/olivere/elastic/v7"
  6. "hongze/hongze_edb_lib/models"
  7. "hongze/hongze_edb_lib/utils"
  8. )
  9. func NewClient() (client *elastic.Client, err error) {
  10. client, err = elastic.NewClient(
  11. elastic.SetURL(ES_URL),
  12. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  13. elastic.SetSniff(false))
  14. return
  15. }
  16. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  17. func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) {
  18. defer func() {
  19. if err != nil {
  20. fmt.Println("EsAddOrEditData Err:", err.Error())
  21. }
  22. }()
  23. client := utils.EsClient
  24. if err != nil {
  25. return
  26. }
  27. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  28. if err != nil {
  29. fmt.Println("新增失败:", err.Error())
  30. return err
  31. }
  32. fmt.Println(resp)
  33. if resp.Status == 0 {
  34. fmt.Println("新增成功", resp.Result)
  35. err = nil
  36. } else {
  37. fmt.Println("AddData", resp.Status, resp.Result)
  38. }
  39. return
  40. }