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 `json:"chartInfoId"` ChartName string `json:"chartName"` ChartImage string `json:"chartImage"` UniqueCode string `json:"uniqueCode"` } // 同步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 matchRangeWithDocIdsNoLimitByScore(key string, from int, to int, sorts []string, docIds []string, score float64) (request *es.ESQueryRequest) { req := new(es.ESQueryRequest) return req.CreateESQueryRequest(htConfig.GetChartIndex(), ChartEsColumn, key, from, to, sorts, es.RangeByConditionWithDocIdsNoLimitByScore).WithScore(score).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 SearchAllChartList(key string, ids []int) (charts []EsChartInfo, err error) { //同步es var docIds []string for _, id := range ids { docIds = append(docIds, strconv.Itoa(id)) } sorts := append(sortField) //兜底500, 避免es查询超时 request := matchRangeWithDocIdsNoLimitByScore(key, 0, 5000, sorts, docIds, 0.5) 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 }