package elastic import ( "context" "eta/eta_index_lib/models" "eta/eta_index_lib/utils" "fmt" "strings" ) func EsCreateIndex(indexName, mappingJson string) (err error) { client := utils.EsClient //定义表结构 exists, err := client.IndexExists(indexName).Do(context.Background()) //<5> if err != nil { return } if !exists { resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background()) //BodyJson(bodyJson).Do(context.Background()) if err != nil { fmt.Println("CreateIndex Err:" + err.Error()) return err } fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged) } else { fmt.Println(indexName + " 已存在") } return } // EsAddOrEditEdbInfoData 新增/修改es中的指标数据 func EsAddOrEditEdbInfoData(indexName, docId string, item *models.EdbInfoList) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditData Err:", err.Error()) } }() client := utils.EsClient if err != nil { return } resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { fmt.Println("新增失败:", err.Error()) return err } fmt.Println(resp) if resp.Status == 0 { fmt.Println("新增成功", resp.Result) err = nil } else { fmt.Println("AddData", resp.Status, resp.Result) } return } // EsAddOrEditBaseFromYongyiIndex 新增/修改es中的原始涌溢指标 func EsAddOrEditBaseFromYongyiIndex(indexName, docId string, item *models.BaseFromYongyiIndexList) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditData Err:", err.Error()) } }() client := utils.EsClient if err != nil { return } resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { if strings.Contains(err.Error(), "no such index") { //新增index err = CreateEsEditBaseFromYongyiIndex() if err != nil { return } resp, err = client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { return } } else { fmt.Println("新增失败:", err.Error()) return err } } fmt.Println(resp) if resp.Status == 0 { fmt.Println("新增成功", resp.Result) err = nil } else { fmt.Println("AddData", resp.Status, resp.Result) } return } func CreateEsEditBaseFromYongyiIndex() (err error) { indexName := utils.ES_INDEX_BASE_FROM_YONGYI_INDEX mappingJson := `{ "mappings": { "dynamic": true, "properties": { "YongyiIndexId": { "type" : "long" }, "IndexName": { "type": "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "ClassifyId": { "type" : "long" }, "Frequency": { "type": "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "IndexCode": { "type": "text" }, "Unit": { "type": "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "CreateTime": { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "ModifyTime": { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "StartDate": { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "EndDate": { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "TerminalCode": { "type": "text" } } } }` err = EsCreateIndex(indexName, mappingJson) return }