elastic.go 821 B

1234567891011121314151617181920212223242526272829303132
  1. package elastic
  2. import (
  3. "context"
  4. dataSourceModel "eta/eta_task/models/data_source"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. )
  8. // EsAddOrEditDataSourceIndex 新增/修改es中的数据源指标
  9. func EsAddOrEditDataSourceIndex(indexName, docId string, item *dataSourceModel.SearchDataSource) (err error) {
  10. defer func() {
  11. if err != nil {
  12. tips := fmt.Sprintf("EsAddOrEditDataSourceIndex err: %v", err)
  13. utils.FileLog.Info(tips)
  14. }
  15. }()
  16. client := utils.EsClient
  17. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  18. if e != nil {
  19. err = fmt.Errorf("resp err, %v", e)
  20. return
  21. }
  22. if resp.Status != 0 {
  23. err = fmt.Errorf("result err, status: %d, result: %s", resp.Status, resp.Result)
  24. return
  25. }
  26. err = nil
  27. fmt.Println("data source write to es success", resp.Result)
  28. return
  29. }