elastic.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_api/models"
  5. saModel "eta/eta_api/models/semantic_analysis"
  6. "eta/eta_api/models/smart_report"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/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. "IsPublicPublish": item.IsPublicPublish,
  100. "Author": item.Author,
  101. "ClassifyIdFirst": item.ClassifyIdFirst,
  102. "ClassifyNameFirst": item.ClassifyNameFirst,
  103. "ClassifyIdSecond": item.ClassifyIdSecond,
  104. "ClassifyNameSecond": item.ClassifyNameSecond,
  105. "ClassifyId": item.ClassifyId,
  106. "ClassifyName": item.ClassifyName,
  107. "Categories": item.Categories,
  108. "StageStr": item.StageStr,
  109. }).Do(context.Background())
  110. if err != nil {
  111. return err
  112. }
  113. //fmt.Println(resp.Status, resp.Result)
  114. if resp.Status == 0 {
  115. fmt.Println("修改成功" + docId)
  116. err = nil
  117. } else {
  118. fmt.Println("EditData", resp.Status, resp.Result)
  119. }
  120. } else {
  121. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  122. if err != nil {
  123. fmt.Println("新增失败:", err.Error())
  124. return err
  125. }
  126. if resp.Status == 0 && resp.Result == "created" {
  127. fmt.Println("新增成功" + docId)
  128. return nil
  129. } else {
  130. fmt.Println("AddData", resp.Status, resp.Result)
  131. }
  132. }
  133. return
  134. }
  135. // EsAddOrEditEnglishReport 新增编辑es英文报告
  136. func EsAddOrEditEnglishReport(indexName, docId string, item *models.ElasticEnglishReportDetail) (err error) {
  137. defer func() {
  138. if err != nil {
  139. fmt.Println("EsAddOrEditEnglishReport Err:", err.Error())
  140. go alarm_msg.SendAlarmMsg("新增编辑es英文报告 EsAddOrEditEnglishReport,Err:"+err.Error(), 3)
  141. }
  142. }()
  143. client, err := NewClient()
  144. if err != nil {
  145. return
  146. }
  147. // docId为报告ID
  148. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  149. if err != nil {
  150. if strings.Contains(err.Error(), "404") {
  151. err = nil
  152. } else {
  153. fmt.Println("Get Err" + err.Error())
  154. return
  155. }
  156. }
  157. if searchById != nil && searchById.Found {
  158. resp, e := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  159. "Id": item.Id,
  160. "ReportId": item.ReportId,
  161. "VideoId": item.VideoId,
  162. "Title": item.Title,
  163. "Abstract": item.Abstract,
  164. "BodyContent": item.BodyContent,
  165. "PublishTime": item.PublishTime,
  166. "PublishState": item.PublishState,
  167. "Author": item.Author,
  168. "ClassifyIdFirst": item.ClassifyIdFirst,
  169. "ClassifyNameFirst": item.ClassifyNameFirst,
  170. "ClassifyIdSecond": item.ClassifyIdSecond,
  171. "ClassifyNameSecond": item.ClassifyNameSecond,
  172. "CreateTime": item.CreateTime,
  173. "Overview": item.Overview,
  174. "ReportCode": item.ReportCode,
  175. "Frequency": item.Frequency,
  176. "StageStr": item.StageStr,
  177. "ContentSub": item.ContentSub,
  178. }).Do(context.Background())
  179. if e != nil {
  180. err = e
  181. return
  182. }
  183. //fmt.Println(resp.Status, resp.Result)
  184. if resp.Status == 0 {
  185. fmt.Println("修改成功" + docId)
  186. err = nil
  187. } else {
  188. fmt.Println("EditData", resp.Status, resp.Result)
  189. }
  190. } else {
  191. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  192. if e != nil {
  193. err = e
  194. fmt.Println("新增失败:", err.Error())
  195. return
  196. }
  197. if resp.Status == 0 && resp.Result == "created" {
  198. fmt.Println("新增成功" + docId)
  199. return
  200. } else {
  201. fmt.Println("AddData", resp.Status, resp.Result)
  202. }
  203. }
  204. return
  205. }
  206. // EsAddOrEditSaDoc 新增编辑语义分析文档
  207. func EsAddOrEditSaDoc(indexName, docId string, item *saModel.ElasticSaDoc) (err error) {
  208. defer func() {
  209. if err != nil {
  210. fmt.Println("EsAddOrEditSaDoc Err:", err.Error())
  211. }
  212. }()
  213. client, e := NewClient()
  214. if e != nil {
  215. err = e
  216. return
  217. }
  218. // docId为语义分析文档ID+段落ID
  219. searchById, e := client.Get().Index(indexName).Id(docId).Do(context.Background())
  220. if e != nil && !strings.Contains(e.Error(), "404") {
  221. err = fmt.Errorf("query sa doc err: %s", e.Error())
  222. return
  223. }
  224. // 更新
  225. if searchById != nil && searchById.Found {
  226. docMap := map[string]interface{}{
  227. "SaDocId": item.SaDocId,
  228. "SaDocSectionId": item.SaDocSectionId,
  229. "ClassifyId": item.ClassifyId,
  230. "ClassifyName": item.ClassifyName,
  231. "Title": item.Title,
  232. "Theme": item.Theme,
  233. "BodyContent": item.BodyContent,
  234. "Author": item.Author,
  235. "CoverImg": item.CoverImg,
  236. "CreateTime": item.CreateTime,
  237. }
  238. resp, e := client.Update().Index(indexName).Id(docId).Doc(docMap).Do(context.Background())
  239. if e != nil {
  240. err = fmt.Errorf("update sa doc err: %s", e.Error())
  241. return
  242. }
  243. if resp.Status == 0 {
  244. return
  245. }
  246. fmt.Println("EditData", resp.Status, resp.Result)
  247. return
  248. }
  249. // 新增
  250. resp, e := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  251. if e != nil {
  252. err = fmt.Errorf("insert sa doc err: %s", e.Error())
  253. return
  254. }
  255. if resp.Status == 0 && resp.Result == "created" {
  256. return
  257. }
  258. fmt.Println("AddData", resp.Status, resp.Result)
  259. return
  260. }
  261. // EsAddOrEditSmartReport 新增编辑es智能研报
  262. func EsAddOrEditSmartReport(indexName, docId string, item *smart_report.ElasticSmartReport) (err error) {
  263. defer func() {
  264. if err != nil {
  265. fmt.Println("EsAddOrEditSmartReport Err:", err.Error())
  266. }
  267. }()
  268. client, err := NewClient()
  269. if err != nil {
  270. return
  271. }
  272. // docId为报告ID
  273. searchById, err := client.Get().Index(indexName).Id(docId).Do(context.Background())
  274. if err != nil && !strings.Contains(err.Error(), "404") {
  275. fmt.Println("Get Err" + err.Error())
  276. return
  277. }
  278. if searchById != nil && searchById.Found {
  279. resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
  280. "SmartReportId": item.SmartReportId,
  281. "Title": item.Title,
  282. "Abstract": item.Abstract,
  283. "BodyContent": item.BodyContent,
  284. "PublishTime": item.PublishTime,
  285. "PublishState": item.PublishState,
  286. "Author": item.Author,
  287. "ClassifyIdFirst": item.ClassifyIdFirst,
  288. "ClassifyNameFirst": item.ClassifyNameFirst,
  289. "ClassifyIdSecond": item.ClassifyIdSecond,
  290. "ClassifyNameSecond": item.ClassifyNameSecond,
  291. "StageStr": item.StageStr,
  292. "Frequency": item.Frequency,
  293. }).Do(context.Background())
  294. if err != nil {
  295. return err
  296. }
  297. //fmt.Println(resp.Status, resp.Result)
  298. if resp.Status == 0 {
  299. fmt.Println("修改成功" + docId)
  300. err = nil
  301. } else {
  302. fmt.Println("EditData", resp.Status, resp.Result)
  303. }
  304. } else {
  305. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  306. if err != nil {
  307. fmt.Println("新增失败:", err.Error())
  308. return err
  309. }
  310. if resp.Status == 0 && resp.Result == "created" {
  311. fmt.Println("新增成功" + docId)
  312. return nil
  313. } else {
  314. fmt.Println("AddData", resp.Status, resp.Result)
  315. }
  316. }
  317. return
  318. }