123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package media
- import (
- "encoding/json"
- "eta/eta_mini_ht_api/common/component/es"
- logger "eta/eta_mini_ht_api/common/component/log"
- "strconv"
- )
- const (
- ChartESColumn = "chartName"
- ChartESRangeColumn = "chartInfoId"
- ChartEsColumn = "chartName"
- )
- // func SearchMediaList(_ string, key string, mediaIds []int, from int, size int, max int64) (medias []MediaDTO, err error) {
- // //同步es
- // var docIds []string
- // for _, id := range mediaIds {
- // docIds = append(docIds, strconv.Itoa(id))
- // }
- // sorts := append(sortField, "publishedTime:desc")
- // request := matchRangeWithDocIds(key, from, size, max, sorts, docIds)
- // re, err := elastic().Search(request)
- // if err != nil {
- // logger.Error("es搜索失败:%v", err)
- // }
- // hits := elastic().GetSource(re.Hits)
- // for _, hit := range hits {
- // var content map[string][]string
- // err = json.Unmarshal(hit.Highlight, &content)
- // media := MediaDTO{}
- // err = json.Unmarshal(hit.Source, &media)
- // if err != nil {
- // logger.Error("解析媒体数据失败:%v", err)
- // continue
- // }
- // media.Highlight = content[ESColumn]
- // media.PublishedTime = media.PublishedTime[:10]
- // media.MediaTitle = media.Highlight[0]
- // medias = append(medias, media)
- // }
- // return
- // }
- type EsChartInfo struct {
- ChartInfoId int
- ChartName string
- ChartImage string
- UniqueCode string
- }
- // 同步es
- func AddChartToEs(esChart EsChartInfo) bool {
- return elastic().InsertOrUpdate(htConfig.GetChartIndex(), esChart.ChartInfoId, esChart)
- }
- func matchRangeWithDocIdsNoLimit(key string, from int, to int, sorts []string, docIds []string) (request *es.ESQueryRequest) {
- req := new(es.ESQueryRequest)
- return req.CreateESQueryRequest(htConfig.GetChartIndex(), ChartEsColumn, key, from, to, sorts, es.RangeByConditionWithDocIdsNoLimit).WithDocs(docIds)
- }
- func SearchChartList(key string, ids []int, from int, size int) (charts []EsChartInfo, err error) {
- //同步es
- var docIds []string
- for _, id := range ids {
- docIds = append(docIds, strconv.Itoa(id))
- }
- sorts := append(sortField)
- request := matchRangeWithDocIdsNoLimit(key, from, size, sorts, docIds)
- re, err := elastic().Search(request)
- if err != nil {
- logger.Error("es搜索异常:%v", err)
- }
- hits := elastic().GetSource(re.Hits)
- if len(hits) == 0 {
- return
- }
- for _, hit := range hits {
- var content map[string][]string
- err = json.Unmarshal(hit.Highlight, &content)
- chart := EsChartInfo{}
- err = json.Unmarshal(hit.Source, &chart)
- if err != nil {
- logger.Error("解析研报数据失败:%v", err)
- continue
- }
- charts = append(charts, chart)
- }
- return
- }
- func UpdateChartImage(image string, id int) bool {
- doc, err := elastic().Get(htConfig.GetChartIndex(), id)
- if err != nil {
- return false
- }
- var chartInfo EsChartInfo
- err = json.Unmarshal(doc.Source, &chartInfo)
- if err != nil {
- logger.Error("es获取图表数据失败:%v", err)
- return false
- }
- if chartInfo.ChartInfoId == 0 {
- logger.Error("es获取图表数据失败,chartInfoId:%d", id)
- return false
- }
- chartInfo.ChartImage = image
- return elastic().Update(htConfig.GetChartIndex(), chartInfo.ChartInfoId, chartInfo)
- }
- func GetChartById(id int) (chartInfo EsChartInfo, err error) {
- doc, err := elastic().Get(htConfig.GetChartIndex(), id)
- if err != nil {
- logger.Error("es获取图表数据失败:%v", err)
- return
- }
- err = json.Unmarshal(doc.Source, &chartInfo)
- if err != nil {
- logger.Error("序列化json数据失败,获取图表数据失败:%v", err)
- return
- }
- return
- }
|