esWriter.go 388 B

12345678910111213141516171819202122232425
  1. package es
  2. import (
  3. "context"
  4. "time"
  5. )
  6. var EWriter = &esWriter{}
  7. type esWriter struct {
  8. }
  9. func (e *esWriter) Write(p []byte) (int, error) {
  10. str := string(p)
  11. Client.Index().
  12. Index(getIndexName()).
  13. Type("_doc").
  14. BodyJson(str).Do(context.Background())
  15. return len(p), nil
  16. }
  17. // 当天日期作为索引名
  18. func getIndexName() string {
  19. return time.Now().Format("2006-01-02")
  20. }