elastic.go 965 B

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