Browse Source

fix:搜索兼容pdf研报

zqbao 9 tháng trước cách đây
mục cha
commit
24ca6d444d
6 tập tin đã thay đổi với 68 bổ sung6 xóa
  1. 2 0
      models/report.go
  2. 4 0
      models/response/report.go
  3. 37 1
      services/elastic/report.go
  4. 14 5
      services/report.go
  5. 5 0
      utils/config.go
  6. 6 0
      utils/constants.go

+ 2 - 0
models/report.go

@@ -179,4 +179,6 @@ type ReportCollectListItem struct {
 	Abstract            string    `description:"摘要"`
 	Stage               string    `description:"期数"`
 	Author              string    `description:"作者"`
+	PdfUrl              string    `description:"pdf文件url"`
+	ReportType          int       `description:"报告类型:1:eta报告,2:小程序pdf报告"`
 }

+ 4 - 0
models/response/report.go

@@ -26,6 +26,8 @@ type ReportSearchResp struct {
 }
 
 type EsReportItem struct {
+	ReportPdfId        int    `description:"pdf文件id"`
+	PdfUrl             string `description:"pdf文件URL"`
 	Author             string `description:"作者"`
 	BodyContent        string `description:"内容"`
 	Categories         string `description:"品种名称"`
@@ -34,10 +36,12 @@ type EsReportItem struct {
 	ClassifyIdSecond   int    `description:"二级分类id"`
 	ClassifyNameSecond string `description:"二级分类名称"`
 	PublishState       int    `description:"1:未发布,2:已发布"`
+	State              int    `description:"pdf报告:1:已发布,2:未发布"`
 	PublishTime        string `description:"发布时间"`
 	ReportChapterId    int    `description:"报告章节Id"`
 	ReportId           int    `description:"报告Id"`
 	Title              string `description:"标题"`
 	Abstract           string `description:"摘要"`
 	StageStr           string `description:"期数"`
+	Stage              int    `description:"pdf期数"`
 }

+ 37 - 1
services/elastic/report.go

@@ -120,7 +120,8 @@ func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []in
 	fmt.Printf("%s", jsonstr)
 
 	request := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
-
+	// request := utils.EsClient.Search().Index(indice...).Source(source) // sets the JSON request
+	fmt.Println(source)
 	searchResp, err = request.Do(context.Background())
 	if err != nil {
 		fmt.Print("结果err:")
@@ -137,6 +138,41 @@ func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []in
 	return
 }
 
+func SearchReportV2(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
+	indices := []string{utils.EsReportIndexName, utils.MINI_REPORT_INDEX_NAME}
+	query := elastic.NewBoolQuery()
+	query = query.Must(elastic.NewMatchPhraseQuery("Title", keyWord)).Boost(5)
+	query = query.Should(elastic.NewMatchPhraseQuery("State", 1))
+	query = query.Should(elastic.NewMatchQuery("PublishState", 2))
+	sort := elastic.NewFieldSort("PublishTime.keyword").Desc()
+	searchResp, err = utils.EsClient.Search().
+		Index(indices...).
+		Query(query).
+		SortBy(sort).
+		Highlight(elastic.NewHighlight().
+			PreTags("<span class=\"report-title_color\">").
+			PostTags("</span>").
+			Field("Title")).
+		From((pageIndex - 1) * pageSize).
+		Size(pageSize).
+		Pretty(true).
+		Do(context.Background())
+	// searchResp, err = request.Do(context.Background())
+	if err != nil {
+		fmt.Print("结果err:")
+		fmt.Println(err.Error())
+		return
+	}
+
+	fmt.Print("结果正常:")
+	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 := utils.EsReportIndexName

+ 14 - 5
services/report.go

@@ -8,6 +8,7 @@ import (
 	"eta/eta_mini_bridge/services/elastic"
 	"eta/eta_mini_bridge/utils"
 	"html"
+	"strconv"
 	"strings"
 	"time"
 
@@ -52,7 +53,7 @@ func addAliasToKeyword(keyword string) string {
 func SearchReport(keyWord string, pageIndex, pageSize int) (ret *response.ReportSearchResp, err error, errMsg string) {
 	//查询正常状态的分类
 	keyWord = addAliasToKeyword(keyWord)
-	searchResp, total, err := elastic.SearchReport(keyWord, []int{}, []int{}, pageIndex, pageSize)
+	searchResp, total, err := elastic.SearchReportV2(keyWord, []int{}, []int{}, pageIndex, pageSize)
 	if err != nil {
 		errMsg = err.Error()
 		err = errors.New("查询失败")
@@ -75,18 +76,26 @@ func SearchReport(keyWord string, pageIndex, pageSize int) (ret *response.Report
 				err = errors.New("解析json出错")
 				return
 			}
-			temp.ReportId = reportItem.ReportId
+			if reportItem.ReportPdfId != 0 {
+				temp.ReportId = reportItem.ReportPdfId
+				temp.ReportType = utils.ReportTypePdf
+				temp.Stage = strconv.Itoa(reportItem.Stage)
+			} else {
+				temp.ReportId = reportItem.ReportId
+				temp.ReportType = utils.ReportTypeEta
+				temp.Stage = reportItem.StageStr
+				temp.ContentSub = reportItem.BodyContent
+			}
+			temp.PdfUrl = reportItem.PdfUrl
 			temp.ReportChapterId = reportItem.ReportChapterId
 			temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
 			temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
 			temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
 			temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
-			temp.Title = reportItem.Title
-			temp.ContentSub = reportItem.BodyContent
 			temp.PublishTime, err = time.ParseInLocation(utils.FormatDateTime, reportItem.PublishTime, time.Local)
+			temp.Title = reportItem.Title
 			temp.Abstract = reportItem.Abstract
 			temp.Author = reportItem.Author
-			temp.Stage = reportItem.StageStr
 
 			if len(v.Highlight["Title"]) > 0 {
 				temp.Title = v.Highlight["Title"][0]

+ 5 - 0
utils/config.go

@@ -57,6 +57,7 @@ var (
 
 // ES索引配置
 var (
+	MINI_REPORT_INDEX_NAME         string // 小程序的pdf报告索引
 	EsReportIndexName              string //研报ES索引
 	EsSemanticAnalysisDocIndexName string //ES语义分析文档索引名
 	SmartReportIndexName           string //智能研报ES索引
@@ -123,6 +124,10 @@ func init() {
 	}
 	// ES 索引
 	{
+		MINI_REPORT_INDEX_NAME = config["mini_report_index_name"]
+		if MINI_REPORT_INDEX_NAME == "" {
+			MINI_REPORT_INDEX_NAME = "test_dw_mini_pdf_report_v1"
+		}
 		EsReportIndexName = config["es_report_index_name"]
 		EsSemanticAnalysisDocIndexName = config["es_semantic_analysis_doc_index_name"]
 		SmartReportIndexName = config["es_smart_report_index_name"]

+ 6 - 0
utils/constants.go

@@ -38,3 +38,9 @@ const (
 	UserStatusPotential = 1 //潜在用户
 	UserStatusFormal    = 2 //正式用户
 )
+
+// 报告类型
+const (
+	ReportTypeEta = 1 // eta的研报
+	ReportTypePdf = 2 // pdf的研报
+)