wechat_article_abstract.go 9.3 KB

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