elasticutil_Index_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package elastic
  2. import (
  3. "encoding/json"
  4. "log"
  5. _ "log"
  6. "net/url"
  7. _ "gopkg.in/mgo.v2/bson"
  8. _ "sfbase/olivere/elastic.v1"
  9. "qfw/util/mongodb"
  10. "testing"
  11. )
  12. func Test_DelIndex(t *testing.T) {
  13. InitElastic("http://192.168.3.14:9800")
  14. client := GetEsConn()
  15. client.DeleteIndex("zcfg_v1").Do()
  16. //client.c("qfw", "qfw2").Do()
  17. //l1 := time.Now().Unix()
  18. //log.Println(util.LongToDate(l1, true))
  19. DestoryEsConn(client)
  20. }
  21. func Test_createMapping(t *testing.T) {
  22. InitElastic("http://192.168.3.14:9800")
  23. client := GetEsConn()
  24. //client.DeleteIndex("qfw").Do()
  25. var testMapping = `{"mappings":{
  26. "enterprise":{
  27. "_id" : {
  28. "path" : "_id"
  29. },
  30. "properties":{
  31. "EntName" : {
  32. "type" : "string",
  33. "analyzer" : "ik"
  34. }
  35. }
  36. }
  37. }
  38. }`
  39. _ = testMapping
  40. createIndex2, err := client.CreateIndex("qfw2").Do()
  41. log.Println(createIndex2, err.Error())
  42. DestoryEsConn(client)
  43. }
  44. func Test_saveMongodbToEs(t *testing.T) {
  45. mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw")
  46. InitElastic("http://192.168.3.14:9800")
  47. res := mongodb.Find("content", `{}`, nil, nil, false, -1, -1)
  48. log.Println(len(*res))
  49. BulkSave("content_v2", "content", res, false)
  50. }
  51. func Test_saveContentMongodbToEs(t *testing.T) {
  52. mongodb.InitMongodbPool(1, "192.168.3.18:27080", "qfw")
  53. InitElastic("http://192.168.3.18:9800")
  54. res := mongodb.Find("enterprise", `{}`, nil, nil, false, -1, -1)
  55. log.Println(len(*res))
  56. BulkSave("enterprise", "enterprise", res, false)
  57. }
  58. func Test_find1(t *testing.T) {
  59. InitElastic("http://192.168.3.14:9800")
  60. log.Println(GetPage("qfw_back", "enterprise", `{"RegOrgName":"中心"}`, "", `"RegOrgName"`, 1, 2))
  61. }
  62. func Test_Convert(t *testing.T) {
  63. mongodb.InitMongodbPool(1, "192.168.3.14:27080", "qfw")
  64. //InitElastic("http://192.168.3.14:9800")
  65. log.Println(ConverData(mongodb.FindOne("enterprise", `{"_id":"556db9f0c2e8753072b52ee9"}`)))
  66. }
  67. func Test_saveToEs(t *testing.T) {
  68. InitElasticSize("http://192.168.3.18:9800", 2)
  69. Save("bidlibrary", "bidlibrary", map[string]interface{}{
  70. "_id": "556db9f0c2e8753072b52ee5",
  71. "title": "你好,试试2",
  72. })
  73. }
  74. func Test_word(t *testing.T) {
  75. InitElasticSize("http://192.168.3.207:9700", 2)
  76. es := GetEsConn()
  77. defer DestoryEsConn(es)
  78. p := url.Values{}
  79. p["text"] = []string{"中华人民 共和国"}
  80. p["analyzer"] = []string{"ik"}
  81. by, err := es.PerformRequest("GET", "/test1/_analyze", p, nil)
  82. b, _ := by.Body.MarshalJSON()
  83. var res map[string]interface{}
  84. json.Unmarshal(b, &res)
  85. log.Println(res, err)
  86. }