elastic.go 9.4 KB

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