wechat_article_abstract.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_api/models/rag"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/olivere/elastic/v7"
  9. "time"
  10. )
  11. // 摘要索引
  12. var EsWechatArticleAbstractName = utils.EsWechatArticleAbstractName
  13. type WechatArticleAbstractItem struct {
  14. WechatArticleAbstractId int `gorm:"column:wechat_article_abstract_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"wechat_article_abstract_id"`
  15. WechatArticleId int `gorm:"column:wechat_article_id;type:int(9) UNSIGNED;comment:关联的微信报告id;default:0;" description:"关联的微信报告id"`
  16. Abstract string `gorm:"column:abstract;type:longtext;comment:摘要内容;" description:"摘要内容"` //
  17. Version int `gorm:"column:version;type:int(10) UNSIGNED;comment:版本号;default:1;" description:"版本号"`
  18. VectorKey string `gorm:"column:vector_key;type:varchar(255);comment:向量key标识;" description:"向量key标识"`
  19. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
  20. CreateTime time.Time `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
  21. Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"`
  22. Link string `gorm:"column:link;type:varchar(255);comment:链接;" description:"链接"`
  23. TagIdList []int `description:"品种id列表"`
  24. }
  25. func (m *WechatArticleAbstractItem) ToView() rag.WechatArticleAbstractView {
  26. var modifyTime, createTime string
  27. if !m.CreateTime.IsZero() {
  28. createTime = m.CreateTime.Format(utils.FormatDateTime)
  29. }
  30. if !m.ModifyTime.IsZero() {
  31. modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
  32. }
  33. tagId := 0
  34. if len(m.TagIdList) > 0 {
  35. tagId = m.TagIdList[0]
  36. }
  37. return rag.WechatArticleAbstractView{
  38. WechatArticleAbstractId: m.WechatArticleAbstractId,
  39. WechatArticleId: m.WechatArticleId,
  40. Abstract: m.Abstract,
  41. Version: m.Version,
  42. VectorKey: m.VectorKey,
  43. ModifyTime: modifyTime,
  44. CreateTime: createTime,
  45. Title: m.Title,
  46. Link: m.Link,
  47. TagId: tagId,
  48. }
  49. }
  50. func (m *WechatArticleAbstractItem) ToViewList(list []*WechatArticleAbstractItem) (wechatArticleViewList []rag.WechatArticleAbstractView) {
  51. wechatArticleViewList = make([]rag.WechatArticleAbstractView, 0)
  52. for _, v := range list {
  53. wechatArticleViewList = append(wechatArticleViewList, v.ToView())
  54. }
  55. return
  56. }
  57. // WechatArticleEsAddOrEdit
  58. // @Description: 新增/编辑微信文章
  59. // @author: Roc
  60. // @datetime 2025-03-13 10:24:05
  61. // @param docId string
  62. // @param item WechatArticleAndPlatform
  63. // @return err error
  64. func WechatArticleAbstractEsAddOrEdit(docId string, item WechatArticleAbstractItem) (err error) {
  65. if docId == "" {
  66. return
  67. }
  68. if EsWechatArticleAbstractName == `` {
  69. return
  70. }
  71. defer func() {
  72. if err != nil {
  73. fmt.Println("WechatArticleEsAddOrEdit Err:", err.Error())
  74. }
  75. }()
  76. client := utils.EsClient
  77. resp, err := client.Index().Index(EsWechatArticleAbstractName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
  78. if err != nil {
  79. fmt.Println("新增失败:", err.Error())
  80. return err
  81. }
  82. if resp.Status == 0 {
  83. fmt.Println("新增成功", resp.Result)
  84. err = nil
  85. } else {
  86. fmt.Println("WechatArticleEsAddOrEdit", resp.Status, resp.Result)
  87. }
  88. return
  89. }
  90. // WechatArticleEsDel
  91. // @Description: 删除微信文章
  92. // @author: Roc
  93. // @datetime 2025-03-13 10:23:55
  94. // @param docId string
  95. // @return err error
  96. func WechatArticleAbstractEsDel(docId string) (err error) {
  97. if docId == "" {
  98. return
  99. }
  100. if EsWechatArticleAbstractName == `` {
  101. return
  102. }
  103. defer func() {
  104. if err != nil {
  105. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  106. }
  107. }()
  108. client := utils.EsClient
  109. resp, err := client.Delete().Index(EsWechatArticleAbstractName).Id(docId).Refresh(`true`).Do(context.Background())
  110. if err != nil {
  111. return
  112. }
  113. if resp.Status == 0 {
  114. fmt.Println("删除成功")
  115. } else {
  116. fmt.Println("WechatArticleEsDel", resp.Status, resp.Result)
  117. }
  118. return
  119. }
  120. func WechatArticleAbstractEsSearch(keywordStr string, tagId, from, size int, sortMap map[string]string) (total int64, list []*WechatArticleAbstractItem, err error) {
  121. indexName := EsWechatArticleAbstractName
  122. list = make([]*WechatArticleAbstractItem, 0)
  123. defer func() {
  124. if err != nil {
  125. fmt.Println("SearchEdbInfoData Err:", err.Error())
  126. }
  127. }()
  128. query := elastic.NewBoolQuery()
  129. if tagId > 0 {
  130. query = query.Must(elastic.NewTermsQuery("TagIdList", tagId))
  131. }
  132. // 名字匹配
  133. if keywordStr != `` {
  134. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, "Abstract"))
  135. }
  136. // 排序
  137. sortList := make([]*elastic.FieldSort, 0)
  138. // 如果没有关键字,那么就走指标id倒序
  139. for orderKey, orderType := range sortMap {
  140. switch orderType {
  141. case "asc":
  142. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  143. case "desc":
  144. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  145. }
  146. }
  147. return searchWechatArticleAbstract(indexName, query, sortList, from, size)
  148. }
  149. // searchEdbInfoDataV2 查询es中的数据
  150. func searchWechatArticleAbstract(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*WechatArticleAbstractItem, err error) {
  151. total, err = searchWechatArticleAbstractTotal(indexName, query)
  152. if err != nil {
  153. return
  154. }
  155. // 获取列表数据
  156. list, err = searchWechatArticleAbstractList(indexName, query, sortList, from, size)
  157. if err != nil {
  158. return
  159. }
  160. return
  161. }
  162. // searchEdbInfoDataTotal
  163. // @Description: 查询es中的数量
  164. // @author: Roc
  165. // @datetime 2024-12-23 11:19:04
  166. // @param indexName string
  167. // @param query elastic.Query
  168. // @return total int64
  169. // @return err error
  170. func searchWechatArticleAbstractTotal(indexName string, query elastic.Query) (total int64, err error) {
  171. defer func() {
  172. if err != nil {
  173. fmt.Println("searchEdbInfoDataTotal Err:", err.Error())
  174. }
  175. }()
  176. client := utils.EsClient
  177. //根据条件数量统计
  178. requestTotalHits := client.Count(indexName).Query(query)
  179. total, err = requestTotalHits.Do(context.Background())
  180. if err != nil {
  181. return
  182. }
  183. return
  184. }
  185. // searchEdbInfoDataList
  186. // @Description: 查询es中的明细数据
  187. // @author: Roc
  188. // @datetime 2024-12-23 11:18:48
  189. // @param indexName string
  190. // @param query elastic.Query
  191. // @param sortList []*elastic.FieldSort
  192. // @param from int
  193. // @param size int
  194. // @return list []*data_manage.EdbInfoList
  195. // @return err error
  196. func searchWechatArticleAbstractList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*WechatArticleAbstractItem, err error) {
  197. list = make([]*WechatArticleAbstractItem, 0)
  198. defer func() {
  199. if err != nil {
  200. fmt.Println("searchEdbInfoDataList Err:", err.Error())
  201. }
  202. }()
  203. client := utils.EsClient
  204. // 高亮
  205. highlight := elastic.NewHighlight()
  206. highlight = highlight.Fields(elastic.NewHighlighterField("Content"))
  207. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  208. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
  209. request := client.Search(indexName).From(from).Size(size) // sets the JSON request
  210. // 如果有指定排序,那么就按照排序来
  211. if len(sortList) > 0 {
  212. for _, v := range sortList {
  213. request = request.SortBy(v)
  214. }
  215. }
  216. searchMap := make(map[string]string)
  217. searchResp, err := request.Query(query).Do(context.Background())
  218. if err != nil {
  219. return
  220. }
  221. //fmt.Println(searchResp)
  222. //fmt.Println(searchResp.Status)
  223. if searchResp.Status != 0 {
  224. return
  225. }
  226. //total = searchResp.TotalHits()
  227. if searchResp.Hits != nil {
  228. for _, v := range searchResp.Hits.Hits {
  229. if _, ok := searchMap[v.Id]; !ok {
  230. itemJson, tmpErr := v.Source.MarshalJSON()
  231. if tmpErr != nil {
  232. err = tmpErr
  233. fmt.Println("movieJson err:", err)
  234. return
  235. }
  236. item := new(WechatArticleAbstractItem)
  237. tmpErr = json.Unmarshal(itemJson, &item)
  238. if tmpErr != nil {
  239. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  240. err = tmpErr
  241. return
  242. }
  243. if len(v.Highlight["Content"]) > 0 {
  244. item.Abstract = v.Highlight["Content"][0]
  245. }
  246. list = append(list, item)
  247. searchMap[v.Id] = v.Id
  248. }
  249. }
  250. }
  251. return
  252. }