elastic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_mini_crm/models"
  7. "eta/eta_mini_crm/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "github.com/olivere/elastic/v7"
  12. )
  13. // indexName:索引名称
  14. // mappingJson:表结构
  15. func EsCreateIndex(indexName, mappingJson string) (err error) {
  16. client := utils.EsClient
  17. //定义表结构
  18. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  19. if err != nil {
  20. return
  21. }
  22. if !exists {
  23. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  24. //BodyJson(bodyJson).Do(context.Background())
  25. if err != nil {
  26. fmt.Println("CreateIndex Err:" + err.Error())
  27. return err
  28. }
  29. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  30. } else {
  31. fmt.Println(indexName + " 已存在")
  32. }
  33. return
  34. }
  35. // EsDeleteData 删除es中的指标数据
  36. func EsDeleteData(indexName, docId string) (err error) {
  37. defer func() {
  38. if err != nil {
  39. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  40. }
  41. }()
  42. client := utils.EsClient
  43. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  44. fmt.Println(resp)
  45. if err != nil {
  46. return
  47. }
  48. if resp.Status == 0 {
  49. fmt.Println("删除成功")
  50. } else {
  51. fmt.Println("AddData", resp.Status, resp.Result)
  52. }
  53. return
  54. }
  55. // EsAddOrEditReport 新增编辑es报告
  56. func EsAddOrEditReportPdf(indexName, docId string, item *models.ReportPdfView) (err error) {
  57. defer func() {
  58. if err != nil {
  59. fmt.Println("EsAddOrEditReport Err:", err.Error())
  60. }
  61. }()
  62. client := utils.EsClient
  63. // docId为报告ID+章节ID
  64. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  65. if err != nil && !strings.Contains(err.Error(), "404") {
  66. fmt.Println("Get Err" + err.Error())
  67. return
  68. }
  69. if searchById != nil && searchById.Found {
  70. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  71. "ReportPdfId": item.ReportPdfId,
  72. "PdfUrl": item.PdfUrl,
  73. "PdfName": item.PdfName,
  74. "Title": item.Title,
  75. "Abstract": item.Abstract,
  76. "PublishTime": item.PublishTime,
  77. "ModifyTime": item.ModifyTime,
  78. "SysUserId": item.SysUserId,
  79. "SysRealName": item.SysRealName,
  80. "Author": item.Author,
  81. "State": item.State,
  82. "ClassifyIdFirst": item.ClassifyIdFirst,
  83. "ClassifyNameFirst": item.ClassifyNameFirst,
  84. "ClassifyIdSecond": item.ClassifyIdSecond,
  85. "ClassifyNameSecond": item.ClassifyNameSecond,
  86. "ClassifyIdThird": item.ClassifyIdThird,
  87. "ClassifyNameThird": item.ClassifyNameThird,
  88. "Stage": item.Stage,
  89. }).Do(context.Background())
  90. if err != nil {
  91. return err
  92. }
  93. //fmt.Println(resp.Status, resp.Result)
  94. if resp.Status == 0 {
  95. fmt.Println("修改成功" + docId)
  96. err = nil
  97. } else {
  98. fmt.Println("EditData", resp.Status, resp.Result)
  99. }
  100. } else {
  101. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  102. if err != nil {
  103. fmt.Println("新增失败:", err.Error())
  104. return err
  105. }
  106. if resp.Status == 0 && resp.Result == "created" {
  107. fmt.Println("新增成功" + docId)
  108. return nil
  109. } else {
  110. fmt.Println("AddData", resp.Status, resp.Result)
  111. }
  112. }
  113. return
  114. }
  115. // AnalyzeResp 分词接口返回结构体
  116. type AnalyzeResp struct {
  117. Tokens []struct {
  118. EndOffset int64 `json:"end_offset"`
  119. Position int64 `json:"position"`
  120. StartOffset int64 `json:"start_offset"`
  121. Token string `json:"token"`
  122. Type string `json:"type"`
  123. } `json:"tokens"`
  124. }
  125. // Analyze 根据输入的文字获取分词后的文字
  126. func Analyze(content string) (contentList []string, err error) {
  127. defer func() {
  128. if err != nil {
  129. fmt.Println("Analyze Err:", err.Error())
  130. }
  131. }()
  132. client := utils.EsClient
  133. queryMap := map[string]string{
  134. "text": content,
  135. "analyzer": "ik_max_word",
  136. }
  137. res, err := client.PerformRequest(
  138. context.Background(),
  139. elastic.PerformRequestOptions{
  140. Method: "GET",
  141. Path: "/_analyze",
  142. Body: queryMap,
  143. Stream: false,
  144. },
  145. )
  146. if res.StatusCode == 200 {
  147. var analyzeResp AnalyzeResp
  148. tmpErr := json.Unmarshal(res.Body, &analyzeResp)
  149. if tmpErr != nil {
  150. err = errors.New("返回数据转结构体失败:" + tmpErr.Error())
  151. return
  152. }
  153. for _, v := range analyzeResp.Tokens {
  154. contentList = append(contentList, v.Token)
  155. }
  156. } else {
  157. err = errors.New("分词失败,返回code异常:" + strconv.Itoa(res.StatusCode))
  158. }
  159. return
  160. }