elastic.go 9.2 KB

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