chart_service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package media
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_ht_api/common/component/es"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "strconv"
  7. )
  8. const (
  9. ChartESColumn = "chartName"
  10. ChartESRangeColumn = "chartInfoId"
  11. ChartEsColumn = "chartName"
  12. )
  13. // func SearchMediaList(_ string, key string, mediaIds []int, from int, size int, max int64) (medias []MediaDTO, err error) {
  14. // //同步es
  15. // var docIds []string
  16. // for _, id := range mediaIds {
  17. // docIds = append(docIds, strconv.Itoa(id))
  18. // }
  19. // sorts := append(sortField, "publishedTime:desc")
  20. // request := matchRangeWithDocIds(key, from, size, max, sorts, docIds)
  21. // re, err := elastic().Search(request)
  22. // if err != nil {
  23. // logger.Error("es搜索失败:%v", err)
  24. // }
  25. // hits := elastic().GetSource(re.Hits)
  26. // for _, hit := range hits {
  27. // var content map[string][]string
  28. // err = json.Unmarshal(hit.Highlight, &content)
  29. // media := MediaDTO{}
  30. // err = json.Unmarshal(hit.Source, &media)
  31. // if err != nil {
  32. // logger.Error("解析媒体数据失败:%v", err)
  33. // continue
  34. // }
  35. // media.Highlight = content[ESColumn]
  36. // media.PublishedTime = media.PublishedTime[:10]
  37. // media.MediaTitle = media.Highlight[0]
  38. // medias = append(medias, media)
  39. // }
  40. // return
  41. // }
  42. type EsChartInfo struct {
  43. ChartInfoId int `json:"chartInfoId"`
  44. ChartName string `json:"chartName"`
  45. ChartImage string `json:"chartImage"`
  46. UniqueCode string `json:"uniqueCode"`
  47. }
  48. // 同步es
  49. func AddChartToEs(esChart EsChartInfo) bool {
  50. return elastic().InsertOrUpdate(htConfig.GetChartIndex(), esChart.ChartInfoId, esChart)
  51. }
  52. func matchRangeWithDocIdsNoLimit(key string, from int, to int, sorts []string, docIds []string) (request *es.ESQueryRequest) {
  53. req := new(es.ESQueryRequest)
  54. return req.CreateESQueryRequest(htConfig.GetChartIndex(), ChartEsColumn, key, from, to, sorts, es.RangeByConditionWithDocIdsNoLimit).WithDocs(docIds)
  55. }
  56. func matchRangeWithDocIdsNoLimitByScore(key string, from int, to int, sorts []string, docIds []string, score float64) (request *es.ESQueryRequest) {
  57. req := new(es.ESQueryRequest)
  58. return req.CreateESQueryRequest(htConfig.GetChartIndex(), ChartEsColumn, key, from, to, sorts, es.RangeByConditionWithDocIdsNoLimitByScore).WithScore(score).WithDocs(docIds)
  59. }
  60. func SearchChartList(key string, ids []int, from int, size int) (charts []EsChartInfo, err error) {
  61. //同步es
  62. var docIds []string
  63. for _, id := range ids {
  64. docIds = append(docIds, strconv.Itoa(id))
  65. }
  66. sorts := append(sortField)
  67. request := matchRangeWithDocIdsNoLimit(key, from, size, sorts, docIds)
  68. re, err := elastic().Search(request)
  69. if err != nil {
  70. logger.Error("es搜索异常:%v", err)
  71. }
  72. hits := elastic().GetSource(re.Hits)
  73. if len(hits) == 0 {
  74. return
  75. }
  76. for _, hit := range hits {
  77. var content map[string][]string
  78. err = json.Unmarshal(hit.Highlight, &content)
  79. chart := EsChartInfo{}
  80. err = json.Unmarshal(hit.Source, &chart)
  81. if err != nil {
  82. logger.Error("解析研报数据失败:%v", err)
  83. continue
  84. }
  85. charts = append(charts, chart)
  86. }
  87. return
  88. }
  89. func SearchAllChartList(key string, ids []int) (charts []EsChartInfo, err error) {
  90. //同步es
  91. var docIds []string
  92. for _, id := range ids {
  93. docIds = append(docIds, strconv.Itoa(id))
  94. }
  95. sorts := append(sortField)
  96. //兜底500, 避免es查询超时
  97. request := matchRangeWithDocIdsNoLimitByScore(key, 0, 5000, sorts, docIds, 0.5)
  98. re, err := elastic().Search(request)
  99. if err != nil {
  100. logger.Error("es搜索异常:%v", err)
  101. }
  102. hits := elastic().GetSource(re.Hits)
  103. if len(hits) == 0 {
  104. return
  105. }
  106. for _, hit := range hits {
  107. var content map[string][]string
  108. err = json.Unmarshal(hit.Highlight, &content)
  109. chart := EsChartInfo{}
  110. err = json.Unmarshal(hit.Source, &chart)
  111. if err != nil {
  112. logger.Error("解析研报数据失败:%v", err)
  113. continue
  114. }
  115. charts = append(charts, chart)
  116. }
  117. return
  118. }
  119. func UpdateChartImage(image string, id int) bool {
  120. doc, err := elastic().Get(htConfig.GetChartIndex(), id)
  121. if err != nil {
  122. return false
  123. }
  124. var chartInfo EsChartInfo
  125. err = json.Unmarshal(doc.Source, &chartInfo)
  126. if err != nil {
  127. logger.Error("es获取图表数据失败:%v", err)
  128. return false
  129. }
  130. if chartInfo.ChartInfoId == 0 {
  131. logger.Error("es获取图表数据失败,chartInfoId:%d", id)
  132. return false
  133. }
  134. chartInfo.ChartImage = image
  135. return elastic().Update(htConfig.GetChartIndex(), chartInfo.ChartInfoId, chartInfo)
  136. }
  137. func GetChartById(id int) (chartInfo EsChartInfo, err error) {
  138. doc, err := elastic().Get(htConfig.GetChartIndex(), id)
  139. if err != nil {
  140. logger.Error("es获取图表数据失败:%v", err)
  141. return
  142. }
  143. err = json.Unmarshal(doc.Source, &chartInfo)
  144. if err != nil {
  145. logger.Error("序列化json数据失败,获取图表数据失败:%v", err)
  146. return
  147. }
  148. return
  149. }