es_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "esindex/config"
  6. "fmt"
  7. "github.com/olivere/elastic/v7"
  8. "go.uber.org/zap"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  10. "strings"
  11. "testing"
  12. )
  13. func TestMatchService(t *testing.T) {
  14. data := `{
  15. "_id" : "6422d91e779467cff1a84885",
  16. "area" : "全国",
  17. "city" : "",
  18. "extracttype" : 0,
  19. "s_sha" : "d7cc66ac91dc6551991df0a37331b628de4c70973c6844f1ee6ef1c2d4e29e95",
  20. "jsondata" : {
  21. "area_city_district" : "福建",
  22. "buyer" : "莆田市第一医院",
  23. "item" : " 货物/医药品/医用材料/其他医用材料",
  24. "agency" : "福建省荔卫药械招标服务有限公司"
  25. },
  26. "channel" : "地方公告"
  27. }`
  28. var obj map[string]interface{}
  29. if err := json.Unmarshal([]byte(data), &obj); err != nil {
  30. panic(err)
  31. }
  32. objectType := MatchService(obj)
  33. fmt.Println("objectType=>", objectType)
  34. }
  35. func TestGetMappting(t *testing.T) {
  36. client, _ := elastic.NewClient(
  37. elastic.SetURL(config.Conf.DB.Es.Addr),
  38. elastic.SetBasicAuth(config.Conf.DB.Es.Username, config.Conf.DB.Es.Password),
  39. elastic.SetSniff(false),
  40. )
  41. index := config.Conf.DB.Es.IndexB
  42. // 获取 Elasticsearch 索引的 mapping 信息
  43. mapping, err := client.GetMapping().Index(index).Do(context.Background())
  44. if err != nil {
  45. fmt.Println("Error getting Elasticsearch mapping:", err)
  46. return
  47. }
  48. indexName, _ := GetIndexName(client, index)
  49. properties := mapping[indexName].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
  50. var errField = make([]string, 0)
  51. var okField = make([]string, 0)
  52. var analyzerMap = make(map[string]string) // 分词信息
  53. var esMap = make(map[string]string) //存储es 字段类型
  54. //
  55. for field, ftype := range BiddingField {
  56. eftypeMap, _ := properties[field].(map[string]interface{})
  57. var etype string
  58. var analyzer string
  59. if fftype, ok := eftypeMap["type"]; ok {
  60. etype = fftype.(string)
  61. esMap[field] = etype
  62. }
  63. if ffanalyzer, ok := eftypeMap["analyzer"]; ok {
  64. analyzer = ffanalyzer.(string)
  65. analyzerMap[field] = analyzer
  66. }
  67. if ftype != "" {
  68. if chargeType(ftype, etype) {
  69. okField = append(okField, field)
  70. } else {
  71. errField = append(errField, field)
  72. }
  73. } else {
  74. if field == "_id" {
  75. continue
  76. } else if field == "purchasinglist" || field == "package" || field == "winnerorder" || field == "procurementlist" {
  77. if eproperties, ok := eftypeMap["properties"]; ok {
  78. if eproMap, ok := eproperties.(map[string]interface{}); ok {
  79. for k, v := range eproMap {
  80. if innerMap, ok := v.(map[string]interface{}); ok {
  81. if innerType, ok := innerMap["type"]; ok {
  82. innerLevel := BiddingLevelField[field]
  83. esMap[fmt.Sprintf("%s.%s", field, k)] = innerType.(string)
  84. if chargeType(innerLevel[k], innerType.(string)) {
  85. okField = append(okField, fmt.Sprintf("%s.%s", field, k))
  86. } else {
  87. errField = append(errField, fmt.Sprintf("%s.%s", field, k))
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. if len(errField) > 0 {
  98. log.Info("test", zap.Int("错误字段数量", len(errField)))
  99. for _, field := range errField {
  100. if strings.Contains(field, ".") {
  101. fe := strings.Split(field, ".")
  102. log.Info(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingLevelField[fe[0]][fe[1]]), esMap[field]))
  103. } else {
  104. log.Info(fmt.Sprintf("%s 字段类型错误", field), zap.String(fmt.Sprintf("数据库类型为:%s,但是es字段类型是:", BiddingField[field]), esMap[field]))
  105. }
  106. }
  107. } else {
  108. log.Info("es 字段类型检测结束,", zap.Int("所有字段都符合,检测字段数量为:", len(okField)))
  109. }
  110. }
  111. func TestGetIndexName(t *testing.T) {
  112. client, _ := elastic.NewClient(
  113. elastic.SetURL(config.Conf.DB.Es.Addr),
  114. elastic.SetBasicAuth(config.Conf.DB.Es.Username, config.Conf.DB.Es.Password),
  115. elastic.SetSniff(false),
  116. )
  117. index := "bidding_v2"
  118. //index := config.Conf.DB.Es.IndexB
  119. name, _ := GetIndexName(client, index)
  120. fmt.Println("name ->", name)
  121. fmt.Println(name)
  122. }
  123. func TestBuyer(t *testing.T) {
  124. rowsPerPage := 1000
  125. currentPage := 1
  126. var total int
  127. for {
  128. fmt.Println("currentPage", currentPage)
  129. arrEs := make([]map[string]interface{}, 0)
  130. offset := (currentPage - 1) * rowsPerPage
  131. query := fmt.Sprintf(`
  132. SELECT * from goods
  133. LIMIT %d, %d;
  134. `, offset, rowsPerPage)
  135. result := Mysql.SelectBySql(query)
  136. if len(*result) > 0 {
  137. for _, re := range *result {
  138. arrEs = append(arrEs, re)
  139. }
  140. }
  141. total = total + len(*result)
  142. if len(*result) < rowsPerPage {
  143. break
  144. }
  145. // 继续查询下一页
  146. currentPage++
  147. }
  148. fmt.Println("over --------")
  149. fmt.Println("total --------", total)
  150. }
  151. func TestIsHanStart(t *testing.T) {
  152. name := "\\u001c 陈巴尔虎旗天顺矿业有限责任公司"
  153. fmt.Println("aa", IsCompanyName(name))
  154. //fmt.Println("uni", IsUnicodeStart(name))
  155. fmt.Println("name", getCompanyName(name))
  156. name = "RT农业发展(乌鲁木齐)有限责任公司"
  157. //fmt.Println("uni", IsUnicodeStart(name))
  158. fmt.Println("bb", IsCompanyName(name))
  159. fmt.Println("name", getCompanyName(name))
  160. name = "(宁波)综命能源服务有限公司"
  161. fmt.Println("name", getCompanyName(name))
  162. //fmt.Println("uni", IsUnicodeStart(name))
  163. fmt.Println("cc", IsCompanyName(name))
  164. a := 15
  165. b := 2
  166. fmt.Println((a / b) + 1)
  167. }