elastic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package services
  2. import (
  3. "context"
  4. "eta_gn/eta_task/models"
  5. "eta_gn/eta_task/services/alarm_msg"
  6. "eta_gn/eta_task/utils"
  7. "fmt"
  8. "github.com/olivere/elastic/v7"
  9. "strings"
  10. )
  11. func NewClient() (client *elastic.Client, err error) {
  12. client, err = elastic.NewClient(
  13. elastic.SetURL(utils.ES_URL),
  14. elastic.SetBasicAuth(utils.ES_USERNAME, utils.ES_PASSWORD),
  15. elastic.SetSniff(false))
  16. return
  17. }
  18. // EsAddOrEditReport 新增编辑es报告
  19. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  20. defer func() {
  21. if err != nil {
  22. fmt.Println("EsAddOrEditReport Err:", err.Error())
  23. }
  24. }()
  25. client, err := NewClient()
  26. if err != nil {
  27. return
  28. }
  29. // docId为报告ID+章节ID
  30. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  31. if err != nil && !strings.Contains(err.Error(), "404") {
  32. fmt.Println("Get Err" + err.Error())
  33. return
  34. }
  35. if searchById != nil && searchById.Found {
  36. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  37. "ReportId": item.ReportId,
  38. "ReportChapterId": item.ReportChapterId,
  39. "Title": item.Title,
  40. "Abstract": item.Abstract,
  41. "BodyContent": item.BodyContent,
  42. "PublishTime": item.PublishTime,
  43. "PublishState": item.PublishState,
  44. "Author": item.Author,
  45. "ClassifyIdFirst": item.ClassifyIdFirst,
  46. "ClassifyNameFirst": item.ClassifyNameFirst,
  47. "ClassifyIdSecond": item.ClassifyIdSecond,
  48. "ClassifyNameSecond": item.ClassifyNameSecond,
  49. "Categories": item.Categories,
  50. "StageStr": item.StageStr,
  51. }).Do(context.Background())
  52. if err != nil {
  53. return err
  54. }
  55. //fmt.Println(resp.Status, resp.Result)
  56. if resp.Status == 0 {
  57. fmt.Println("修改成功" + docId)
  58. err = nil
  59. } else {
  60. fmt.Println("EditData", resp.Status, resp.Result)
  61. }
  62. } else {
  63. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  64. if err != nil {
  65. fmt.Println("新增失败:", err.Error())
  66. return err
  67. }
  68. if resp.Status == 0 && resp.Result == "created" {
  69. fmt.Println("新增成功" + docId)
  70. return nil
  71. } else {
  72. fmt.Println("AddData", resp.Status, resp.Result)
  73. }
  74. }
  75. return
  76. }
  77. // EsAddOrEditEnglishReport 新增编辑es英文报告
  78. func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEnglishReportDetail) (err error) {
  79. defer func() {
  80. if err != nil {
  81. fmt.Println("EsAddOrEditEnglishReport Err:", err.Error())
  82. go alarm_msg.SendAlarmMsg("新增编辑es英文报告 EsAddOrEditEnglishReport,Err:"+err.Error(), 3)
  83. }
  84. }()
  85. client, err := NewClient()
  86. if err != nil {
  87. return
  88. }
  89. // docId为报告ID
  90. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  91. if err != nil {
  92. if strings.Contains(err.Error(), "404") {
  93. err = nil
  94. } else {
  95. fmt.Println("Get Err" + err.Error())
  96. return
  97. }
  98. }
  99. if searchById != nil && searchById.Found {
  100. resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  101. "Id": item.Id,
  102. "ReportId": item.ReportId,
  103. "VideoId": item.VideoId,
  104. "Title": item.Title,
  105. "Abstract": item.Abstract,
  106. "BodyContent": item.BodyContent,
  107. "PublishTime": item.PublishTime,
  108. "PublishState": item.PublishState,
  109. "Author": item.Author,
  110. "ClassifyIdFirst": item.ClassifyIdFirst,
  111. "ClassifyNameFirst": item.ClassifyNameFirst,
  112. "ClassifyIdSecond": item.ClassifyIdSecond,
  113. "ClassifyNameSecond": item.ClassifyNameSecond,
  114. "CreateTime": item.CreateTime,
  115. "Overview": item.Overview,
  116. "ReportCode": item.ReportCode,
  117. "Frequency": item.Frequency,
  118. "StageStr": item.StageStr,
  119. "ContentSub": item.ContentSub,
  120. }).Do(context.Background())
  121. if e != nil {
  122. err = e
  123. return
  124. }
  125. //fmt.Println(resp.Status, resp.Result)
  126. if resp.Status == 0 {
  127. fmt.Println("修改成功" + docId)
  128. err = nil
  129. } else {
  130. fmt.Println("EditData", resp.Status, resp.Result)
  131. }
  132. } else {
  133. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  134. if e != nil {
  135. err = e
  136. fmt.Println("新增失败:", err.Error())
  137. return
  138. }
  139. if resp.Status == 0 && resp.Result == "created" {
  140. fmt.Println("新增成功" + docId)
  141. return
  142. } else {
  143. fmt.Println("AddData", resp.Status, resp.Result)
  144. }
  145. }
  146. return
  147. }