elastic.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/olivere/elastic/v7"
  6. "hongze/hz_eta_api/models"
  7. saModel "hongze/hz_eta_api/models/semantic_analysis"
  8. "hongze/hz_eta_api/services/alarm_msg"
  9. "hongze/hz_eta_api/utils"
  10. "strings"
  11. )
  12. func NewClient() (client *elastic.Client, err error) {
  13. client, err = elastic.NewClient(
  14. elastic.SetURL(utils.ES_URL),
  15. elastic.SetBasicAuth(utils.ES_USERNAME, utils.ES_PASSWORD),
  16. elastic.SetSniff(false))
  17. return
  18. }
  19. // indexName:索引名称
  20. // mappingJson:表结构
  21. func EsCreateIndex(indexName, mappingJson string) (err error) {
  22. client, err := NewClient()
  23. if err != nil {
  24. return
  25. }
  26. //定义表结构
  27. exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
  28. if err != nil {
  29. return
  30. }
  31. if !exists {
  32. resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
  33. //BodyJson(bodyJson).Do(context.Background())
  34. if err != nil {
  35. fmt.Println("CreateIndex Err:" + err.Error())
  36. return err
  37. }
  38. fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
  39. } else {
  40. fmt.Println(indexName + " 已存在")
  41. }
  42. return
  43. }
  44. // 删除数据
  45. func EsDeleteData(indexName, docId string) (err error) {
  46. client, err := NewClient()
  47. if err != nil {
  48. return
  49. }
  50. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  51. fmt.Println(resp)
  52. if err != nil {
  53. return
  54. }
  55. if resp.Status == 0 {
  56. fmt.Println("删除成功")
  57. } else {
  58. fmt.Println("AddData", resp.Status, resp.Result)
  59. }
  60. return
  61. }
  62. func MappingModify(indexName, mappingJson string) {
  63. client, err := NewClient()
  64. if err != nil {
  65. return
  66. }
  67. result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
  68. fmt.Println(err)
  69. fmt.Println(result)
  70. return
  71. }
  72. // EsAddOrEditReport 新增编辑es报告
  73. func EsAddOrEditReport(indexName, docId string, item *models.ElasticReportDetail) (err error) {
  74. defer func() {
  75. if err != nil {
  76. fmt.Println("EsAddOrEditReport Err:", err.Error())
  77. }
  78. }()
  79. client, err := NewClient()
  80. if err != nil {
  81. return
  82. }
  83. // docId为报告ID+章节ID
  84. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  85. if err != nil && !strings.Contains(err.Error(), "404") {
  86. fmt.Println("Get Err" + err.Error())
  87. return
  88. }
  89. if searchById != nil && searchById.Found {
  90. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  91. "ReportId": item.ReportId,
  92. "ReportChapterId": item.ReportChapterId,
  93. "Title": item.Title,
  94. "Abstract": item.Abstract,
  95. "BodyContent": item.BodyContent,
  96. "PublishTime": item.PublishTime,
  97. "PublishState": item.PublishState,
  98. "Author": item.Author,
  99. "ClassifyIdFirst": item.ClassifyIdFirst,
  100. "ClassifyNameFirst": item.ClassifyNameFirst,
  101. "ClassifyIdSecond": item.ClassifyIdSecond,
  102. "ClassifyNameSecond": item.ClassifyNameSecond,
  103. "Categories": item.Categories,
  104. "StageStr": item.StageStr,
  105. }).Do(context.Background())
  106. if err != nil {
  107. return err
  108. }
  109. //fmt.Println(resp.Status, resp.Result)
  110. if resp.Status == 0 {
  111. fmt.Println("修改成功" + docId)
  112. err = nil
  113. } else {
  114. fmt.Println("EditData", resp.Status, resp.Result)
  115. }
  116. } else {
  117. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  118. if err != nil {
  119. fmt.Println("新增失败:", err.Error())
  120. return err
  121. }
  122. if resp.Status == 0 && resp.Result == "created" {
  123. fmt.Println("新增成功" + docId)
  124. return nil
  125. } else {
  126. fmt.Println("AddData", resp.Status, resp.Result)
  127. }
  128. }
  129. return
  130. }
  131. // EsAddOrEditEnglishReport 新增编辑es英文报告
  132. func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEnglishReportDetail) (err error) {
  133. defer func() {
  134. if err != nil {
  135. fmt.Println("EsAddOrEditEnglishReport Err:", err.Error())
  136. go alarm_msg.SendAlarmMsg("新增编辑es英文报告 EsAddOrEditEnglishReport,Err:"+err.Error(), 3)
  137. }
  138. }()
  139. client, err := NewClient()
  140. if err != nil {
  141. return
  142. }
  143. // docId为报告ID
  144. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  145. if err != nil {
  146. if strings.Contains(err.Error(), "404") {
  147. err = nil
  148. } else {
  149. fmt.Println("Get Err" + err.Error())
  150. return
  151. }
  152. }
  153. if searchById != nil && searchById.Found {
  154. resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  155. "Id": item.Id,
  156. "ReportId": item.ReportId,
  157. "VideoId": item.VideoId,
  158. "Title": item.Title,
  159. "Abstract": item.Abstract,
  160. "BodyContent": item.BodyContent,
  161. "PublishTime": item.PublishTime,
  162. "PublishState": item.PublishState,
  163. "Author": item.Author,
  164. "ClassifyIdFirst": item.ClassifyIdFirst,
  165. "ClassifyNameFirst": item.ClassifyNameFirst,
  166. "ClassifyIdSecond": item.ClassifyIdSecond,
  167. "ClassifyNameSecond": item.ClassifyNameSecond,
  168. "CreateTime": item.CreateTime,
  169. "Overview": item.Overview,
  170. "ReportCode": item.ReportCode,
  171. "Frequency": item.Frequency,
  172. "StageStr": item.StageStr,
  173. "ContentSub": item.ContentSub,
  174. }).Do(context.Background())
  175. if e != nil {
  176. err = e
  177. return
  178. }
  179. //fmt.Println(resp.Status, resp.Result)
  180. if resp.Status == 0 {
  181. fmt.Println("修改成功" + docId)
  182. err = nil
  183. } else {
  184. fmt.Println("EditData", resp.Status, resp.Result)
  185. }
  186. } else {
  187. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  188. if e != nil {
  189. err = e
  190. fmt.Println("新增失败:", err.Error())
  191. return
  192. }
  193. if resp.Status == 0 && resp.Result == "created" {
  194. fmt.Println("新增成功" + docId)
  195. return
  196. } else {
  197. fmt.Println("AddData", resp.Status, resp.Result)
  198. }
  199. }
  200. return
  201. }
  202. // EsAddOrEditSaDoc 新增编辑语义分析文档
  203. func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) {
  204. defer func() {
  205. if err != nil {
  206. fmt.Println("EsAddOrEditSaDoc Err:", err.Error())
  207. }
  208. }()
  209. client, e := NewClient()
  210. if e != nil {
  211. err = e
  212. return
  213. }
  214. // docId为语义分析文档ID+段落ID
  215. searchById, e := client.Get().Index(indexName).Id(docId).Do(context.Background())
  216. if e != nil && !strings.Contains(e.Error(), "404") {
  217. err = fmt.Errorf("query sa doc err: %s", e.Error())
  218. return
  219. }
  220. // 更新
  221. if searchById != nil && searchById.Found {
  222. docMap := map[string]interface{}{
  223. "SaDocId": item.SaDocId,
  224. "SaDocSectionId": item.SaDocSectionId,
  225. "ClassifyId": item.ClassifyId,
  226. "ClassifyName": item.ClassifyName,
  227. "Title": item.Title,
  228. "Theme": item.Theme,
  229. "BodyContent": item.BodyContent,
  230. "Author": item.Author,
  231. "CoverImg": item.CoverImg,
  232. "CreateTime": item.CreateTime,
  233. }
  234. resp, e := client.Update().Index(indexName).Id(docId).Doc(docMap).Do(context.Background())
  235. if e != nil {
  236. err = fmt.Errorf("update sa doc err: %s", e.Error())
  237. return
  238. }
  239. if resp.Status == 0 {
  240. return
  241. }
  242. fmt.Println("EditData", resp.Status, resp.Result)
  243. return
  244. }
  245. // 新增
  246. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  247. if e != nil {
  248. err = fmt.Errorf("insert sa doc err: %s", e.Error())
  249. return
  250. }
  251. if resp.Status == 0 && resp.Result == "created" {
  252. return
  253. }
  254. fmt.Println("AddData", resp.Status, resp.Result)
  255. return
  256. }