123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- package services
- import (
- "context"
- "eta/eta_api/models"
- saModel "eta/eta_api/models/semantic_analysis"
- "eta/eta_api/models/smart_report"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/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
- }
- // indexName:索引名称
- // mappingJson:表结构
- func EsCreateIndex(indexName, mappingJson string) (err error) {
- client, err := NewClient()
- if err != nil {
- return
- }
- //定义表结构
- 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
- }
- // 删除数据
- func EsDeleteData(indexName, docId string) (err error) {
- client, err := NewClient()
- if err != nil {
- return
- }
- resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
- fmt.Println(resp)
- if err != nil {
- return
- }
- if resp.Status == 0 {
- fmt.Println("删除成功")
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- func MappingModify(indexName, mappingJson string) {
- client, err := NewClient()
- if err != nil {
- return
- }
- result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
- fmt.Println(err)
- fmt.Println(result)
- 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,
- "IsPublicPublish": item.IsPublicPublish,
- "Author": item.Author,
- "ClassifyIdFirst": item.ClassifyIdFirst,
- "ClassifyNameFirst": item.ClassifyNameFirst,
- "ClassifyIdSecond": item.ClassifyIdSecond,
- "ClassifyNameSecond": item.ClassifyNameSecond,
- "ClassifyId": item.ClassifyId,
- "ClassifyName": item.ClassifyName,
- "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
- }
- // EsAddOrEditSaDoc 新增编辑语义分析文档
- func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) {
- defer func() {
- if err != nil {
- fmt.Println("EsAddOrEditSaDoc Err:", err.Error())
- }
- }()
- client, e := NewClient()
- if e != nil {
- err = e
- return
- }
- // docId为语义分析文档ID+段落ID
- searchById, e := client.Get().Index(indexName).Id(docId).Do(context.Background())
- if e != nil && !strings.Contains(e.Error(), "404") {
- err = fmt.Errorf("query sa doc err: %s", e.Error())
- return
- }
- // 更新
- if searchById != nil && searchById.Found {
- docMap := map[string]interface{}{
- "SaDocId": item.SaDocId,
- "SaDocSectionId": item.SaDocSectionId,
- "ClassifyId": item.ClassifyId,
- "ClassifyName": item.ClassifyName,
- "Title": item.Title,
- "Theme": item.Theme,
- "BodyContent": item.BodyContent,
- "Author": item.Author,
- "CoverImg": item.CoverImg,
- "CreateTime": item.CreateTime,
- }
- resp, e := client.Update().Index(indexName).Id(docId).Doc(docMap).Do(context.Background())
- if e != nil {
- err = fmt.Errorf("update sa doc err: %s", e.Error())
- return
- }
- if resp.Status == 0 {
- return
- }
- fmt.Println("EditData", resp.Status, resp.Result)
- return
- }
- // 新增
- resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
- if e != nil {
- err = fmt.Errorf("insert sa doc err: %s", e.Error())
- return
- }
- if resp.Status == 0 && resp.Result == "created" {
- return
- }
- fmt.Println("AddData", resp.Status, resp.Result)
- return
- }
- // EsAddOrEditSmartReport 新增编辑es智能研报
- func EsAddOrEditSmartReport(indexName, docId string, item *smart_report.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
- }
|