query_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package elastic
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "go-common/library/queue/databus/report"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func Test_UserActionLog(t *testing.T) {
  10. Convey("elastic user action log", t, func() {
  11. type page struct {
  12. Num int `json:"num"`
  13. Size int `json:"size"`
  14. Total int `json:"total"`
  15. }
  16. var res struct {
  17. Page *page `json:"page"`
  18. Result []*report.UserActionLog `json:"result"`
  19. }
  20. r := NewElastic(nil).NewRequest("log_user_action").Index("log_user_action_21_2018_06_1623")
  21. err := r.Scan(context.TODO(), &res)
  22. So(err, ShouldBeNil)
  23. t.Logf("query page(%+v)", res.Page)
  24. t.Logf("query result(%v)", len(res.Result))
  25. })
  26. }
  27. func Test_Scan(t *testing.T) {
  28. Convey("scan", t, func() {
  29. type page struct {
  30. Num int `json:"num"`
  31. Size int `json:"size"`
  32. Total int `json:"total"`
  33. }
  34. type ticketData struct {
  35. ID int `json:"id"`
  36. Name string `json:"name"`
  37. City int `json:"city"`
  38. Province int `json:"province"`
  39. }
  40. var res struct {
  41. Page *page `json:"page"`
  42. Result []*ticketData `json:"result"`
  43. }
  44. r := NewElastic(nil).NewRequest("ticket_venue").Fields("id", "city").Index("ticket_venue").WhereEq("city", 310100).Order("ctime", OrderDesc).Order("id", OrderAsc).
  45. WhereOr("province", 310000).WhereRange("id", 1, 2000000, RangeScopeLcRc).WhereLike([]string{"name"}, []string{"梅赛德斯奔驰文化中心"}, true, LikeLevelHigh)
  46. err := r.Scan(context.TODO(), &res)
  47. So(err, ShouldBeNil)
  48. t.Logf("query page(%+v)", res.Page)
  49. t.Logf("query result(%v)", len(res.Result))
  50. })
  51. }
  52. func Test_Index(t *testing.T) {
  53. Convey("example index by mod", t, func() {
  54. oid := int64(8888888)
  55. r := NewElastic(nil).NewRequest("LOG_USER_COIN")
  56. r.IndexByMod("log_user_action", oid, 100)
  57. q, _ := r.q.string()
  58. So(q, ShouldContainSubstring, `"from":"log_user_action_88"`)
  59. })
  60. Convey("example index by time", t, func() {
  61. Convey("indexByTime by week", func() {
  62. // 按周划分索引的方式
  63. r := NewElastic(nil).NewRequest("LOG_USER_COIN")
  64. r.IndexByTime("log_user_action", IndexTypeWeek, time.Date(2018, 8, 25, 2, 0, 0, 0, time.Local), time.Date(2018, 9, 24, 2, 0, 0, 0, time.Local))
  65. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_08_2431")
  66. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_0107")
  67. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_0815")
  68. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_1623")
  69. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_2431")
  70. })
  71. Convey("indexByTime by year", func() {
  72. // 按年划分索引的方式
  73. r := NewElastic(nil).NewRequest("LOG_USER_COIN")
  74. r.IndexByTime("log_user_action", IndexTypeYear, time.Date(2017, 8, 25, 2, 0, 0, 0, time.Local), time.Date(2018, 9, 24, 2, 0, 0, 0, time.Local))
  75. So(r.q.From, ShouldContainSubstring, "log_user_action_2017")
  76. So(r.q.From, ShouldContainSubstring, "log_user_action_2018")
  77. })
  78. Convey("indexByTime by month", func() {
  79. // 按月划分索引的方式
  80. r := NewElastic(nil).NewRequest("LOG_USER_COIN")
  81. r.IndexByTime("log_user_action", IndexTypeMonth, time.Date(2018, 8, 25, 2, 0, 0, 0, time.Local), time.Date(2018, 9, 24, 2, 0, 0, 0, time.Local))
  82. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_08")
  83. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09")
  84. })
  85. Convey("indexByTime by day", func() {
  86. // 按天划分索引的方式
  87. r := NewElastic(nil).NewRequest("LOG_USER_COIN")
  88. r.IndexByTime("log_user_action", IndexTypeDay, time.Date(2018, 9, 23, 4, 0, 0, 0, time.Local), time.Date(2018, 9, 24, 2, 0, 0, 0, time.Local))
  89. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_23")
  90. So(r.q.From, ShouldContainSubstring, "log_user_action_2018_09_24")
  91. })
  92. })
  93. }
  94. func Test_Combo_Simple(t *testing.T) {
  95. Convey("combo", t, func() {
  96. e := NewElastic(nil)
  97. // 实现: (tid in (2,3,4,5,9,20,21,22)) && (tid in (35,38)) && (tid in (15,17,18))
  98. cmbA := &Combo{}
  99. cmbA.ComboIn([]map[string][]interface{}{
  100. {"tid": {2, 3, 4, 5, 9, 20, 21, 22}},
  101. }).MinIn(1).MinAll(1)
  102. cmbB := &Combo{}
  103. cmbB.ComboIn([]map[string][]interface{}{
  104. {"tid": {35, 38}},
  105. }).MinIn(1).MinAll(1)
  106. cmbC := &Combo{}
  107. cmbC.ComboIn([]map[string][]interface{}{
  108. {"tid": {15, 17, 18}},
  109. }).MinIn(1).MinAll(1)
  110. r := e.NewRequest("").WhereCombo(cmbA, cmbB, cmbC)
  111. q, _ := r.q.string()
  112. So(q, ShouldContainSubstring, `"where":{"combo":[{"eq":null,"in":[{"tid":[2,3,4,5,9,20,21,22]}],"range":null,"min":{"eq":0,"in":1,"range":0,"min":1}},{"eq":null,"in":[{"tid":[35,38]}],"range":null,"min":{"eq":0,"in":1,"range":0,"min":1}},{"eq":null,"in":[{"tid":[15,17,18]}],"range":null,"min":{"eq":0,"in":1,"range":0,"min":1}}]}`)
  113. })
  114. }
  115. func Test_Combo_Complicated(t *testing.T) {
  116. Convey("combo not", t, func() {
  117. e := NewElastic(nil)
  118. cmb := &Combo{}
  119. cmb.ComboIn([]map[string][]interface{}{
  120. {"tid": {1, 2}},
  121. {"tid_type": {2, 3}},
  122. }).ComboRange([]map[string]string{
  123. {"id": "(10,20)"},
  124. }).ComboNotEQ([]map[string]interface{}{
  125. {"aid": 122},
  126. {"id": 677},
  127. }).MinIn(1).MinRange(1).MinNotEQ(1).MinAll(1)
  128. r := e.NewRequest("").WhereCombo(cmb)
  129. q, _ := r.q.string()
  130. So(q, ShouldContainSubstring, `"where":{"combo":[{"in":[{"tid":[1,2]},{"tid_type":[2,3]}],"range":[{"id":"(10,20)"}],"not_eq":[{"aid":122},{"id":677}],"min":{"in":1,"range":1,"not_eq":1,"min":1}}]}}`)
  131. })
  132. Convey("combo", t, func() {
  133. e := NewElastic(nil)
  134. // 实现:
  135. // (aid=122 or id=677) && (tid in (1,2,3,21) or tid_type in (1,2,3)) && (id > 10) &&
  136. // (aid=88 or fid=99) && (mid in (11,33) or id in (22,33)) && (2 < cid <= 10 || sid > 10)
  137. cmbA := &Combo{}
  138. cmbA.ComboEQ([]map[string]interface{}{
  139. {"aid": 122},
  140. {"id": 677},
  141. }).ComboIn([]map[string][]interface{}{
  142. {"tid": {1, 2, 3, 21}},
  143. {"tid_type": {1, 2, 3}},
  144. }).ComboRange([]map[string]string{
  145. {"id": "(10,)"},
  146. }).MinIn(1).MinEQ(1).MinRange(1).MinAll(1)
  147. cmbB := &Combo{}
  148. cmbB.ComboEQ([]map[string]interface{}{
  149. {"aid": 88},
  150. {"fid": 99},
  151. }).ComboIn([]map[string][]interface{}{
  152. {"mid": {11, 33}},
  153. {"id": {22, 33}},
  154. }).ComboRange([]map[string]string{
  155. {"cid": "(2,4]"},
  156. {"sid": "(10,)"},
  157. }).MinEQ(1).MinIn(2).MinRange(1).MinAll(1)
  158. r := e.NewRequest("").WhereCombo(cmbA, cmbB)
  159. q, _ := r.q.string()
  160. So(q, ShouldContainSubstring, `"where":{"combo":[{"eq":[{"aid":122},{"id":677}],"in":[{"tid":[1,2,3,21]},{"tid_type":[1,2,3]}],"range":[{"id":"(10,)"}],"min":{"eq":1,"in":1,"range":1,"min":1}},{"eq":[{"aid":88},{"fid":99}],"in":[{"mid":[11,33]},{"id":[22,33]}],"range":[{"cid":"(2,4]"},{"sid":"(10,)"}],"min":{"eq":1,"in":2,"range":1,"min":1}}]}`)
  161. })
  162. }
  163. func Test_Query(t *testing.T) {
  164. Convey("query conditions", t, func() {
  165. e := NewElastic(nil)
  166. r := e.NewRequest("").WhereEq("city", 310100)
  167. q, _ := r.q.string()
  168. So(q, ShouldContainSubstring, `"where":{"eq":{"city":310100}}`)
  169. r = e.NewRequest("").WhereLike([]string{"a", "b"}, []string{"c", "d"}, true, LikeLevelHigh).WhereLike([]string{"e", "f"}, []string{"g", "h"}, false, LikeLevelMiddle)
  170. q, _ = r.q.string()
  171. So(q, ShouldContainSubstring, `"where":{"like":[{"kw_fields":["a","b"],"kw":["c","d"],"or":true,"level":"high"},{"kw_fields":["e","f"],"kw":["g","h"],"or":false,"level":"middle"}]}`)
  172. r = e.NewRequest("").WhereNot(NotTypeEq, "province")
  173. q, _ = r.q.string()
  174. So(q, ShouldContainSubstring, `"where":{"not":{"eq":{"province":true}}}`)
  175. r = e.NewRequest("").WhereRange("id", 100, 500, RangeScopeLcRo).WhereRange("date", "2018-08-08 08:08:08", "2019-09-09 09:09:09", RangeScopeLoRo)
  176. q, _ = r.q.string()
  177. So(q, ShouldContainSubstring, `"where":{"range":{"date":"(2018-08-08 08:08:08,2019-09-09 09:09:09)","id":"[100,500)"}}`)
  178. r = e.NewRequest("").WhereOr("city", 100000)
  179. q, _ = r.q.string()
  180. So(q, ShouldContainSubstring, `"where":{"or":{"city":100000}}`)
  181. ids := []int64{100, 200, 300}
  182. r = e.NewRequest("").WhereIn("city", ids)
  183. q, _ = r.q.string()
  184. So(q, ShouldContainSubstring, `"where":{"in":{"city":[100,200,300]}}`)
  185. strs := []string{"a"}
  186. r = e.NewRequest("").WhereIn("name", strs)
  187. q, _ = r.q.string()
  188. So(q, ShouldContainSubstring, `"where":{"in":{"name":["a"]}}`)
  189. field := "a"
  190. order := []map[string]string{{"a": "asc"}}
  191. r = e.NewRequest("").GroupBy(EnhancedModeGroupBy, field, order)
  192. q, _ = r.q.string()
  193. So(q, ShouldContainSubstring, `"where":{"enhanced":[{"mode":"group_by","field":"a","order":[{"a":"asc"}]}]}`)
  194. field = "a"
  195. r = e.NewRequest("").Sum(field)
  196. q, _ = r.q.string()
  197. So(q, ShouldContainSubstring, `"where":{"enhanced":[{"mode":"sum","field":"a"}]}`)
  198. })
  199. }