rag_eta_report_abstract.go 8.5 KB

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