浏览代码

获取收藏图表列表

kobe6258 3 月之前
父节点
当前提交
45a2ad4763

+ 38 - 9
common/component/es/es.go

@@ -36,15 +36,16 @@ const (
 	MatchAll = "match_all"
 	Match    = "match"
 
-	CountWithDocIds                   = "count_with_doc_ids"
-	Range                             = "range"
-	MatchAllByCondition               = "match_all_by_condition"
-	RangeByCondition                  = "range_by_condition"
-	RangeByConditionWithDocIds        = "range_by_condition_with_doc_ids"
-	RangeByConditionWithDocIdsNoLimit = "range_by_condition_with_doc_ids_no_limit"
-	RangeWithDocIds                   = "range_with_doc_ids"
-	LimitByScore                      = "limit_by_score"
-	HomeSearch                        = "home_search"
+	CountWithDocIds                          = "count_with_doc_ids"
+	Range                                    = "range"
+	MatchAllByCondition                      = "match_all_by_condition"
+	RangeByCondition                         = "range_by_condition"
+	RangeByConditionWithDocIds               = "range_by_condition_with_doc_ids"
+	RangeByConditionWithDocIdsNoLimit        = "range_by_condition_with_doc_ids_no_limit"
+	RangeByConditionWithDocIdsNoLimitByScore = "range_by_condition_with_doc_ids_no_limit_by_score"
+	RangeWithDocIds                          = "range_with_doc_ids"
+	LimitByScore                             = "limit_by_score"
+	HomeSearch                               = "home_search"
 )
 
 func GetInstance() *ESClient {
@@ -453,6 +454,34 @@ func (req *ESQueryRequest) parseJsonQuery() (queryMap map[string]interface{}) {
 			},
 		}
 		return
+	case RangeByConditionWithDocIdsNoLimitByScore:
+		queryMap = map[string]interface{}{
+			"query": map[string]interface{}{
+				"match": map[string]interface{}{
+					req.Column: req.Key,
+				},
+			},
+			"highlight": map[string]interface{}{
+				"fields": map[string]interface{}{
+					req.Column: map[string]interface{}{},
+				},
+				"pre_tags":  []string{"<span style='color:#0078E8'>"},
+				"post_tags": []string{"</span>"},
+			},
+			"post_filter": map[string]interface{}{
+				"bool": map[string]interface{}{
+					"must": []map[string]interface{}{
+						{
+							"terms": map[string]interface{}{
+								"_id": req.DocIds,
+							},
+						},
+					},
+				},
+			},
+			"min_score": req.MinScore,
+		}
+		return
 	case LimitByScore:
 		queryMap = map[string]interface{}{
 			"query": map[string]interface{}{

+ 50 - 4
controllers/user/bookmark_controller.go

@@ -180,8 +180,9 @@ func (bk *BookMarkController) BookMarkSearch(key string) {
 		var userRiskStatus string
 		pageRes.Total, pageRes.LatestId, reportIds, _, mappingRiskLevel, userRiskStatus, err = report.RangeSearch(key, true, userInfo.Id)
 		if err != nil {
-			logger.Error("获取报告原始ID列表失败:%v", err)
-			bk.FailedResult("分页搜索报告列表失败", result)
+			logger.Error("搜索收藏列表失败%v", err)
+			err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
+			bk.FailedResult("分页搜索收藏列表失败", result)
 			return
 		}
 		if len(reportIds) == 0 {
@@ -222,8 +223,8 @@ type BookMarkListReq struct {
 	SourceType string `json:"source_type"`
 }
 
-// SearchBookMark 获取收藏列表
-// @Description 获取收藏列表
+// SearchBookMark 搜索收藏列表
+// @Description 搜索收藏列表
 // @Success 200 {object}
 // @router /searchBookMark [get]
 func (bk *BookMarkController) SearchBookMark(sourceType string, key string) {
@@ -246,6 +247,12 @@ func (bk *BookMarkController) SearchBookMark(sourceType string, key string) {
 		userInfo := bk.Data["user"].(user.User)
 		var sourceIds []int
 		pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
+		if err != nil {
+			logger.Error("获取收藏列表失败%v", err)
+			err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
+			bk.FailedResult("分页查询收藏列表失败", result)
+			return
+		}
 		//隐藏品种信息未设置风险等级的报告
 		if sourceType == Report {
 			pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
@@ -359,6 +366,12 @@ func (bk *BookMarkController) BookMarkList(sourceType string) {
 		userInfo := bk.Data["user"].(user.User)
 		var sourceIds []int
 		pageRes.Total, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, sourceType)
+		if err != nil {
+			logger.Error("获取收藏列表失败%v", err)
+			err = exception.NewWithException(exception.GetBookMarkListFailed, err.Error())
+			bk.FailedResult("分页查询收藏列表失败", result)
+			return
+		}
 		//隐藏品种信息未设置风险等级的报告
 		if sourceType == Report {
 			pageRes.Total, sourceIds, err = report.FilterReportIds(sourceIds)
@@ -430,6 +443,39 @@ func (bk *BookMarkController) ChartList() {
 	})
 }
 
+// SearchChartList 获取搜索关键字的收藏列表
+// @Description 获取搜索关键字的收藏列表
+// @Success 200 {object}
+// @router /bookmark/search/chartList [get]
+func (bk *BookMarkController) SearchChartList(key string) {
+	controllers.Wrap(&bk.BaseController, func() (result *controllers.WrapData, err error) {
+		result = bk.InitWrapData("分页搜索收藏列表失败")
+		if key == "" {
+			err = exception.New(exception.IllegalSearchKeyword)
+			bk.FailedResult("分页搜索收藏列表失败", result)
+			return
+		}
+		userInfo := bk.Data["user"].(user.User)
+		var sourceIds []int
+		_, sourceIds, err = user.GetTotalBookMarkPageBySourceType(userInfo.Id, Chart)
+		var bookMarkList []userService.BookMarkInterface
+		chartList, chartErr := chartService.SearchAllChartList(key, sourceIds)
+		if chartErr != nil {
+			logger.Error("搜索研报列表失败%v", err)
+			err = exception.NewWithException(exception.GetBookMarkListFailed, chartErr.Error())
+			bk.FailedResult("分页搜索收藏列表失败", result)
+			return
+		}
+		for _, chart := range chartList {
+			bookMarkList = append(bookMarkList, ConvertToBookMarkChart(chart))
+		}
+		//bookMarkList, err = user.SearchBookMark(key, sourceType, sourceIds, bk.PageInfo, userInfo.Id)
+		bookMarks := new(page.PageResult)
+		bookMarks.Data = bookMarkList
+		bk.SuccessResult("分页搜索收藏列表成功", bookMarks, result)
+		return
+	})
+}
 func getReportList(info page.PageInfo, templateUserId int, sourceIds []int) (list []userService.BookMarkReport, err error) {
 	sourceIds, err = userService.GetBookMarkPageRangeBySourceType(templateUserId, info, Report, sourceIds)
 	if err != nil {

+ 35 - 0
domian/media/chart_service.go

@@ -57,6 +57,10 @@ func matchRangeWithDocIdsNoLimit(key string, from int, to int, sorts []string, d
 	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
@@ -86,6 +90,37 @@ func SearchChartList(key string, ids []int, from int, size int) (charts []EsChar
 	}
 	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 {

+ 11 - 0
routers/commentsRouter.go

@@ -462,6 +462,17 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_mini_ht_api/controllers/user:BookMarkController"] = append(beego.GlobalControllerRouter["eta/eta_mini_ht_api/controllers/user:BookMarkController"],
+        beego.ControllerComments{
+            Method: "SearchChartList",
+            Router: `/bookmark/search/chartList`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(
+				param.New("key"),
+			),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_mini_ht_api/controllers/user:BookMarkController"] = append(beego.GlobalControllerRouter["eta/eta_mini_ht_api/controllers/user:BookMarkController"],
         beego.ControllerComments{
             Method: "CheckBookMark",

+ 10 - 0
service/media/chart_service.go

@@ -50,6 +50,16 @@ func SearchChartList(key string, ids []int, pageInfo page.PageInfo) (charts []Ch
 	}
 	return
 }
+func SearchAllChartList(key string, ids []int) (charts []ChartInfo, err error) {
+	esChart, err := chartService.SearchAllChartList(key, ids)
+	if err != nil {
+		return
+	}
+	for _, chart := range esChart {
+		charts = append(charts, convertChartInfo(chart))
+	}
+	return
+}
 
 func UpdateChartImage(image string, id int) bool {
 	return chartService.UpdateChartImage(image, id)