package elastic import ( "context" "encoding/json" "eta/eta_api/models/rag" "eta/eta_api/utils" "fmt" "github.com/olivere/elastic/v7" "time" ) // WechatArticleAndPlatform // @Description: 存入ES的数据 type WechatArticleAndPlatform struct { WechatArticleId int `gorm:"column:wechat_article_id;type:int(10) UNSIGNED;primaryKey;not null;" description:""` WechatPlatformId int `gorm:"column:wechat_platform_id;type:int(11);comment:归属公众号id;default:0;" description:"归属公众号id"` FakeId string `gorm:"column:fake_id;type:varchar(255);comment:公众号唯一id;" description:"公众号唯一id"` Title string `gorm:"column:title;type:varchar(255);comment:标题;" description:"标题"` Link string `gorm:"column:link;type:varchar(255);comment:链接;" description:"链接"` CoverUrl string `gorm:"column:cover_url;type:varchar(255);comment:公众号封面;" description:"公众号封面"` Description string `gorm:"column:description;type:varchar(255);comment:描述;" description:"描述"` Content string `gorm:"column:content;type:longtext;comment:报告详情;" description:"报告详情"` TextContent string `gorm:"column:text_content;type:text;comment:文本内容;" description:"文本内容"` //Abstract string `gorm:"column:abstract;type:text;comment:摘要;" description:"摘要"` Country string `gorm:"column:country;type:varchar(255);comment:国家;" description:"国家"` Province string `gorm:"column:province;type:varchar(255);comment:省;" description:"省"` City string `gorm:"column:city;type:varchar(255);comment:市;" description:"市"` ArticleCreateTime time.Time `gorm:"column:article_create_time;type:datetime;comment:报告创建时间;default:NULL;" description:"报告创建时间"` IsDeleted int `gorm:"column:is_deleted;type:tinyint(4);comment:是否删除,0:未删除,1: 已删除;default:0;" description:"是否删除,0:未删除,1: 已删除"` ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;" description:"修改时间"` CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:入库时间;default:NULL;" description:"入库时间"` Nickname string `gorm:"column:nickname;type:varchar(255);comment:公众号名称;" description:"nickname"` // 公众号名称 Alias string `gorm:"column:alias;type:varchar(255);comment:别名;" description:"alias"` // 别名 RoundHeadImg string `gorm:"column:round_head_img;type:varchar(255);comment:头像;" description:"round_head_img"` // 头像 } func (m *WechatArticleAndPlatform) ToView() rag.WechatArticleView { var articleCreateTime, modifyTime, createTime string if !m.ArticleCreateTime.IsZero() { articleCreateTime = m.ArticleCreateTime.Format(utils.FormatDateTime) } if !m.CreateTime.IsZero() { createTime = m.CreateTime.Format(utils.FormatDateTime) } if !m.ModifyTime.IsZero() { modifyTime = m.ModifyTime.Format(utils.FormatDateTime) } return rag.WechatArticleView{ WechatArticleId: m.WechatArticleId, WechatPlatformId: m.WechatPlatformId, FakeId: m.FakeId, Title: m.Title, Link: m.Link, CoverUrl: m.CoverUrl, Description: m.Description, Content: m.Content, TextContent: m.TextContent, //Abstract: m.Abstract, Country: m.Country, Province: m.Province, City: m.City, ArticleCreateTime: articleCreateTime, ModifyTime: modifyTime, CreateTime: createTime, WechatPlatformName: m.Nickname, WechatPlatformRoundHeadImg: m.RoundHeadImg, } } func (m *WechatArticleAndPlatform) ArticleAndPlatformListToViewList(list []*WechatArticleAndPlatform) (wechatArticleViewList []rag.WechatArticleView) { wechatArticleViewList = make([]rag.WechatArticleView, 0) for _, v := range list { wechatArticleViewList = append(wechatArticleViewList, v.ToView()) } return } // WechatArticleEsAddOrEdit // @Description: 新增/编辑微信文章 // @author: Roc // @datetime 2025-03-13 10:24:05 // @param docId string // @param item WechatArticleAndPlatform // @return err error func WechatArticleEsAddOrEdit(docId string, item WechatArticleAndPlatform) (err error) { if docId == "" { return } if utils.EsWechatArticleName == `` { return } defer func() { if err != nil { fmt.Println("WechatArticleEsAddOrEdit Err:", err.Error()) } }() client := utils.EsClient resp, err := client.Index().Index(utils.EsWechatArticleName).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 WechatArticleEsDel(docId string) (err error) { if docId == "" { return } if utils.EsWechatArticleName == `` { return } defer func() { if err != nil { fmt.Println("EsDeleteEdbInfoData Err:", err.Error()) } }() client := utils.EsClient resp, err := client.Delete().Index(utils.EsWechatArticleName).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 } func WechatArticleEsSearch(keywordStr string, wechatPlatformId, from, size int, sortMap map[string]string) (total int64, list []*WechatArticleAndPlatform, err error) { indexName := utils.EsWechatArticleName list = make([]*WechatArticleAndPlatform, 0) defer func() { if err != nil { fmt.Println("SearchEdbInfoData Err:", err.Error()) } }() query := elastic.NewBoolQuery() if wechatPlatformId > 0 { query = query.Must(elastic.NewTermQuery("WechatPlatformId", wechatPlatformId)) } // 名字匹配 if keywordStr != `` { query = query.Must(elastic.NewMultiMatchQuery(keywordStr, "Title")) } // 排序 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 searchWechatArticle(indexName, query, sortList, from, size) } // searchEdbInfoDataV2 查询es中的数据 func searchWechatArticle(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*WechatArticleAndPlatform, err error) { total, err = searchWechatArticleTotal(indexName, query) if err != nil { return } // 获取列表数据 list, err = searchWechatArticleList(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 searchWechatArticleTotal(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 searchWechatArticleList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*WechatArticleAndPlatform, err error) { list = make([]*WechatArticleAndPlatform, 0) defer func() { if err != nil { fmt.Println("searchEdbInfoDataList Err:", err.Error()) } }() client := utils.EsClient // 高亮 highlight := elastic.NewHighlight() highlight = highlight.Fields(elastic.NewHighlighterField("Title")) highlight = highlight.PreTags("").PostTags("") //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(WechatArticleAndPlatform) tmpErr = json.Unmarshal(itemJson, &item) if tmpErr != nil { fmt.Println("json.Unmarshal movieJson err:", tmpErr) err = tmpErr return } if len(v.Highlight["Title"]) > 0 { item.Title = v.Highlight["Title"][0] } list = append(list, item) searchMap[v.Id] = v.Id } } } return }