rag_question.go 7.7 KB

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