Browse Source

Es搜索分页,报告搜索、资源包搜索接口

xingzai 2 years ago
parent
commit
616cc470bc
7 changed files with 452 additions and 16 deletions
  1. 132 0
      controllers/report.go
  2. 20 16
      controllers/search.go
  3. 1 0
      models/article.go
  4. 71 0
      models/report.go
  5. 9 0
      routers/commentsRouter.go
  6. 137 0
      services/elastic.go
  7. 82 0
      services/report.go

+ 132 - 0
controllers/report.go

@@ -939,3 +939,135 @@ func (this *MobileReportController) SearchResource() {
 	br.Msg = "获取成功"
 	br.Msg = "获取成功"
 	br.Data = resp
 	br.Data = resp
 }
 }
+
+// @Title 报告搜索、资源包搜索接口
+// @Description 报告搜索、资源包搜索接口接口
+// @Param   KeyWord   query   string  true       "搜索关键词"
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
+// @Success 200 {object} models.SearchReportAndResourceResp
+// @router /searchReportAndResource [get]
+func (this *MobileReportController) SearchReportAndResource() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	user := this.User
+	if user == nil {
+		br.Msg = "请重新登录"
+		br.Ret = 408
+		return
+	}
+	uid := user.UserId
+	keyWord := this.GetString("KeyWord")
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	var total int
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = paging.StartIndex(currentIndex, pageSize)
+	page := paging.GetPaging(currentIndex, pageSize, total)
+	var condition string
+	var conditionSql string
+	//匹配报告标题、
+	condition = ` AND ( a.title LIKE '%` + keyWord + `%' OR  a.body LIKE '%` + keyWord + `%') AND a.publish_status = 1 `
+	conditionSql = ` AND a.article_id <  ` + strconv.Itoa(utils.SummaryArticleId) + condition + ` OR ( article_type = 'lyjh' ` + condition + ` ) `
+	total, err := models.GetReoprtSearchCount(conditionSql)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.Msg = "获取总数失败,Err:" + err.Error()
+		return
+	}
+	ListHzReport, err := services.GetReoprtSearchListHz(conditionSql, user.UserId, startSize, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,GetReoprtSearchListHz Err:" + err.Error()
+		return
+	}
+	if len(ListHzReport) == 0 {
+		ListHzReport = make([]*models.ArticleCollectionResp, 0)
+	}
+
+	fllowList, err := models.GetUserFllowIndustrialList(uid)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,GetUserFllowIndustrialList Err:" + err.Error()
+		return
+	}
+	fllowMap := make(map[int]int)
+	if len(fllowList) > 0 {
+		for _, v := range fllowList {
+			fllowMap[v.IndustrialManagementId] = v.IndustrialManagementId
+		}
+	}
+
+	var conditionOr string
+	conditionOr += ` OR ( m.subject_names LIKE '%` + keyWord + `%' AND a.article_id < ` + strconv.Itoa(utils.SummaryArticleId) + `  AND publish_status = 1 ) `
+	condition = ` AND m.industry_name LIKE '%` + keyWord + `%' AND a.article_id < ` + strconv.Itoa(utils.SummaryArticleId) + conditionOr
+
+	listHzResource, err := models.GetSearchResourceListHz(condition, 0, pageSize)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取品种信息失败,Err:" + err.Error()
+		return
+	}
+
+	conditionOr = ` OR ( m.subject_names LIKE '%` + keyWord + `%' AND a.article_id >= ` + strconv.Itoa(utils.SummaryArticleId) + `  AND publish_status = 1 ) `
+	condition = ` AND m.industry_name LIKE '%` + keyWord + `%' AND a.article_id >= ` + strconv.Itoa(utils.SummaryArticleId) + conditionOr
+
+	//合并产业关联的标的
+	listSubjcet, err := models.GetThemeHeatSubjectList("")
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取标的信息失败,Err:" + err.Error()
+		return
+	}
+	industrialIdArr := make([]int, 0)
+	for k, v := range listHzResource {
+		listHzResource[k].Source = 1
+		if fllowMap[v.IndustrialManagementId] > 0 {
+			listHzResource[k].IsFollw = true
+		}
+		industrialIdArr = append(industrialIdArr, v.IndustrialManagementId)
+	}
+
+	if len(industrialIdArr) > 0 {
+		//合并产业关联的标的
+		listSubjcet, err = models.GetIndustrialSubjectAllByIndustrialId(industrialIdArr)
+		if err != nil {
+			br.Msg = "获取信息失败"
+			br.ErrMsg = "获取标的信息失败,Err:" + err.Error()
+			return
+		}
+		mapIndustrial := make(map[string]int)
+		for _, v := range listSubjcet {
+			for k2, v2 := range listHzResource {
+				if v2.IndustrialManagementId == v.IndustrialManagementId && mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] == 0 {
+					listHzResource[k2].IndustrialSubjectList = append(listHzResource[k2].IndustrialSubjectList, v)
+					mapIndustrial[fmt.Sprint(v2.IndustrialManagementId, "Hz", v.SubjectName)] = v2.IndustrialManagementId
+				}
+			}
+		}
+	}
+	resp := new(models.SearchReportAndResourceResp)
+	if len(listHzResource) == 0 {
+		listHzResource = make([]*models.IndustrialManagementHotResp, 0)
+	}
+	if keyWord != "" {
+		go services.AddUserSearchLog(user, keyWord, 5)
+	}
+	page = paging.GetPaging(currentIndex, pageSize, total)
+	resp.Paging = page
+	resp.ListHzResource = listHzResource
+	resp.ListHzReport = ListHzReport
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 20 - 16
controllers/search.go

@@ -92,8 +92,8 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 	if orderColumn == "" {
 	if orderColumn == "" {
 		orderColumn = "Matching"
 		orderColumn = "Matching"
 	}
 	}
-	indexName := utils.IndexName
-	pageSize = 20
+	//indexName := utils.IndexName
+	//pageSize = 20
 	var chartTotal int
 	var chartTotal int
 	resp := new(models.SearchResp)
 	resp := new(models.SearchResp)
 	//page := paging.GetPaging(currentIndex, pageSize, total)
 	//page := paging.GetPaging(currentIndex, pageSize, total)
@@ -102,9 +102,9 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 	var condition string
 	var condition string
 	var pars []interface{}
 	var pars []interface{}
 	if listType == 1 || listType == 3 {
 	if listType == 1 || listType == 3 {
-		if listType == 1 {
-			pageSize = 100
-		}
+		//if listType == 1 {
+		//	pageSize = 100
+		//}
 		if currentIndex <= 2 {
 		if currentIndex <= 2 {
 			condition = ` AND title LIKE '%` + keyWord + `%'`
 			condition = ` AND title LIKE '%` + keyWord + `%'`
 			chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
 			chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
@@ -161,17 +161,21 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 	var result []*models.SearchItem
 	var result []*models.SearchItem
 	var total int64
 	var total int64
 	if listType == 1 || listType == 2 {
 	if listType == 1 || listType == 2 {
-		if orderColumn == "PublishDate" {
-			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
-			result = tmpResult
-			total = tmpTotal
-			err = tmpErr
-		} else {
-			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
-			result = tmpResult
-			total = tmpTotal
-			err = tmpErr
-		}
+		//if orderColumn == "PublishDate" {
+		//	tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
+		//	result = tmpResult
+		//	total = tmpTotal
+		//	err = tmpErr
+		//} else {
+		//	tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
+		//	result = tmpResult
+		//	total = tmpTotal
+		//	err = tmpErr
+		//}
+		tmpResult, tmpTotal, tmpErr := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn)
+		result = tmpResult
+		total = tmpTotal
+		err = tmpErr
 		if err != nil {
 		if err != nil {
 			br.Msg = "检索失败"
 			br.Msg = "检索失败"
 			br.ErrMsg = "检索失败,Err:" + err.Error()
 			br.ErrMsg = "检索失败,Err:" + err.Error()

+ 1 - 0
models/article.go

@@ -118,6 +118,7 @@ type ArticleCollectionResp struct {
 	CategoryId      string                      `description:"文章分类"`
 	CategoryId      string                      `description:"文章分类"`
 	HttpUrl         string                      `description:"文章链接跳转地址"`
 	HttpUrl         string                      `description:"文章链接跳转地址"`
 	IsNeedJump      bool                        `description:"是否需要跳转链接地址"`
 	IsNeedJump      bool                        `description:"是否需要跳转链接地址"`
+	MyCollectNum    int                         `description:"本人是否收藏数量"`
 }
 }
 
 
 //列表
 //列表

+ 71 - 0
models/report.go

@@ -3,6 +3,7 @@ package models
 import (
 import (
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"github.com/rdlucklib/rdluck_tools/paging"
+	"strconv"
 )
 )
 
 
 type IndustrialManagementList struct {
 type IndustrialManagementList struct {
@@ -190,3 +191,73 @@ func GetNewIndustrialUserRecordArticle(industrialManagementId, categoryId int) (
 	err = o.Raw(sql, industrialManagementId, categoryId).QueryRow(&item)
 	err = o.Raw(sql, industrialManagementId, categoryId).QueryRow(&item)
 	return
 	return
 }
 }
+
+//列表
+func GetReoprtSearchListHz(condition string, userId int) (items []*ArticleCollectionResp, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			a.article_id,
+			a.title,
+			a.annotation,
+			date_format( a.publish_date, '%Y-%m-%d' ) AS publish_date,
+			m.industry_name,
+			m.industrial_management_id,
+			( SELECT count( 1 ) FROM cygx_article_history_record_newpv AS h WHERE h.article_id = a.article_id ) AS pv,
+			( SELECT count( 1 ) FROM cygx_article_collect AS ac WHERE ac.article_id = a.article_id ) AS collect_num,
+			( SELECT count( 1 ) FROM cygx_article_collect AS ac WHERE ac.article_id = a.article_id  AND user_id = ?) AS my_collect_num
+		FROM
+			cygx_article AS a
+			LEFT JOIN cygx_industrial_article_group_management AS mg ON mg.article_id = a.article_id
+			LEFT JOIN cygx_industrial_management AS m ON m.industrial_management_id = mg.industrial_management_id
+		WHERE
+			1 = 1 `
+	if condition != "" {
+		sql += condition
+	}
+	_, err = o.Raw(sql, userId).QueryRows(&items)
+	return
+} //end
+
+//产业列表
+func GetSearchResourceListHz(condition string, startSize, pageSize int) (items []*IndustrialManagementHotResp, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			m.industry_name,
+			m.industrial_management_id,
+ 			MAX( a.publish_date ) as publish_date_order,
+			date_format( MAX( a.publish_date ), '%Y-%m-%d' ) AS publish_date 
+		FROM
+			cygx_industrial_management AS m
+			INNER JOIN cygx_industrial_article_group_management AS mg ON mg.industrial_management_id = m.industrial_management_id
+			INNER JOIN cygx_article AS a ON a.article_id = mg.article_id  AND  a.article_type != 'lyjh'
+		WHERE
+			1 = 1
+			AND publish_status = 1 ` + condition + `  GROUP BY m.industrial_management_id  ORDER BY publish_date_order DESC `
+
+	if startSize > 0 || pageSize > 0 {
+		sql += ` LIMIT ` + strconv.Itoa(startSize) + "," + strconv.Itoa(pageSize)
+	}
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
+//用户收藏榜start
+type IndustrialManagementHotResp struct {
+	IndustrialManagementId int                  `orm:"column(industrial_management_id);pk" description:"产业id"`
+	IndustryName           string               `description:"产业名称"`
+	IsFollw                bool                 `description:"是否关注"`
+	FllowNum               int                  `description:"关注数量"`
+	IsNew                  bool                 `description:"是否新标签"`
+	IsHot                  bool                 `description:"是否新标签"`
+	PublishDate            string               `description:"发布时间"`
+	ArticleReadNum         int                  `description:"文章阅读数量"`
+	Source                 int                  `description:"来源 1:弘则资源包(报告)、2:研选主题(报告)"`
+	IndustrialSubjectList  []*IndustrialSubject `description:"标的列表"`
+}
+
+//搜索资源包 start
+type SearchReportAndResourceResp struct {
+	ListHzResource []*IndustrialManagementHotResp `description:"弘则资源包"`
+	ListHzReport   []*ArticleCollectionResp       `description:"弘则报告"`
+	Paging         *paging.PagingItem             `description:"弘则报告分页"`
+}

+ 9 - 0
routers/commentsRouter.go

@@ -115,6 +115,15 @@ func init() {
             Filters: nil,
             Filters: nil,
             Params: nil})
             Params: nil})
 
 
+    beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"] = append(beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"],
+        beego.ControllerComments{
+            Method: "SearchReportAndResource",
+            Router: `/searchReportAndResource`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"] = append(beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"],
     beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"] = append(beego.GlobalControllerRouter["hongze/hongze_clpt/controllers:MobileReportController"],
         beego.ControllerComments{
         beego.ControllerComments{
             Method: "SearchResource",
             Method: "SearchResource",

+ 137 - 0
services/elastic.go

@@ -238,3 +238,140 @@ func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize
 	}
 	}
 	return
 	return
 }
 }
+
+func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
+	indexName := utils.IndexName
+	client := utils.Client
+	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
+	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
+	keyWordLen := len(keyWordArr)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
+		keyWordLen = len(keyWordArr)
+	}
+	mustMap := make([]interface{}, 0)
+	shouldMap := make(map[string]interface{}, 0)
+	shouldMapquery := make([]interface{}, 0)
+	// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
+	//keyWordWeight := GetWeight(keyWordLen)
+	lenkeyWordArr := len(keyWordArr)
+	for k, v := range keyWordArr {
+		if v != "" {
+			shouldMapquery = append(shouldMapquery, map[string]interface{}{
+				"function_score": map[string]interface{}{
+					"query": map[string]interface{}{
+						"multi_match": map[string]interface{}{
+							"boost":  (lenkeyWordArr - k) * 100, //给查询的值赋予权重
+							"fields": []interface{}{"Title"},
+							"query":  v,
+						},
+					},
+				},
+			})
+
+			shouldMapquery = append(shouldMapquery, map[string]interface{}{
+				"function_score": map[string]interface{}{
+					"query": map[string]interface{}{
+						"multi_match": map[string]interface{}{
+							"boost":  (lenkeyWordArr-k)*100 - 1, //给查询的值赋予权重
+							"fields": []interface{}{"BodyText"},
+							"query":  v,
+						},
+					},
+				},
+			})
+		}
+	}
+	shouldMap = map[string]interface{}{
+		"should": shouldMapquery,
+	}
+	//排序
+	sortMap := make([]interface{}, 0)
+	//时间
+	sortMap = append(sortMap, map[string]interface{}{
+		"PublishDate": map[string]interface{}{
+			"order": "desc",
+		},
+	})
+	//高亮
+	highlightMap := make(map[string]interface{}, 0)
+	highlightMap = map[string]interface{}{
+		"fields": map[string]interface{}{
+			"BodyText": map[string]interface{}{},
+			"Title":    map[string]interface{}{},
+		},
+		//样式 红色
+		"post_tags": []interface{}{"</font>"},
+		"pre_tags":  []interface{}{"<font color='red'>"},
+	}
+
+	mustMap = append(mustMap, map[string]interface{}{
+		"bool": shouldMap,
+	})
+	queryMap := map[string]interface{}{
+		"query": map[string]interface{}{
+			"bool": map[string]interface{}{
+				"must": mustMap,
+			},
+		},
+	}
+	if orderColumn == "PublishDate" {
+		queryMap["sort"] = sortMap
+	}
+	queryMap["from"] = startSize
+	queryMap["size"] = pageSize
+	queryMap["highlight"] = highlightMap
+	jsonBytes, _ := json.Marshal(queryMap)
+	fmt.Println(string(jsonBytes))
+	request := client.Search(indexName).Source(queryMap) // sets the JSON request
+	searchByMatch, err := request.Do(context.Background())
+	if searchByMatch != nil {
+		if searchByMatch.Hits != nil {
+			for _, v := range searchByMatch.Hits.Hits {
+				var isAppend bool
+				articleJson, err := v.Source.MarshalJSON()
+				if err != nil {
+					return nil, 0, err
+				}
+				article := new(models.CygxArticleEs)
+				err = json.Unmarshal(articleJson, &article)
+				if err != nil {
+					return nil, 0, err
+				}
+				searchItem := new(models.SearchItem)
+				searchItem.ArticleId, _ = strconv.Atoi(v.Id)
+				if len(v.Highlight["BodyText"]) > 0 {
+					searchItem.Body = v.Highlight["BodyText"]
+				} else {
+					bodyRune := []rune(article.BodyText)
+					bodyRuneLen := len(bodyRune)
+					if bodyRuneLen > 100 {
+						bodyRuneLen = 100
+					}
+					body := string(bodyRune[:bodyRuneLen])
+					searchItem.Body = []string{body}
+				}
+				var title string
+				if len(v.Highlight["Title"]) > 0 {
+					title = v.Highlight["Title"][0]
+				} else {
+					title = article.Title
+				}
+				searchItem.Title = title
+				searchItem.PublishDate = article.PublishDate
+				searchItem.ExpertBackground = article.ExpertBackground
+				searchItem.CategoryId = article.CategoryId
+				for _, v_result := range result {
+					if v_result.ArticleId == searchItem.ArticleId {
+						isAppend = true
+					}
+				}
+				if !isAppend {
+					result = append(result, searchItem)
+				}
+			}
+		}
+		total = searchByMatch.Hits.TotalHits.Value
+	}
+	return
+}

+ 82 - 0
services/report.go

@@ -0,0 +1,82 @@
+package services
+
+import (
+	"hongze/hongze_clpt/models"
+	"hongze/hongze_clpt/utils"
+	"strconv"
+	"strings"
+)
+
+func GetReoprtSearchListHz(condition string, userId, startSize, pageSize int) (items []*models.ArticleCollectionResp, err error) {
+	listHz, err := models.GetReoprtSearchList(condition, userId, startSize, pageSize)
+	if err != nil {
+		return
+	}
+	for k, _ := range listHz {
+		listHz[k].Source = 1
+	}
+	var articleIds string
+	for _, v := range listHz {
+		articleIds += strconv.Itoa(v.ArticleId) + ","
+	}
+	articleIds = strings.TrimRight(articleIds, ",")
+	silcearticleIds := strings.Split(articleIds, ",")
+	//获取文章关联的产业
+	var pars []interface{}
+	pars = make([]interface{}, 0)
+	articleIdList := make([]string, 0)
+	for _, v := range silcearticleIds {
+		articleIdList = append(articleIdList, v)
+	}
+	condition = ` AND mg.article_id IN (  ` + utils.GetOrmInReplace(len(silcearticleIds)) + ` )  `
+	pars = append(pars, articleIdList)
+	industrialList, err := models.GetIndustrialListByarticleId(pars, condition)
+	if err != nil {
+		return
+	}
+	industrialMap := make(map[int][]*models.IndustrialManagementResp)
+	if len(industrialList) > 0 {
+		for _, v := range industrialList {
+			item := new(models.IndustrialManagementResp)
+			//item.ArticleId = v.ArticleId
+			item.IndustrialManagementId = v.IndustrialManagementId
+			item.IndustryName = v.IndustryName
+			item.ChartPermissionId = v.ChartPermissionId
+			industrialMap[v.ArticleId] = append(industrialMap[v.ArticleId], item)
+		}
+	}
+
+	detailCategoryUrl, err := models.GetConfigByCode("category_map_img_url")
+	if err != nil {
+		return
+	}
+	categoryUrlList := strings.Split(detailCategoryUrl.ConfigValue, "{|}")
+	mapCategoryUrl := make(map[string]string)
+	var categoryId string
+	var imgUrlChart string
+	for _, v := range categoryUrlList {
+		vslice := strings.Split(v, "_")
+		categoryId = vslice[0]
+		imgUrlChart = vslice[len(vslice)-1]
+		mapCategoryUrl[categoryId] = imgUrlChart
+	}
+
+	for k, v := range listHz {
+		if len(industrialMap[v.ArticleId]) > 0 {
+			listHz[k].List = industrialMap[v.ArticleId]
+		} else {
+			listHz[k].List = make([]*models.IndustrialManagementResp, 0)
+		}
+		listHz[k].Body = ""
+		listHz[k].Abstract, _ = GetReportContentTextSub(v.Abstract)
+		listHz[k].Annotation, _ = GetReportContentTextSub(v.Annotation)
+		//行业比较研究、资金流向,显示报告的摘要
+		if listHz[k].CategoryName == "行业比较研究" || listHz[k].CategoryName == "资金流向" {
+			listHz[k].Annotation = listHz[k].Abstract
+		}
+		listHz[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
+		listHz[k].Body = ""
+	}
+	items = listHz
+	return
+}