wechat_article_abstract.go 9.2 KB

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