elastic_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package elastic
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. var esClinet Elastic
  8. //func init() {
  9. // esClinet = Elastic{
  10. // S_esurl: "127.0.0.1:19805",
  11. // I_size: 2,
  12. // Username: "es_all",
  13. // Password: "TopJkO2E_d1x",
  14. // }
  15. // fmt.Println(esClinet)
  16. //
  17. // esClinet.InitElasticSize()
  18. //
  19. //}
  20. func TestCount(t *testing.T) {
  21. esClinet = Elastic{
  22. S_esurl: "http://192.168.3.241:40801",
  23. I_size: 2,
  24. Username: "",
  25. Password: "",
  26. }
  27. esClinet.InitElasticSize()
  28. //rangeQuery := es.NewRangeQuery("id").Gte("6463a8800000000000000000").Lt("6464fa000000000000000000")
  29. //termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网")
  30. //boolQuery := es.NewBoolQuery().Must(rangeQuery, termsQuery)
  31. //
  32. //bytes, err := json.Marshal(boolQuery)
  33. //if err != nil {
  34. // fmt.Println("err", err.Error())
  35. //}
  36. //fmt.Println("qqq", string(bytes))
  37. //count1 := esClinet.Count("bidding", boolQuery)
  38. //fmt.Println(count1)
  39. fmt.Println(esClinet.Count("bidding_20231121", nil))
  40. }
  41. func TestBulkSave(t *testing.T) {
  42. esClinet = Elastic{
  43. S_esurl: "http://127.0.0.1:19805",
  44. I_size: 2,
  45. Username: "es_all",
  46. Password: "TopJkO2E_d1x",
  47. }
  48. esClinet.InitElasticSize()
  49. id := "6398a381063a7b816e072adf"
  50. esUpdate := map[string]interface{}{
  51. "area": "新疆",
  52. "city": "伊犁哈萨克自治州",
  53. "district": "奎屯市",
  54. }
  55. updateEs := []map[string]interface{}{
  56. {"_id": id},
  57. esUpdate,
  58. }
  59. arru := make([][]map[string]interface{}, 0)
  60. arru = append(arru, updateEs)
  61. esClinet.UpdateBulk("bidding", arru...)
  62. fmt.Println(arru)
  63. }
  64. func TestElastic_ExistsIndex(t *testing.T) {
  65. esClinet = Elastic{
  66. S_esurl: "http://127.0.0.1:19805",
  67. I_size: 2,
  68. Username: "es_all",
  69. Password: "TopJkO2E_d1x",
  70. }
  71. esClinet.InitElasticSize()
  72. res, err := esClinet.ExistsIndex("bidding")
  73. assert.Equal(t, nil, err)
  74. assert.Equal(t, true, res)
  75. }
  76. func TestElastic_SetAlias(t *testing.T) {
  77. //esClinet = Elastic{
  78. // S_esurl: "http://127.0.0.1:19805",
  79. // I_size: 2,
  80. // Username: "es_all",
  81. // Password: "TopJkO2E_d1x",
  82. //}
  83. esClinet = Elastic{
  84. S_esurl: "http://127.0.0.1:19905",
  85. I_size: 2,
  86. Username: "jybid",
  87. Password: "Top2023_JEB01i@31",
  88. }
  89. esClinet.InitElasticSize()
  90. err := esClinet.SetAlias("bidding_20231120", "bidding_pre2")
  91. assert.Equal(t, nil, err)
  92. }
  93. func TestElastic_RemoveAlias(t *testing.T) {
  94. esClinet = Elastic{
  95. S_esurl: "http://127.0.0.1:19805",
  96. I_size: 2,
  97. Username: "es_all",
  98. Password: "TopJkO2E_d1x",
  99. }
  100. esClinet.InitElasticSize()
  101. err := esClinet.RemoveAlias("bidding_20231120", "bidding_pre")
  102. assert.Equal(t, nil, err)
  103. }
  104. func TestCleanIndex(t *testing.T) {
  105. esClinet = Elastic{
  106. S_esurl: "http://127.0.0.1:19805",
  107. I_size: 2,
  108. Username: "es_all",
  109. Password: "TopJkO2E_d1x",
  110. }
  111. esClinet.InitElasticSize()
  112. err := esClinet.RemoveAlias("bidding_20231118", "bidding_pre")
  113. assert.Equal(t, nil, err)
  114. }
  115. func TestElastic_DeleteByID(t *testing.T) {
  116. esClinet = Elastic{
  117. S_esurl: "http://127.0.0.1:19805",
  118. I_size: 2,
  119. Username: "es_all",
  120. Password: "TopJkO2E_d1x",
  121. }
  122. esClinet.InitElasticSize()
  123. err := esClinet.DeleteByID("bidding_2023112216", "655c99c60687916fae58eec1")
  124. assert.Equal(t, nil, err)
  125. }