123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- package elastic
- import (
- "context"
- "encoding/json"
- "eta/eta_api/models/rag"
- "eta/eta_api/utils"
- "fmt"
- "github.com/olivere/elastic/v7"
- "time"
- )
- // 问题索引
- var EsRagQuestionName = utils.EsRagQuestionName
- type RagQuestionItem struct {
- QuestionId int `gorm:"column:question_id;type:int(9) UNSIGNED;primaryKey;not null;" description:"question_id"`
- QuestionTitle string `gorm:"column:question_title;type:varchar(255);comment:问题标题;" description:"问题标题"`
- QuestionContent string `gorm:"column:question_content;type:varchar(255);comment:问题内容;" description:"问题内容"`
- Sort int `gorm:"column:sort;type:int(11);comment:排序;default:0;" description:"排序"`
- SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:添加人id;default:0;" description:"添加人id"`
- SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(255);comment:添加人真实名称;" description:"添加人真实名称"`
- ModifyTime time.Time `gorm:"column:modify_time;type:datetime;default:NULL;" description:"modify_time"`
- CreateTime time.Time `gorm:"column:create_time;type:datetime;default:NULL;" description:"create_time"`
- }
- func (m *RagQuestionItem) ToView() rag.QuestionView {
- var modifyTime, createTime string
- if !m.CreateTime.IsZero() {
- createTime = m.CreateTime.Format(utils.FormatDateTime)
- }
- if !m.ModifyTime.IsZero() {
- modifyTime = m.ModifyTime.Format(utils.FormatDateTime)
- }
- return rag.QuestionView{
- QuestionId: m.QuestionId,
- QuestionTitle: m.QuestionTitle,
- QuestionContent: m.QuestionContent,
- Sort: m.Sort,
- SysUserId: m.SysUserId,
- SysUserRealName: m.SysUserRealName,
- ModifyTime: modifyTime,
- CreateTime: createTime,
- }
- }
- func (m *RagQuestionItem) ToViewList(list []*RagQuestionItem) (wechatArticleViewList []rag.QuestionView) {
- wechatArticleViewList = make([]rag.QuestionView, 0)
- for _, v := range list {
- wechatArticleViewList = append(wechatArticleViewList, v.ToView())
- }
- return
- }
- // RagQuestionEsAddOrEdit
- // @Description: 新增/编辑微信文章
- // @author: Roc
- // @datetime 2025-03-13 10:24:05
- // @param docId string
- // @param item WechatArticleAndPlatform
- // @return err error
- func RagQuestionEsAddOrEdit(docId string, item RagQuestionItem) (err error) {
- if docId == "" {
- return
- }
- if EsRagQuestionName == `` {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("WechatArticleEsAddOrEdit Err:", err.Error())
- }
- }()
- client := utils.EsClient
- resp, err := client.Index().Index(EsRagQuestionName).Id(docId).BodyJson(item).Refresh("true").Do(context.Background())
- if err != nil {
- fmt.Println("新增失败:", err.Error())
- return err
- }
- if resp.Status == 0 {
- fmt.Println("新增成功", resp.Result)
- err = nil
- } else {
- fmt.Println("WechatArticleEsAddOrEdit", resp.Status, resp.Result)
- }
- return
- }
- // WechatArticleEsDel
- // @Description: 删除微信文章
- // @author: Roc
- // @datetime 2025-03-13 10:23:55
- // @param docId string
- // @return err error
- func RagQuestionEsDel(docId string) (err error) {
- if docId == "" {
- return
- }
- if EsRagQuestionName == `` {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
- }
- }()
- client := utils.EsClient
- resp, err := client.Delete().Index(EsRagQuestionName).Id(docId).Refresh(`true`).Do(context.Background())
- if err != nil {
- return
- }
- if resp.Status == 0 {
- fmt.Println("删除成功")
- } else {
- fmt.Println("WechatArticleEsDel", resp.Status, resp.Result)
- }
- return
- }
- // RagQuestionEsSearch
- // @Description: 搜索
- // @author: Roc
- // @datetime 2025-03-13 19:54:54
- // @param keywordStr string
- // @param tagIdList []int
- // @param platformIdList []int
- // @param from int
- // @param size int
- // @param sortMap map[string]string
- // @return total int64
- // @return list []*RagQuestionItem
- // @return err error
- func RagQuestionEsSearch(keywordStr string, from, size int, sortMap map[string]string) (total int64, list []*RagQuestionItem, err error) {
- indexName := EsRagQuestionName
- list = make([]*RagQuestionItem, 0)
- defer func() {
- if err != nil {
- fmt.Println("SearchEdbInfoData Err:", err.Error())
- }
- }()
- query := elastic.NewBoolQuery()
- // 名字匹配
- if keywordStr != `` {
- query = query.Must(elastic.NewMultiMatchQuery(keywordStr, "QuestionContent"))
- }
- // 排序
- sortList := make([]*elastic.FieldSort, 0)
- // 如果没有关键字,那么就走指标id倒序
- for orderKey, orderType := range sortMap {
- switch orderType {
- case "asc":
- sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
- case "desc":
- sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
- }
- }
- return searchRagQuestion(indexName, query, sortList, from, size)
- }
- // searchRagQuestion 查询es中的数据
- func searchRagQuestion(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*RagQuestionItem, err error) {
- total, err = searchRagQuestionTotal(indexName, query)
- if err != nil {
- return
- }
- // 获取列表数据
- list, err = searchRagQuestionList(indexName, query, sortList, from, size)
- if err != nil {
- return
- }
- return
- }
- // searchEdbInfoDataTotal
- // @Description: 查询es中的数量
- // @author: Roc
- // @datetime 2024-12-23 11:19:04
- // @param indexName string
- // @param query elastic.Query
- // @return total int64
- // @return err error
- func searchRagQuestionTotal(indexName string, query elastic.Query) (total int64, err error) {
- defer func() {
- if err != nil {
- fmt.Println("searchEdbInfoDataTotal Err:", err.Error())
- }
- }()
- client := utils.EsClient
- //根据条件数量统计
- requestTotalHits := client.Count(indexName).Query(query)
- total, err = requestTotalHits.Do(context.Background())
- if err != nil {
- return
- }
- return
- }
- // searchEdbInfoDataList
- // @Description: 查询es中的明细数据
- // @author: Roc
- // @datetime 2024-12-23 11:18:48
- // @param indexName string
- // @param query elastic.Query
- // @param sortList []*elastic.FieldSort
- // @param from int
- // @param size int
- // @return list []*data_manage.EdbInfoList
- // @return err error
- func searchRagQuestionList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*RagQuestionItem, err error) {
- list = make([]*RagQuestionItem, 0)
- defer func() {
- if err != nil {
- fmt.Println("searchEdbInfoDataList Err:", err.Error())
- }
- }()
- client := utils.EsClient
- // 高亮
- highlight := elastic.NewHighlight()
- highlight = highlight.Fields(elastic.NewHighlighterField("Content"))
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
- //request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
- request := client.Search(indexName).From(from).Size(size) // sets the JSON request
- // 如果有指定排序,那么就按照排序来
- if len(sortList) > 0 {
- for _, v := range sortList {
- request = request.SortBy(v)
- }
- }
- searchMap := make(map[string]string)
- searchResp, err := request.Query(query).Do(context.Background())
- if err != nil {
- return
- }
- //fmt.Println(searchResp)
- //fmt.Println(searchResp.Status)
- if searchResp.Status != 0 {
- return
- }
- //total = searchResp.TotalHits()
- if searchResp.Hits != nil {
- for _, v := range searchResp.Hits.Hits {
- if _, ok := searchMap[v.Id]; !ok {
- itemJson, tmpErr := v.Source.MarshalJSON()
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("movieJson err:", err)
- return
- }
- item := new(RagQuestionItem)
- tmpErr = json.Unmarshal(itemJson, &item)
- if tmpErr != nil {
- fmt.Println("json.Unmarshal movieJson err:", tmpErr)
- err = tmpErr
- return
- }
- if len(v.Highlight["QuestionContent"]) > 0 {
- item.QuestionContent = v.Highlight["QuestionContent"][0]
- }
- list = append(list, item)
- searchMap[v.Id] = v.Id
- }
- }
- }
- return
- }
|