es_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/olivere/elastic/v7"
  7. "reflect"
  8. "testing"
  9. )
  10. func TestMatchService(t *testing.T) {
  11. data := `{
  12. "_id" : "6422d91e779467cff1a84885",
  13. "area" : "全国",
  14. "city" : "",
  15. "extracttype" : 0,
  16. "s_sha" : "d7cc66ac91dc6551991df0a37331b628de4c70973c6844f1ee6ef1c2d4e29e95",
  17. "jsondata" : {
  18. "area_city_district" : "福建",
  19. "buyer" : "莆田市第一医院",
  20. "item" : " 货物/医药品/医用材料/其他医用材料",
  21. "agency" : "福建省荔卫药械招标服务有限公司"
  22. },
  23. "channel" : "地方公告"
  24. }`
  25. var obj map[string]interface{}
  26. if err := json.Unmarshal([]byte(data), &obj); err != nil {
  27. panic(err)
  28. }
  29. objectType := MatchService(obj)
  30. fmt.Println("objectType=>", objectType)
  31. }
  32. func TestGetMappting(t *testing.T) {
  33. client, _ := elastic.NewClient(
  34. elastic.SetURL("http://192.168.3.149:9200"),
  35. elastic.SetSniff(false),
  36. )
  37. index := "wcc_test"
  38. // 获取 Elasticsearch 索引的 mapping 信息
  39. mapping, err := client.GetMapping().Index(index).Do(context.Background())
  40. if err != nil {
  41. fmt.Println("Error getting Elasticsearch mapping:", err)
  42. return
  43. }
  44. properties := mapping[index].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
  45. fmt.Println(properties)
  46. var errField = make([]string, 0)
  47. //for _, v := range BiddingField {
  48. // if v != "" {
  49. //
  50. // }
  51. //}
  52. for k, v := range properties {
  53. b, ok := BiddingField[k]
  54. if ok {
  55. mappingType := v.(map[string]interface{})["type"].(string)
  56. analyzer := v.(map[string]interface{})["analyzer"].(string)
  57. //分词器不为空
  58. if analyzer != "" {
  59. }
  60. //="",表示二级
  61. if b == "" {
  62. fmt.Println(111)
  63. } else {
  64. if mappingType == "keyword" || mappingType == "text" {
  65. if b != "string" {
  66. errField = append(errField, k)
  67. }
  68. } else if mappingType == "boolean" {
  69. if b != "bool" {
  70. errField = append(errField, k)
  71. }
  72. } else if mappingType == "" {
  73. }
  74. fmt.Println(222)
  75. }
  76. }
  77. }
  78. }
  79. func getGoVarType(esMappingType string) reflect.Type {
  80. var goVarType reflect.Type
  81. switch esMappingType {
  82. case "text", "keyword", "ip", "geo_point", "date", "binary":
  83. goVarType = reflect.TypeOf("")
  84. case "long":
  85. goVarType = reflect.TypeOf(int64(0))
  86. case "integer":
  87. goVarType = reflect.TypeOf(int32(0))
  88. case "short":
  89. goVarType = reflect.TypeOf(int16(0))
  90. case "byte":
  91. goVarType = reflect.TypeOf(int8(0))
  92. case "double":
  93. goVarType = reflect.TypeOf(0.0)
  94. case "float":
  95. goVarType = reflect.TypeOf(float32(0.0))
  96. case "boolean":
  97. goVarType = reflect.TypeOf(false)
  98. case "nested":
  99. goVarType = reflect.TypeOf(make(map[string]interface{}))
  100. default:
  101. goVarType = reflect.TypeOf(nil)
  102. }
  103. return goVarType
  104. }