elastic.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package services
  2. import (
  3. "context"
  4. "eta_gn/eta_api/models"
  5. saModel "eta_gn/eta_api/models/semantic_analysis"
  6. "eta_gn/eta_api/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. // 删除数据
  19. func EsDeleteData(indexName, docId string) (err error) {
  20. client, err := NewClient()
  21. if err != nil {
  22. return
  23. }
  24. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  25. fmt.Println(resp)
  26. if err != nil {
  27. return
  28. }
  29. if resp.Status == 0 {
  30. fmt.Println("删除成功")
  31. } else {
  32. fmt.Println("AddData", resp.Status, resp.Result)
  33. }
  34. return
  35. }
  36. // EsAddOrEditReport 新增编辑es报告
  37. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  38. defer func() {
  39. if err != nil {
  40. fmt.Println("EsAddOrEditReport Err:", err.Error())
  41. }
  42. }()
  43. client, err := NewClient()
  44. if err != nil {
  45. return
  46. }
  47. // docId为报告ID+章节ID
  48. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  49. if err != nil && !strings.Contains(err.Error(), "404") {
  50. fmt.Println("Get Err" + err.Error())
  51. return
  52. }
  53. if searchById != nil && searchById.Found {
  54. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  55. "ReportId": item.ReportId,
  56. "ReportChapterId": item.ReportChapterId,
  57. "Title": item.Title,
  58. "Abstract": item.Abstract,
  59. "BodyContent": item.BodyContent,
  60. "PublishTime": item.PublishTime,
  61. "PublishState": item.PublishState,
  62. "Author": item.Author,
  63. "ClassifyIdFirst": item.ClassifyIdFirst,
  64. "ClassifyNameFirst": item.ClassifyNameFirst,
  65. "ClassifyIdSecond": item.ClassifyIdSecond,
  66. "ClassifyNameSecond": item.ClassifyNameSecond,
  67. "ClassifyId": item.ClassifyId,
  68. "ClassifyName": item.ClassifyName,
  69. "Categories": item.Categories,
  70. "StageStr": item.StageStr,
  71. }).Do(context.Background())
  72. if err != nil {
  73. return err
  74. }
  75. //fmt.Println(resp.Status, resp.Result)
  76. if resp.Status == 0 {
  77. fmt.Println("修改成功" + docId)
  78. err = nil
  79. } else {
  80. fmt.Println("EditData", resp.Status, resp.Result)
  81. }
  82. } else {
  83. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  84. if err != nil {
  85. fmt.Println("新增失败:", err.Error())
  86. return err
  87. }
  88. if resp.Status == 0 && resp.Result == "created" {
  89. fmt.Println("新增成功" + docId)
  90. return nil
  91. } else {
  92. fmt.Println("AddData", resp.Status, resp.Result)
  93. }
  94. }
  95. return
  96. }
  97. // EsAddOrEditSaDoc 新增编辑语义分析文档
  98. func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) {
  99. defer func() {
  100. if err != nil {
  101. fmt.Println("EsAddOrEditSaDoc Err:", err.Error())
  102. }
  103. }()
  104. client, e := NewClient()
  105. if e != nil {
  106. err = e
  107. return
  108. }
  109. // docId为语义分析文档ID+段落ID
  110. searchById, e := client.Get().Index(indexName).Id(docId).Do(context.Background())
  111. if e != nil && !strings.Contains(e.Error(), "404") {
  112. err = fmt.Errorf("query sa doc err: %s", e.Error())
  113. return
  114. }
  115. // 更新
  116. if searchById != nil && searchById.Found {
  117. docMap := map[string]interface{}{
  118. "SaDocId": item.SaDocId,
  119. "SaDocSectionId": item.SaDocSectionId,
  120. "ClassifyId": item.ClassifyId,
  121. "ClassifyName": item.ClassifyName,
  122. "Title": item.Title,
  123. "Theme": item.Theme,
  124. "BodyContent": item.BodyContent,
  125. "Author": item.Author,
  126. "CoverImg": item.CoverImg,
  127. "CreateTime": item.CreateTime,
  128. }
  129. resp, e := client.Update().Index(indexName).Id(docId).Doc(docMap).Do(context.Background())
  130. if e != nil {
  131. err = fmt.Errorf("update sa doc err: %s", e.Error())
  132. return
  133. }
  134. if resp.Status == 0 {
  135. return
  136. }
  137. fmt.Println("EditData", resp.Status, resp.Result)
  138. return
  139. }
  140. // 新增
  141. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  142. if e != nil {
  143. err = fmt.Errorf("insert sa doc err: %s", e.Error())
  144. return
  145. }
  146. if resp.Status == 0 && resp.Result == "created" {
  147. return
  148. }
  149. fmt.Println("AddData", resp.Status, resp.Result)
  150. return
  151. }