es.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package knowledge
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta_gn/eta_report/models"
  6. "eta_gn/eta_report/utils"
  7. "fmt"
  8. "html"
  9. "strconv"
  10. "strings"
  11. "github.com/PuerkitoBio/goquery"
  12. )
  13. // EsAddOrEditKnowledgeResource 新增/修改es中的知识资源数据
  14. func EsAddOrEditKnowledgeResource(item *models.KnowledgeResource) (err error) {
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("EsAddOrEditData Err:", err.Error())
  18. utils.FileLog.Info("EsAddOrEditKnowledgeResource err:", err)
  19. }
  20. }()
  21. indexName := utils.EsKnowledgeResourceIndexName
  22. client := utils.EsClient
  23. if item.IsFile == 0 {
  24. content := ExtractTextFromResourceContent(item.Content)
  25. contentRunes := []rune(content)
  26. if len(contentRunes) > 60 {
  27. item.Content = string(contentRunes[:60])
  28. } else {
  29. item.Content = content
  30. }
  31. }
  32. request := client.Index().Index(indexName).Id(strconv.Itoa(item.KnowledgeResourceId)).BodyJson(item)
  33. response, err := request.Do(context.Background())
  34. if err != nil {
  35. jsonBytes, _ := json.Marshal(item)
  36. utils.FileLog.Info("add json:%s,EsAddOrEditKnowledgeResource err:%s", string(jsonBytes), err.Error())
  37. return
  38. }
  39. if response.Status == 0 {
  40. err = nil
  41. } else {
  42. fmt.Println("EsAddOrEditKnowledgeResource:", response.Status, response.Result)
  43. }
  44. return
  45. }
  46. func ExtractTextFromResourceContent(content string) (text string) {
  47. content = html.UnescapeString(content)
  48. doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  49. if err != nil {
  50. return
  51. }
  52. text = doc.Text()
  53. text = strings.ReplaceAll(text, "\n", "")
  54. return
  55. }
  56. func UpdateEsKnowledgeResource(docId int, docFields map[string]interface{}) (err error) {
  57. defer func() {
  58. if err != nil {
  59. fmt.Println("EsAddOrEditData Err:", err.Error())
  60. utils.FileLog.Info("EsAddOrEditKnowledgeResource err:", err)
  61. }
  62. }()
  63. indexName := utils.EsKnowledgeResourceIndexName
  64. client := utils.EsClient
  65. res, err := client.Update().
  66. Index(indexName).
  67. Id(strconv.Itoa(docId)).
  68. Doc(docFields).
  69. Do(context.Background())
  70. if err != nil {
  71. utils.FileLog.Info("update docId:%d,UpdateEsKnowledgeResource err:%s", docId, err.Error())
  72. return
  73. }
  74. if res.Result == "updated" {
  75. err = nil
  76. } else {
  77. fmt.Println("UpdateEsKnowledgeResource:", res.Result)
  78. }
  79. return
  80. }