elastic_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package elastic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. es "github.com/olivere/elastic/v7"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. )
  9. var esClinet Elastic
  10. //func init() {
  11. // esClinet = Elastic{
  12. // S_esurl: "127.0.0.1:19805",
  13. // I_size: 2,
  14. // Username: "es_all",
  15. // Password: "TopJkO2E_d1x",
  16. // }
  17. // fmt.Println(esClinet)
  18. //
  19. // esClinet.InitElasticSize()
  20. //
  21. //}
  22. func TestCount(t *testing.T) {
  23. esClinet = Elastic{
  24. S_esurl: "http://127.0.0.1:19805",
  25. I_size: 2,
  26. Username: "es_all",
  27. Password: "TopJkO2E_d1x",
  28. }
  29. esClinet.InitElasticSize()
  30. rangeQuery := es.NewRangeQuery("id").Gte("6463a8800000000000000000").Lt("6464fa000000000000000000")
  31. termsQuery := es.NewTermsQuery("site", "元博网(采购与招标网)", "中国招标与采购网")
  32. boolQuery := es.NewBoolQuery().Must(rangeQuery, termsQuery)
  33. bytes, err := json.Marshal(boolQuery)
  34. if err != nil {
  35. fmt.Println("err", err.Error())
  36. }
  37. fmt.Println("qqq", string(bytes))
  38. count1 := esClinet.Count("bidding", boolQuery)
  39. fmt.Println(count1)
  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_20231118")
  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.InitElasticSize()
  84. err := esClinet.SetAlias("bidding_20231118", "bidding_pre")
  85. assert.Equal(t, nil, err)
  86. }
  87. func TestElastic_RemoveAlias(t *testing.T) {
  88. esClinet = Elastic{
  89. S_esurl: "http://127.0.0.1:19805",
  90. I_size: 2,
  91. Username: "es_all",
  92. Password: "TopJkO2E_d1x",
  93. }
  94. esClinet.InitElasticSize()
  95. err := esClinet.RemoveAlias("bidding_20231118", "bidding_pre")
  96. assert.Equal(t, nil, err)
  97. }