elastic.go 4.6 KB

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