elastic.go 4.0 KB

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