update_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package elastic
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func Test_Upsert(t *testing.T) {
  9. Convey("upsert", t, func() {
  10. e := NewElastic(nil)
  11. us := e.NewUpdate("articledata").Insert()
  12. data := map[string]int{"id": 552, "share": 321}
  13. us.AddData("articledata", data)
  14. data = map[string]int{"id": 558, "share": 1137}
  15. us.AddData("articledata", data)
  16. t.Logf("params(%s)", us.Params())
  17. err := us.Do(context.Background())
  18. So(err, ShouldBeNil)
  19. })
  20. }
  21. func Test_Upsert_ByMod(t *testing.T) {
  22. Convey("upsert", t, func() {
  23. data := make([]map[string]interface{}, 0)
  24. d := map[string]interface{}{"id": 22, "share": 22, "oid": int64(2266)}
  25. data = append(data, d)
  26. d = map[string]interface{}{"id": 33, "share": 33, "oid": int64(3388)}
  27. data = append(data, d)
  28. e := NewElastic(nil)
  29. us := e.NewUpdate("reply").Insert()
  30. for _, v := range data {
  31. oid, ok := v["oid"]
  32. if !ok {
  33. continue
  34. }
  35. oidReal, ok := oid.(int64) //must as same as interface type
  36. if !ok {
  37. continue
  38. }
  39. us.AddData(us.IndexByMod("reply_record", oidReal, 100), v)
  40. }
  41. t.Logf("params(%s)", us.Params())
  42. if us.HasData() {
  43. err := us.Do(context.Background())
  44. So(err, ShouldBeNil)
  45. } else {
  46. So(us.HasData(), ShouldBeTrue)
  47. }
  48. })
  49. }
  50. func Test_Update_Index(t *testing.T) {
  51. Convey("test update index", t, func() {
  52. e := NewElastic(nil)
  53. us := e.NewUpdate("reply").Insert()
  54. Convey("example by mod 100", func() {
  55. oid := int64(808)
  56. index := us.IndexByMod("reply", oid, 100)
  57. So(index, ShouldEqual, "reply_08")
  58. })
  59. Convey("example by mod 1000", func() {
  60. oid := int64(808)
  61. index := us.IndexByMod("reply", oid, 1000)
  62. So(index, ShouldEqual, "reply_808")
  63. })
  64. Convey("example by mod 10000", func() {
  65. oid := int64(808)
  66. index := us.IndexByMod("reply", oid, 10000)
  67. So(index, ShouldEqual, "reply_0808")
  68. })
  69. Convey("example by mod oid 0", func() {
  70. oid := int64(0)
  71. index := us.IndexByMod("reply", oid, 100)
  72. So(index, ShouldEqual, "reply_00")
  73. })
  74. Convey("example by mod oid 20", func() {
  75. oid := int64(808)
  76. index := us.IndexByMod("reply", oid, 20)
  77. So(index, ShouldEqual, "reply_08")
  78. })
  79. })
  80. }
  81. func Test_Upsert_ByTime(t *testing.T) {
  82. Convey("upsert", t, func() {
  83. data := make([]map[string]interface{}, 0)
  84. d := map[string]interface{}{"id": 22, "share": 22, "ctime": time.Now().AddDate(-2, 0, 0)}
  85. data = append(data, d)
  86. d = map[string]interface{}{"id": 33, "share": 33, "ctime": time.Now().AddDate(-3, 0, 0)}
  87. data = append(data, d)
  88. e := NewElastic(nil)
  89. us := e.NewUpdate("reply_list").Insert()
  90. for _, v := range data {
  91. ctime, ok := v["ctime"]
  92. if !ok {
  93. continue
  94. }
  95. ctimeReal, ok := ctime.(time.Time) //must as same as interface type
  96. if !ok {
  97. continue
  98. }
  99. indexName := us.IndexByTime("reply_list_hot", IndexTypeYear, ctimeReal)
  100. v["ctime"] = ctimeReal.Format("2006-01-02 15:04:05")
  101. us.AddData(indexName, v)
  102. }
  103. t.Logf("params(%s)", us.Params())
  104. if us.HasData() {
  105. err := us.Do(context.Background())
  106. So(err, ShouldBeNil)
  107. } else {
  108. So(us.HasData(), ShouldBeTrue)
  109. }
  110. })
  111. }