elastic.go 9.1 KB

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