elasticutil_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package elastic
  2. import (
  3. "fmt"
  4. "log"
  5. "qfw/util/mongodb"
  6. "testing"
  7. "time"
  8. "gopkg.in/mgo.v2/bson"
  9. _ "gopkg.in/olivere/elastic.v1"
  10. )
  11. func Test_Find1(t *testing.T) {
  12. InitElasticSize("http://192.168.3.18:9800", 5)
  13. log.Println(Get("service_v1", "service", `{"query":{"bool":{"must":[],"must_not":[],"should":[],"minimum_should_match" : 1}},"_source":["_id","s_name"],"sort":[{"l_createdate":"desc"}]}`))
  14. }
  15. func Test_save(t *testing.T) {
  16. InitElastic("http://192.168.3.14:9800")
  17. //client.CreateIndex("test").Body(testMapping).Do()
  18. bid := bson.NewObjectId()
  19. map1 := map[string]interface{}{
  20. "_id": bid,
  21. "name": "不知道,就是你们我的",
  22. "age": 503,
  23. "addr": "可就不呀,2245555555是不是5555",
  24. }
  25. b := Save("test", "test", &map1)
  26. log.Println(b, bid, bid.String())
  27. //log.Println("保存结果", b, DelById("test", "test", bid.String()))
  28. }
  29. func Test_GoQ(t *testing.T) {
  30. InitElastic("http://192.168.3.18:9800")
  31. //"$中不支持range查询"
  32. //res := GetPage("bidding", "bidding", `{"$or":[{"TERM_toptype":"拟建"},{"TERM_toptype":"结果"}],"extracttype":{"$gt":1},"TERM_area": "广东","$and":[{"TERM_area": "广东"}{"TERM_area": "广东"}]}`, `{"id":-1}`, `"title","toptype"`, 0, 50)
  33. res := GetPage("bidding", "bidding", `{"$or":[{"TERM_toptype":"拟建"},{"TERM_toptype":"结果"}]}`, `{"publishtime":-1}`, `"title","toptype","publishtime"`, 0, 50)
  34. log.Println(res)
  35. }
  36. func Test_get(t *testing.T) {
  37. InitElastic("http://192.168.3.14:9800")
  38. a := make(chan int, 1)
  39. for i := 0; i < 1000; i++ {
  40. go func(b int) {
  41. log.Println(b, len(*GetPage("content", "content", `{}`, "", "", 0, 20)))
  42. }(i)
  43. }
  44. <-a
  45. //Q1 := `{"name":"*走*"}`
  46. //支持类mongodb的sql中的$and,$or $gt $lt
  47. //Q1 := `{"age":{"$lt":11}}`
  48. //log.Println("解析Q1,生成elastic所有的DSL,一般不直接调用:", AnalyQuery(Q1, "", QStr))
  49. //log.Println("解析Q1、排序、显示字段,分页,生成elastic所有的DSL,一般不直接调用:", MakeQuery(Q1, `{"name":1,"age":-1}`, `"name","age"`, -1, 0))
  50. //log.Println("分页查询,排序和显示字段,分页:", GetPage("test", "test", Q1, "", "", 0, 2))
  51. //原生的查询方法,需要自己写DSL查询
  52. //s := Get("test", `{"query" : { "bool": { "must":[{"query_string":{"default_field":"name","query":"*"}}]}}}`)
  53. //s := Get("test", "test", `{"query":{"match_all":{}}}`)
  54. //log.Println("查找全部", s)
  55. }
  56. func Test_GetById(t *testing.T) {
  57. InitElastic("http://192.168.3.14:9800")
  58. log.Println("GetById,获取对象", GetById("test", "test", "1", "553ddbc7a75c550890000001"))
  59. }
  60. func Test_DelType(t *testing.T) {
  61. InitElastic("http://192.168.3.14:9800")
  62. //Del("qfw", "enterprise", MakeQuery(`{}`, "", "", -1, -1))
  63. c := GetEsConn()
  64. //c.Delete().Index("qfw").Type("enterprise").Do()
  65. //c.Refresh("qfw")
  66. c.Alias().Add("qfw", "qfw_back").Do()
  67. }
  68. func Test_Del(t *testing.T) {
  69. InitElastic("http://192.168.3.14:9800")
  70. /*
  71. Save("test", "test", map[string]interface{}{
  72. "_id": 291,
  73. "name": "走啊走",
  74. "name2": map[string]interface{}{
  75. "aa": "完全不好使",
  76. "bb": []int{1, 2, 3},
  77. "cc": 29,
  78. },
  79. "age": 29,
  80. "num": 112,
  81. "addr": "可就不呀,224是不是",
  82. })
  83. */
  84. //log.Println(Get("test", "test", MakeQuery(`{"name2.aa":"0"}`, "", "", -1, -1)))
  85. //log.Println("--:--", es.NewQueryStringQuery(`{"query":{"match":{"age":50}}}`).Query)
  86. //log.Println("1:", MakeQuery(`{"_id":2}`, "", "", -1, -1))
  87. //log.Println("Del方法,删除:", Del("test", "test", MakeQuery(`{"$and":[{"age":{"$gte":28}},{"age":{"$lt":30}}],"TERM_num":112,"name2.cc":29}`, "", "", -1, -1)))
  88. //log.Println("Del方法,删除:", Del("test", "test", MakeQuery(`{}`, "", "", -1, -1)))
  89. DelById("qfw", "content", "AU2P5rW95E6XJdgtD0Eh")
  90. }
  91. func Test_Update(t *testing.T) {
  92. InitElastic("http://192.168.3.14:9800")
  93. log.Println("更新结果", Update("test", "test", "9", `ctx._source.age=101;ctx._source.name="张三"`))
  94. }
  95. func Test_gg(t *testing.T) {
  96. InitElastic("http://192.168.3.14:9800")
  97. vv := Get("service", "service", `{"query":{"term":{"s_enterpriseid":"`+"555446f6120d8f654414a81f"+`"}},
  98. _source:[`+`"_id"`+`],
  99. sort:{"l_createdate":"desc"},
  100. from:`+fmt.Sprintf("%v", 0)+`,
  101. size:`+fmt.Sprintf("%v", 10)+`
  102. }`)
  103. log.Println(vv)
  104. }
  105. func Test_DelById(t *testing.T) {
  106. InitElastic("http://192.168.3.14:9800")
  107. // Save("test", "test", map[string]interface{}{
  108. // "_id": 101,
  109. // "name": "走啊走",
  110. // "name2": map[string]interface{}{
  111. // "aa": "完全不好使",
  112. // },
  113. // "age": 220,
  114. // "addr": "可就不呀,224是不是",
  115. // })
  116. log.Println("删除结果", DelById("test", "test", "559334fb91db0a21a400000a"))
  117. }
  118. //先删除后增
  119. func Test_UpdateNewDoc(t *testing.T) {
  120. InitElastic("http://192.168.3.14:9800")
  121. log.Println("完整更新对象", UpdateNewDoc("service", "service", `{"_id":56497eb0bbb93e2070000002,"name5":"我是新对象3~","tt":{"name":"内嵌的name"}}`))
  122. }
  123. //批量保存/更新
  124. func Test_BulkSave(t *testing.T) {
  125. InitElastic("http://192.168.3.14:9800")
  126. // res := []map[string]interface{}{
  127. // map[string]interface{}{
  128. // "_id": 51,
  129. // "name9": "map第11",
  130. // },
  131. // map[string]interface{}{
  132. // "_id": 52,
  133. // "name9": "map第22",
  134. // },
  135. // map[string]interface{}{
  136. // "_id": 53,
  137. // "name9": "map第33",
  138. // },
  139. // }
  140. mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw")
  141. // res := mongodb.Find("bidding", `{"infoformat":2}`, nil, nil, false, 0, 50)
  142. // for _, v := range *res {
  143. // Save("bidding", "bidding", v)
  144. // }
  145. res := mongodb.FindById("bidding", "5833a0452d76c41b289b41f7", nil)
  146. log.Println("res:", res)
  147. Save("bidding", "bidding", res)
  148. log.Println("批量保存对象:")
  149. //BulkSave("test", "test", &res, true)
  150. }
  151. //批量更新字段
  152. func Test_BulkUpdate(t *testing.T) {
  153. InitElastic("http://192.168.3.18:9800")
  154. //regno 540192600031442,542422600000086
  155. ids := []string{"55a85997a442ceca9e20de9f", "55a85997a442ceca9e20deaf"}
  156. BulkUpdate("enterprise", "enterprise", ids, `ctx._source.Ycml=false`)
  157. }
  158. //获取数据,不受限制
  159. func Test_getNoLimit(t *testing.T) {
  160. InitElasticSize("http://192.168.3.18:9800", 1)
  161. k := "EntType"
  162. v := "9600"
  163. query := `{"query": {"bool":{"must":[{"query_string":{"default_field":"` + k + `","query":"` + v + `"}},{"query_string":{"default_field":"Ycml","query":true}}]}},"_source": ["_id","EntName"],"from": 0,"size": 999999}`
  164. nt := GetNoLimit("enterprise", "enterprise", query)
  165. log.Println("总数", len(*nt))
  166. }
  167. func Test_Count(t *testing.T) {
  168. log.Println("ESCOUNT", Count("test", "test", MakeQuery(`{"name":"你们","TERM_age":503}`, "", "", -1, -1)))
  169. }
  170. func Test_tmp(t *testing.T) {
  171. InitElastic("http://192.168.3.14:9800")
  172. DelById("qfw", "enterprise", "55633f50a442ab325175252b")
  173. }
  174. func Test_updateEntDoc(t *testing.T) {
  175. InitElastic("http://192.168.3.14:9800")
  176. mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw")
  177. UpdateEntDoc("556d858ec2e875307286f868")
  178. }
  179. func Test_first(t *testing.T) {
  180. InitElasticSize("http://192.168.3.14:9800", 1)
  181. nt := GetPage("content", "content", `{"releasetime": {"$lt": 1440773202}, "s_contenttype": "qykb"}`, `{"releasetime":-1}`, `{"s_title":1}`, 0, 2)
  182. var i = 0
  183. for _, v := range *nt {
  184. log.Println("\n", v, i)
  185. i++
  186. }
  187. }
  188. func Test_date(t *testing.T) {
  189. //"1448267541"
  190. //s := time.Now().UnixNano()
  191. //log.Println(s, time.Now().Unix(), fmt.Sprintf("%d", s)[4:12], 999999/60/60)
  192. t1 := time.Now().Unix()
  193. t2 := t1 + 10000
  194. tt1 := time.Unix(t1, 0)
  195. tt2 := time.Unix(t2, 0)
  196. nt1 := time.Date(tt1.Year(), tt1.Month(), tt1.Day(), 0, 0, 0, 0, time.Local)
  197. nt2 := time.Date(tt2.Year(), tt2.Month(), tt2.Day(), 0, 0, 0, 0, time.Local)
  198. log.Println((nt2.Unix() - nt1.Unix()) / 86400)
  199. }
  200. func Test_Getpage(t *testing.T) {
  201. InitElasticSize("http://192.168.3.18:9800", 1)
  202. data := *GetPage("enterprise", "enterprise", `{"s_action":"01"}`, `{"l_submittime":-1}`, `"EntName","l_submittime","_id"`, 0, 8)
  203. log.Println("data:", data)
  204. }
  205. func Test_q(t *testing.T) {
  206. //log.Println(MakeQuery(`{"_id":"556349a6a442ab325177ade0"}`, "", `"_id"`, -1, -1))
  207. //Count("enterprise","enterprise",)
  208. }
  209. func Test_bidding(t *testing.T) {
  210. InitElasticSize("http://192.168.3.18:9800", 1)
  211. //log.Println(Count("bidding_v1", "bidding", GetNgramQuery("河南+工商", `"title"`)))
  212. log.Println(GetByNgramAll("bidding_v1", "bidding", "河南+郑州", `{"term":{"_id":"57aadfdf61a0721f1519c19d"}}`, `"title","detail"`, `{"publishtime":-1}`, `"title","_id"`, 0, 10, true, false))
  213. //res := GetPage("bidding", "bidding", "", `{"comeintime":-1}`, `"_id"`, 0, 1)
  214. //log.Println((*res)[0]["_id"])
  215. }