elastic.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_task/models"
  5. "eta/eta_task/services/alarm_msg"
  6. "eta/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. }
  148. // EsAddOrEditSmartReport 新增编辑es智能研报
  149. func EsAddOrEditSmartReport(indexName, docId string, item *models.ElasticSmartReport) (err error) {
  150. defer func() {
  151. if err != nil {
  152. fmt.Println("EsAddOrEditSmartReport Err:", err.Error())
  153. }
  154. }()
  155. client, err := NewClient()
  156. if err != nil {
  157. return
  158. }
  159. // docId为报告ID
  160. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  161. if err != nil && !strings.Contains(err.Error(), "404") {
  162. fmt.Println("Get Err" + err.Error())
  163. return
  164. }
  165. if searchById != nil && searchById.Found {
  166. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  167. "SmartReportId": item.SmartReportId,
  168. "Title": item.Title,
  169. "Abstract": item.Abstract,
  170. "BodyContent": item.BodyContent,
  171. "PublishTime": item.PublishTime,
  172. "PublishState": item.PublishState,
  173. "Author": item.Author,
  174. "ClassifyIdFirst": item.ClassifyIdFirst,
  175. "ClassifyNameFirst": item.ClassifyNameFirst,
  176. "ClassifyIdSecond": item.ClassifyIdSecond,
  177. "ClassifyNameSecond": item.ClassifyNameSecond,
  178. "StageStr": item.StageStr,
  179. "Frequency": item.Frequency,
  180. }).Do(context.Background())
  181. if err != nil {
  182. return err
  183. }
  184. //fmt.Println(resp.Status, resp.Result)
  185. if resp.Status == 0 {
  186. fmt.Println("修改成功" + docId)
  187. err = nil
  188. } else {
  189. fmt.Println("EditData", resp.Status, resp.Result)
  190. }
  191. } else {
  192. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  193. if err != nil {
  194. fmt.Println("新增失败:", err.Error())
  195. return err
  196. }
  197. if resp.Status == 0 && resp.Result == "created" {
  198. fmt.Println("新增成功" + docId)
  199. return nil
  200. } else {
  201. fmt.Println("AddData", resp.Status, resp.Result)
  202. }
  203. }
  204. return
  205. }