浏览代码

研报列表的普通搜索逻辑,添加es搜索;搜索过滤隐藏的分类下的报告

xiexiaoyuan 3 年之前
父节点
当前提交
b8d0868a35

+ 3 - 0
models/response/report.go

@@ -92,6 +92,7 @@ type ReportListItem struct {
 	Stage              int    `description:"期数" json:"stage"`
 	Stage              int    `description:"期数" json:"stage"`
 	VideoUrl           string `description:"音频文件URL" json:"video_url"`
 	VideoUrl           string `description:"音频文件URL" json:"video_url"`
 	AuthOk              bool `json:"auth_ok"`
 	AuthOk              bool `json:"auth_ok"`
+	TitleInfo         string `json:"title_info"`
 }
 }
 
 
 type ReportList struct {
 type ReportList struct {
@@ -140,6 +141,8 @@ type EsReportItem struct {
 	ReportChapterId    int    `description:"报告章节Id"`
 	ReportChapterId    int    `description:"报告章节Id"`
 	ReportId           int    `description:"报告Id"`
 	ReportId           int    `description:"报告Id"`
 	Title              string `description:"标题"`
 	Title              string `description:"标题"`
+	Abstract           string `description:"摘要"`
+	StageStr           string `description:"期数"`
 }
 }
 
 
 type TickerDataItem struct {
 type TickerDataItem struct {

+ 3 - 21
models/tables/rddp/classify/query.go

@@ -5,13 +5,8 @@ import (
 	"hongze/hongze_yb/utils"
 	"hongze/hongze_yb/utils"
 )
 )
 
 
-func GetAll()(list []*Classify, err error)  {
-	err = global.MYSQL["rddp"].Model(Classify{}).Scan(&list).Error
-	return
-}
-
-func GetSimpleAll()(list []*Classify, err error)  {
-	err = global.MYSQL["rddp"].Select("id, classify_name, yb_icon_url, yb_bg_url, parent_id").Model(Classify{}).Scan(&list).Error
+func GetSimpleAll()(list []*Classify, err error)  {// 过滤权益研报的分类
+	err = global.MYSQL["rddp"].Select("id, classify_name, yb_icon_url, yb_bg_url, parent_id").Model(Classify{}).Where("is_show = 1  AND parent_id != 56 AND id != 56").Scan(&list).Error
 	return
 	return
 }
 }
 // GetByClassifyName 根据分类名称查找专栏详情
 // GetByClassifyName 根据分类名称查找专栏详情
@@ -26,7 +21,7 @@ func GetByClassifyName(classifyName string) (item *Classify, err error) {
 // GetSecondIdsByClassifyNames 根据权限相关的二级分类名称获取名称ID
 // GetSecondIdsByClassifyNames 根据权限相关的二级分类名称获取名称ID
 func GetSecondIdsByClassifyNames(names []string) (ids []int, err error) {
 func GetSecondIdsByClassifyNames(names []string) (ids []int, err error) {
 	var list []*Classify
 	var list []*Classify
-	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id >0 ", names).Scan(&list).Error
+	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) and parent_id >0 AND is_show = 1 AND parent_id != 56", names).Scan(&list).Error
 	if err != nil {
 	if err != nil {
 		if err == utils.ErrNoRow {
 		if err == utils.ErrNoRow {
 			err = nil
 			err = nil
@@ -66,19 +61,6 @@ func GetListByPid(pid int) (list []*Classify, err error) {
 	return
 	return
 }
 }
 
 
-// GetIdsByClassifyName 根据分类名称查找id
-func GetIdsByClassifyName(names []string) (ids []int, err error) {
-	var list []*Classify
-	err = global.MYSQL["rddp"].Model(Classify{}).Select("id").Where("classify_name in (?) ", names).Scan(&list).Error
-	if err != nil {
-		return
-	}
-	for _, v := range list {
-		ids = append(ids, v.Id)
-	}
-	return
-}
-
 
 
 // GetIdsByClassifyNameAndParentId 查询
 // GetIdsByClassifyNameAndParentId 查询
 func GetIdsByClassifyNameAndParentId(names []string, parentId int) (ids []int, err error) {
 func GetIdsByClassifyNameAndParentId(names []string, parentId int) (ids []int, err error) {

+ 1 - 0
models/tables/rddp/report/report.go

@@ -25,6 +25,7 @@ type Report struct {
 	VideoPlaySeconds   string `description:"音频播放时长" json:"video_play_seconds"`
 	VideoPlaySeconds   string `description:"音频播放时长" json:"video_play_seconds"`
 	ContentSub         string `description:"内容前两个章节" json:"content_sub"`
 	ContentSub         string `description:"内容前两个章节" json:"content_sub"`
 	ThsMsgIsSend       int    `description:"客户群消息是否已发送,0:否,1:是" json:"ths_msg_is_send"`
 	ThsMsgIsSend       int    `description:"客户群消息是否已发送,0:否,1:是" json:"ths_msg_is_send"`
+	StageStr           string `description:"期数" json:"stage_str"`
 }
 }
 
 
 type UnReadItem struct {
 type UnReadItem struct {

+ 155 - 7
services/elastic/report.go

@@ -2,6 +2,7 @@ package elastic
 
 
 import (
 import (
 	"context"
 	"context"
+	"encoding/json"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"github.com/olivere/elastic/v7"
 	"github.com/olivere/elastic/v7"
@@ -9,7 +10,8 @@ import (
 	"hongze/hongze_yb/utils"
 	"hongze/hongze_yb/utils"
 )
 )
 
 
-func SearchReport(keyWord string, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
+// 首页搜索
+func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
 	indexName := global.CONFIG.EsClient.Prefix+utils.ES_INDEX_RDDP_REPORT
 	indexName := global.CONFIG.EsClient.Prefix+utils.ES_INDEX_RDDP_REPORT
 	var must  []map[string]interface{}
 	var must  []map[string]interface{}
 
 
@@ -73,16 +75,34 @@ func SearchReport(keyWord string, pageIndex, pageSize int) (searchResp *elastic.
 		},
 		},
 	}
 	}
 	must = append(must, mustMap)
 	must = append(must, mustMap)
-
-	must = append(must, map[string]interface{}{
-		"match": map[string]interface{}{
-			"PublishState": 2,
+	filterMust := []map[string]interface{}{
+		map[string]interface{}{
+			"term": map[string]interface{}{
+				"PublishState": 2,                     //必须是已发布的报告
+			},
+		},
+		map[string]interface{}{
+			"terms": map[string]interface{}{
+				"ClassifyIdFirst":classifyIdFirsts,     //分类必须是正常显示状态
+			},
+		},
+		map[string]interface{}{
+			"terms": map[string]interface{}{
+				"ClassifyIdSecond":classifyIdSeconds,     //分类必须是正常显示状态
+			},
+		},
+	}
+	filterMap := map[string]interface{}{
+		"bool": map[string]interface{}{
+			"must": filterMust,
+			//"must_not": filterMustNot,
 		},
 		},
-	})
+	}
 	source := map[string]interface{}{
 	source := map[string]interface{}{
 		"query": map[string]interface{}{
 		"query": map[string]interface{}{
 			"bool": map[string]interface{}{
 			"bool": map[string]interface{}{
 				"must": must,
 				"must": must,
+				"filter": filterMap,
 			},
 			},
 		},
 		},
 	}
 	}
@@ -126,7 +146,135 @@ func SearchReport(keyWord string, pageIndex, pageSize int) (searchResp *elastic.
 	}
 	}
 
 
 	fmt.Print("结果正常:")
 	fmt.Print("结果正常:")
-	fmt.Println(searchResp.Hits)
+	fmt.Println(searchResp.Status)
+	if searchResp.Status != 0 {
+		err = errors.New("查询失败")
+	}
+	total = searchResp.TotalHits()
+	return
+}
+
+// ReportListSearch 报告列表页的搜索
+func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
+	indexName := global.CONFIG.EsClient.Prefix+utils.ES_INDEX_RDDP_REPORT
+	var must  []map[string]interface{}
+	shouldSub := []map[string]interface{}{
+		map[string]interface{}{
+			"match": map[string]interface{}{
+				"Title": map[string]interface{}{
+					"query": keyWord,
+					"boost": 3,
+				},
+			},
+		},
+		map[string]interface{}{
+			"match": map[string]interface{}{
+				"StageStr": map[string]interface{}{
+					"query": keyWord,
+					"boost": 2,
+				},
+			},
+		},
+		map[string]interface{}{
+			"match": map[string]interface{}{
+				"Abstract": map[string]interface{}{
+					"query": keyWord,
+					"boost": 2,
+				},
+			},
+		},
+		map[string]interface{}{
+			"match_phrase": map[string]interface{}{
+				"Title": map[string]interface{}{
+					"query": keyWord,
+					//"slop": "50",
+					"boost": 5,
+				},
+			},
+		},
+		map[string]interface{}{
+			"match_phrase": map[string]interface{}{
+				"Abstract": map[string]interface{}{
+					"query": keyWord,
+					//"slop": "50",
+					"boost": 5,
+				},
+			},
+		},
+	}
+	mustMap := map[string]interface{}{
+		"bool": map[string]interface{}{
+			"should": shouldSub,
+		},
+	}
+	must = append(must, mustMap)
+
+	filter := []map[string]interface{}{
+		map[string]interface{}{
+			"term": map[string]interface{}{
+				"PublishState": 2,
+			},
+		},
+		map[string]interface{}{
+			"term": map[string]interface{}{
+				"ReportChapterId":0,     //排除章节内容
+			},
+		},
+		map[string]interface{}{
+			"term": map[string]interface{}{
+				"ClassifyIdFirst":classifyIdFirst,
+			},
+		},
+		map[string]interface{}{
+			"terms": map[string]interface{}{
+				"ClassifyIdSecond": classifyIdSeconds,
+			},
+		},
+	}
+	source := map[string]interface{}{
+		"query": map[string]interface{}{
+			"bool": map[string]interface{}{
+				"must": must,
+				"filter": filter,
+			},
+		},
+	}
+
+	source["from"] = (pageIndex - 1) * pageSize
+	source["size"] = pageSize
+	source["highlight"] = map[string]interface{}{
+		"fields": map[string]interface{}{
+			"Title":map[string]interface{}{},
+			"Abstract":map[string]interface{}{},
+			"StageStr":map[string]interface{}{},
+		},
+		"pre_tags" : "<span style=\"color:#efefef\">",
+		"post_tags": "</span>",
+	}
+
+	source["sort"] = []map[string]interface{}{
+		map[string]interface{}{
+			"_score":map[string]interface{}{
+				"order":"desc",
+			},
+		},
+		map[string]interface{}{
+			"PublishTime.keyword":map[string]interface{}{
+				"order":"desc",
+			},
+		},
+	}
+	jsonstr, err := json.Marshal(source)
+	fmt.Printf("%s",jsonstr)
+	request := global.EsClient.Search(indexName).Source(source) // sets the JSON request
+	searchResp, err = request.Do(context.Background())
+	if err != nil {
+		fmt.Print("结果err:")
+		fmt.Println(err.Error())
+		return
+	}
+
+	fmt.Print("结果正常:")
 	fmt.Println(searchResp.Status)
 	fmt.Println(searchResp.Status)
 	if searchResp.Status != 0 {
 	if searchResp.Status != 0 {
 		err = errors.New("查询失败")
 		err = errors.New("查询失败")

+ 95 - 19
services/report/report.go

@@ -28,6 +28,7 @@ import (
 	"time"
 	"time"
 )
 )
 
 
+// GetLatestClassReport 已购列表最新报告
 func GetLatestClassReport(permissionIds []int, userId uint64) (purchaseList []*purchase.PurchaseListItem, err error)  {
 func GetLatestClassReport(permissionIds []int, userId uint64) (purchaseList []*purchase.PurchaseListItem, err error)  {
 	var errMsg string
 	var errMsg string
 	defer func() {
 	defer func() {
@@ -458,7 +459,7 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 		}
 		}
 	}
 	}
 
 
-	if classifyIdSecond > 0 {
+	if classifyIdSecond > 0 && keyWord == "" {
 		classifyInfo, tErr := classify.GetByClassifyId(classifyIdSecond)
 		classifyInfo, tErr := classify.GetByClassifyId(classifyIdSecond)
 		if tErr != nil {
 		if tErr != nil {
 			errMsg = tErr.Error()
 			errMsg = tErr.Error()
@@ -489,6 +490,7 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 	}else{
 	}else{
 		if classifyParent.ClassifyName == "晨报" || classifyParent.ClassifyName == "周报"{
 		if classifyParent.ClassifyName == "晨报" || classifyParent.ClassifyName == "周报"{
 			reportImgUrl =  chartPermissionImageMap[1]
 			reportImgUrl =  chartPermissionImageMap[1]
+			classifyIdSeconds = append(classifyIdSeconds,0)
 		}
 		}
 
 
 		chartPermissions, tErr := chart_permission_search_key_word_mapping.GetChartPermissionByFrom("rddp")
 		chartPermissions, tErr := chart_permission_search_key_word_mapping.GetChartPermissionByFrom("rddp")
@@ -548,24 +550,75 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 		}
 		}
 	}
 	}
 
 
-	if keyWord != "" {
-		condition += ` AND (title LIKE ? OR abstract LIKE ?) `
-		pars = append(pars, "%" + keyWord + "%")
-		pars = append(pars, "%" + keyWord + "%")
-	}
-	offset := (pageIndex - 1) * pageSize
-	list, err := report.GetReportList(condition, pars, offset, pageSize)
-	if err != nil {
-		errMsg = err.Error()
-		err = errors.New("报告查询出错")
-		return
-	}
-	total, err := report.GetReportListCount(condition, pars)
-	if err != nil {
-		errMsg = err.Error()
-		err = errors.New("查询报告总数出错")
-		return
+	var total int64
+	var list []*report.Report
+	if keyWord == "" {
+		offset := (pageIndex - 1) * pageSize
+		list, err = report.GetReportList(condition, pars, offset, pageSize)
+		if err != nil {
+			errMsg = err.Error()
+			err = errors.New("报告查询出错")
+			return
+		}
+		total, err = report.GetReportListCount(condition, pars)
+		if err != nil {
+			errMsg = err.Error()
+			err = errors.New("查询报告总数出错")
+			return
+		}
+	}else{
+		if classifyIdSecond > 0 {
+			classifyIdSeconds = append(classifyIdSeconds, classifyIdSecond)
+		}
+		if classifyParent.ClassifyName == "晨报" || classifyParent.ClassifyName == "周报" {
+			classifyIdSeconds = append(classifyIdSeconds,0)
+		}
+		searchResp, total1, tErr := elasticService.ReportListSearch(keyWord, classifyIdFirst, classifyIdSeconds, pageIndex, pageSize)
+		if tErr != nil {
+			errMsg = tErr.Error()
+			err = errors.New("查询失败")
+			return
+		}
+		total = int64(total1)
+		if searchResp.Hits != nil {
+			for _, v := range searchResp.Hits.Hits {
+				temp := new(report.Report)
+				itemJson, tmpErr := v.Source.MarshalJSON()
+				if tmpErr != nil {
+					errMsg = tmpErr.Error()
+					err = errors.New("解析出错")
+					return
+				}
+				reportItem := new(response.EsReportItem)
+				tmpErr = json.Unmarshal(itemJson, &reportItem)
+				if tmpErr != nil {
+					errMsg = tmpErr.Error()
+					err = errors.New("解析json出错")
+					return
+				}
+				temp.Id = reportItem.ReportId
+				temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
+				temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
+				temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
+				temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
+				temp.Abstract = reportItem.Abstract
+				temp.StageStr = reportItem.StageStr
+				temp.Title = reportItem.Title
+				temp.PublishTime, err = time.Parse("2006-01-02 15:04:05", reportItem.PublishTime)
+				if len(v.Highlight["Title"]) > 0 {
+					temp.Title = v.Highlight["Title"][0]
+				}
+				if len(v.Highlight["Abstract"]) > 0 {
+					temp.Title = v.Highlight["Abstract"][0]
+				}
+				if len(v.Highlight["StageStr"]) > 0 {
+					temp.Title = v.Highlight["StageStr"][0]
+				}
+				list = append(list, temp)
+			}
+		}
 	}
 	}
+
 	var reportList []*response.ReportListItem
 	var reportList []*response.ReportListItem
 	if len(list) > 0 {
 	if len(list) > 0 {
 		for _, reportInfo := range list {
 		for _, reportInfo := range list {
@@ -580,8 +633,14 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 			reportItem.Stage = reportInfo.Stage
 			reportItem.Stage = reportInfo.Stage
 			reportItem.Abstract = reportInfo.Abstract
 			reportItem.Abstract = reportInfo.Abstract
 			reportItem.Author = reportInfo.Author
 			reportItem.Author = reportInfo.Author
+			stageStr := reportInfo.StageStr
+			if stageStr == "" {
+				stageStr = strconv.Itoa(reportInfo.Stage)
+			}
+			reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】%s", stageStr, reportItem.ClassifyNameSecond, reportItem.Title)
 			if reportInfo.ClassifyNameFirst == "晨报" || reportInfo.ClassifyNameFirst == "周报" || classifyIdSecond > 0{
 			if reportInfo.ClassifyNameFirst == "晨报" || reportInfo.ClassifyNameFirst == "周报" || classifyIdSecond > 0{
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + reportImgUrl
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + reportImgUrl
+				reportItem.TitleInfo = fmt.Sprintf("【第%s期|FICC|%s】%s", stageStr, reportItem.ClassifyNameFirst, reportItem.Title)
 			} else if url,ok := permissionImageMap[reportInfo.ClassifyNameSecond]; ok {
 			} else if url,ok := permissionImageMap[reportInfo.ClassifyNameSecond]; ok {
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + url
 				reportItem.ReportImgUrl = utils.ALIYUN_YBIMG_HOST + url
 			}
 			}
@@ -590,6 +649,7 @@ func GetReportList(user user.UserInfo, keyWord string, classifyIdFirst, classify
 				reportItem.VideoUrl = reportInfo.VideoUrl
 				reportItem.VideoUrl = reportInfo.VideoUrl
 				reportItem.AuthOk = authOk
 				reportItem.AuthOk = authOk
 			}
 			}
+
 			reportList = append(reportList, reportItem)
 			reportList = append(reportList, reportItem)
 		}
 		}
 
 
@@ -694,7 +754,23 @@ func SearchReport(user user.UserInfo, keyWord string, pageIndex, pageSize int)(r
 			global.LOG.Critical(fmt.Sprintf("SearchReport: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
 			global.LOG.Critical(fmt.Sprintf("SearchReport: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
 		}
 		}
 	}()
 	}()
-	searchResp, total, err := elasticService.SearchReport(keyWord, pageIndex, pageSize)
+	//查询正常状态的分类
+	classifys, err := classify.GetSimpleAll()
+	if err != nil {
+		errMsg = err.Error()
+		err = errors.New("分类查询出错")
+		return
+	}
+	var classifyIdFirsts []int
+	classifyIdSeconds := []int{0}
+	for _, v := range classifys{
+		if v.ParentId == 0 {
+			classifyIdFirsts = append(classifyIdFirsts, v.Id)
+		}else{
+			classifyIdSeconds = append(classifyIdSeconds, v.Id)
+		}
+	}
+	searchResp, total, err := elasticService.SearchReport(keyWord, classifyIdFirsts, classifyIdSeconds, pageIndex, pageSize)
 	if err != nil {
 	if err != nil {
 		errMsg = err.Error()
 		errMsg = err.Error()
 		err = errors.New("查询失败")
 		err = errors.New("查询失败")