pre_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/olivere/elastic/v7"
  5. "strconv"
  6. "testing"
  7. "time"
  8. )
  9. func TestMonth(T *testing.T) {
  10. // 创建2023年12月31日的时间对象
  11. date := time.Date(2023, time.August, 30, 0, 0, 0, 0, time.UTC)
  12. next := date.AddDate(0, -1, -1)
  13. month := int(next.Month())
  14. monthStr := strconv.Itoa(month)
  15. year := next.Year()
  16. yearStr := strconv.Itoa(year)
  17. //预处理索引名称
  18. PreBiddingIndex := "bidding_" + yearStr + monthStr
  19. fmt.Sprintf(PreBiddingIndex)
  20. }
  21. func TestDeleteIndex(t *testing.T) {
  22. data := map[string]string{
  23. "aa": "aaa",
  24. "bb": "bbb",
  25. "cc": "ccc",
  26. }
  27. for _, v := range data {
  28. if v == "aaa" {
  29. return
  30. }
  31. fmt.Println(v)
  32. }
  33. }
  34. //TestCreateIndex 创建索引
  35. func TestCreateIndex(t *testing.T) {
  36. var clients = make(map[string]*elastic.Client, 0)
  37. for k, v := range GF.ES {
  38. url := v.URL
  39. username := v.Username
  40. password := v.Password
  41. // 创建 Elasticsearch 客户端
  42. client, err := elastic.NewClient(
  43. elastic.SetURL(url),
  44. elastic.SetBasicAuth(username, password),
  45. elastic.SetSniff(false),
  46. )
  47. if err != nil {
  48. fmt.Println("创建 Elasticsearch 客户端失败", err)
  49. }
  50. clients[k] = client
  51. }
  52. err := CreateIndex(clients, "bidding_2023112214")
  53. fmt.Println(err)
  54. }