wechat_article_abstract.go 9.5 KB

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