elastic.go 9.2 KB

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