package services import ( "context" "eta/eta_task/models" "eta/eta_task/services/alarm_msg" "eta/eta_task/utils" "fmt" "github.com/olivere/elastic/v7" "strings" ) func NewClient() (client *elastic.Client, err error) { client, err = elastic.NewClient( elastic.SetURL(utils.ES_URL), elastic.SetBasicAuth(utils.ES_USERNAME, utils.ES_PASSWORD), elastic.SetSniff(false)) return } // EsAddOrEditReport 新增编辑es报告 func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditReport Err:", err.Error()) } }() client, err := NewClient() if err != nil { return } // docId为报告ID+章节ID searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background()) if err != nil && !strings.Contains(err.Error(), "404") { fmt.Println("Get Err" + err.Error()) return } if searchById != nil && searchById.Found { resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{ "ReportId": item.ReportId, "ReportChapterId": item.ReportChapterId, "Title": item.Title, "Abstract": item.Abstract, "BodyContent": item.BodyContent, "PublishTime": item.PublishTime, "PublishState": item.PublishState, "Author": item.Author, "ClassifyIdFirst": item.ClassifyIdFirst, "ClassifyNameFirst": item.ClassifyNameFirst, "ClassifyIdSecond": item.ClassifyIdSecond, "ClassifyNameSecond": item.ClassifyNameSecond, "Categories": item.Categories, "StageStr": item.StageStr, }).Do(context.Background()) if err != nil { return err } //fmt.Println(resp.Status, resp.Result) if resp.Status == 0 { fmt.Println("修改成功" + docId) err = nil } else { fmt.Println("EditData", resp.Status, resp.Result) } } else { resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { fmt.Println("新增失败:", err.Error()) return err } if resp.Status == 0 && resp.Result == "created" { fmt.Println("新增成功" + docId) return nil } else { fmt.Println("AddData", resp.Status, resp.Result) } } return } // EsAddOrEditEnglishReport 新增编辑es英文报告 func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEnglishReportDetail) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditEnglishReport Err:", err.Error()) go alarm_msg.SendAlarmMsg("新增编辑es英文报告 EsAddOrEditEnglishReport,Err:"+err.Error(), 3) } }() client, err := NewClient() if err != nil { return } // docId为报告ID searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background()) if err != nil { if strings.Contains(err.Error(), "404") { err = nil } else { fmt.Println("Get Err" + err.Error()) return } } if searchById != nil && searchById.Found { resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{ "Id": item.Id, "ReportId": item.ReportId, "VideoId": item.VideoId, "Title": item.Title, "Abstract": item.Abstract, "BodyContent": item.BodyContent, "PublishTime": item.PublishTime, "PublishState": item.PublishState, "Author": item.Author, "ClassifyIdFirst": item.ClassifyIdFirst, "ClassifyNameFirst": item.ClassifyNameFirst, "ClassifyIdSecond": item.ClassifyIdSecond, "ClassifyNameSecond": item.ClassifyNameSecond, "CreateTime": item.CreateTime, "Overview": item.Overview, "ReportCode": item.ReportCode, "Frequency": item.Frequency, "StageStr": item.StageStr, "ContentSub": item.ContentSub, }).Do(context.Background()) if e != nil { err = e return } //fmt.Println(resp.Status, resp.Result) if resp.Status == 0 { fmt.Println("修改成功" + docId) err = nil } else { fmt.Println("EditData", resp.Status, resp.Result) } } else { resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if e != nil { err = e fmt.Println("新增失败:", err.Error()) return } if resp.Status == 0 && resp.Result == "created" { fmt.Println("新增成功" + docId) return } else { fmt.Println("AddData", resp.Status, resp.Result) } } return } // EsAddOrEditSmartReport 新增编辑es智能研报 func EsAddOrEditSmartReport(indexName, docId string, item *models.ElasticSmartReport) (err error) { defer func() { if err != nil { fmt.Println("EsAddOrEditSmartReport Err:", err.Error()) } }() client, err := NewClient() if err != nil { return } // docId为报告ID searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background()) if err != nil && !strings.Contains(err.Error(), "404") { fmt.Println("Get Err" + err.Error()) return } if searchById != nil && searchById.Found { resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{ "SmartReportId": item.SmartReportId, "Title": item.Title, "Abstract": item.Abstract, "BodyContent": item.BodyContent, "PublishTime": item.PublishTime, "PublishState": item.PublishState, "Author": item.Author, "ClassifyIdFirst": item.ClassifyIdFirst, "ClassifyNameFirst": item.ClassifyNameFirst, "ClassifyIdSecond": item.ClassifyIdSecond, "ClassifyNameSecond": item.ClassifyNameSecond, "StageStr": item.StageStr, "Frequency": item.Frequency, }).Do(context.Background()) if err != nil { return err } //fmt.Println(resp.Status, resp.Result) if resp.Status == 0 { fmt.Println("修改成功" + docId) err = nil } else { fmt.Println("EditData", resp.Status, resp.Result) } } else { resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background()) if err != nil { fmt.Println("新增失败:", err.Error()) return err } if resp.Status == 0 && resp.Result == "created" { fmt.Println("新增成功" + docId) return nil } else { fmt.Println("AddData", resp.Status, resp.Result) } } return }