wechat_article_abstract.go 9.6 KB

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