123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package main
- import (
- "context"
- "encoding/json"
- "fmt"
- "github.com/olivere/elastic/v7"
- "reflect"
- "testing"
- )
- func TestMatchService(t *testing.T) {
- data := `{
- "_id" : "6422d91e779467cff1a84885",
- "area" : "全国",
- "city" : "",
- "extracttype" : 0,
- "s_sha" : "d7cc66ac91dc6551991df0a37331b628de4c70973c6844f1ee6ef1c2d4e29e95",
- "jsondata" : {
- "area_city_district" : "福建",
- "buyer" : "莆田市第一医院",
- "item" : " 货物/医药品/医用材料/其他医用材料",
- "agency" : "福建省荔卫药械招标服务有限公司"
- },
- "channel" : "地方公告"
- }`
- var obj map[string]interface{}
- if err := json.Unmarshal([]byte(data), &obj); err != nil {
- panic(err)
- }
- objectType := MatchService(obj)
- fmt.Println("objectType=>", objectType)
- }
- func TestGetMappting(t *testing.T) {
- client, _ := elastic.NewClient(
- elastic.SetURL("http://192.168.3.149:9200"),
- elastic.SetSniff(false),
- )
- index := "wcc_test"
- // 获取 Elasticsearch 索引的 mapping 信息
- mapping, err := client.GetMapping().Index(index).Do(context.Background())
- if err != nil {
- fmt.Println("Error getting Elasticsearch mapping:", err)
- return
- }
- properties := mapping[index].(map[string]interface{})["mappings"].(map[string]interface{})["properties"].(map[string]interface{})
- fmt.Println(properties)
- var errField = make([]string, 0)
- //for _, v := range BiddingField {
- // if v != "" {
- //
- // }
- //}
- for k, v := range properties {
- b, ok := BiddingField[k]
- if ok {
- mappingType := v.(map[string]interface{})["type"].(string)
- analyzer := v.(map[string]interface{})["analyzer"].(string)
- //分词器不为空
- if analyzer != "" {
- }
- //="",表示二级
- if b == "" {
- fmt.Println(111)
- } else {
- if mappingType == "keyword" || mappingType == "text" {
- if b != "string" {
- errField = append(errField, k)
- }
- } else if mappingType == "boolean" {
- if b != "bool" {
- errField = append(errField, k)
- }
- } else if mappingType == "" {
- }
- fmt.Println(222)
- }
- }
- }
- }
- func getGoVarType(esMappingType string) reflect.Type {
- var goVarType reflect.Type
- switch esMappingType {
- case "text", "keyword", "ip", "geo_point", "date", "binary":
- goVarType = reflect.TypeOf("")
- case "long":
- goVarType = reflect.TypeOf(int64(0))
- case "integer":
- goVarType = reflect.TypeOf(int32(0))
- case "short":
- goVarType = reflect.TypeOf(int16(0))
- case "byte":
- goVarType = reflect.TypeOf(int8(0))
- case "double":
- goVarType = reflect.TypeOf(0.0)
- case "float":
- goVarType = reflect.TypeOf(float32(0.0))
- case "boolean":
- goVarType = reflect.TypeOf(false)
- case "nested":
- goVarType = reflect.TypeOf(make(map[string]interface{}))
- default:
- goVarType = reflect.TypeOf(nil)
- }
- return goVarType
- }
|