wechat_article_abstract.go 9.4 KB

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