elastic.go 4.0 KB

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