123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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
- }
|