elastic.go 4.1 KB

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