浏览代码

报告查询,报告搜索

xiexiaoyuan 2 年之前
父节点
当前提交
f51d25f3ff

+ 126 - 2
controller/english_report/english_report.go

@@ -1,12 +1,15 @@
 package english_report
 
 import (
+	"encoding/json"
 	"github.com/gin-gonic/gin"
 	"github.com/go-playground/validator/v10"
 	"hongze/hongze_yb_en_api/controller/resp"
 	"hongze/hongze_yb_en_api/global"
 	"hongze/hongze_yb_en_api/models/base"
+	"hongze/hongze_yb_en_api/models/english_classify"
 	"hongze/hongze_yb_en_api/models/english_report"
+	elasticService "hongze/hongze_yb_en_api/services/elastic"
 	"hongze/hongze_yb_en_api/utils"
 )
 
@@ -27,12 +30,12 @@ func (er *EnglishReportController) List(c *gin.Context) {
 	page := new(base.Page)
 	page.SetPageSize(req.PageSize)
 	page.SetCurrent(req.Current)
-	item := new(english_report.EnglishReport)
+	item := new(english_report.Report)
 
 	condition := " state=2"
 	var pars []interface{}
 	var total int64
-	var tmpList []*english_report.EnglishReport
+	var tmpList []*english_report.Report
 	var list []*english_report.ListItem
 
 	if req.ClassifyIdFirst != "" {
@@ -81,4 +84,125 @@ func (er *EnglishReportController) List(c *gin.Context) {
 	baseData.SetPage(page)
 	baseData.SetList(list)
 	resp.OkData("获取成功", baseData, c)
+}
+
+func (er *EnglishReportController) Classify(c *gin.Context) {
+	item := new(english_classify.Classify)
+	parentList, err := item.GetParent()
+	if err != nil {
+		resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
+		return
+	}
+	childList, err := item.GetChild()
+	if err != nil {
+		resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
+		return
+	}
+	parentMap := make(map[int][]*english_classify.ClassifyItem, 0)
+	for _, v := range childList {
+		tmp := &english_classify.ClassifyItem{
+			Id:            v.Id,
+			ClassifyName:  v.ClassifyName,
+			Sort:          v.Sort,
+			ParentId:      v.ParentId,
+			ClassifyLabel: v.ClassifyLabel,
+			ShowType:      v.ShowType,
+			IsShow:        v.IsShow,
+			CreateTime:    utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
+			ModifyTime:    utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
+		}
+		parentMap[v.ParentId] = append(parentMap[v.ParentId], tmp)
+	}
+	list := make([]*english_classify.ClassifyListItem, 0)
+	for _, v := range parentList {
+		tmp := &english_classify.ClassifyListItem{
+			Id:            v.Id,
+			ClassifyName:  v.ClassifyName,
+			Sort:          v.Sort,
+			ParentId:      v.ParentId,
+			ClassifyLabel: v.ClassifyLabel,
+			ShowType:      v.ShowType,
+			IsShow:        v.IsShow,
+			CreateTime:    utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
+			ModifyTime:    utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
+		}
+		if child, ok := parentMap[v.Id]; ok {
+			tmp.Child = child
+		}
+		list = append(list, tmp)
+	}
+	baseData := new(base.BaseOnlyData)
+	baseData.SetList(list)
+	resp.OkData("获取成功", baseData, c)
+}
+
+func (er *EnglishReportController) FilterByEs(c *gin.Context) {
+	req := new(english_report.ReportSearchReq)
+	err := c.BindQuery(&req)
+	if err != nil {
+		errs, ok := err.(validator.ValidationErrors)
+		if !ok {
+			resp.FailData("参数解析失败", "Err:"+err.Error(), c)
+			return
+		}
+		resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
+		return
+	}
+	page := new(base.Page)
+	page.SetPageSize(req.PageSize)
+	page.SetCurrent(req.Current)
+
+	if req.KeyWord == "" {
+		resp.FailMsg("请输入搜索词", "", c)
+		return
+	}
+
+	reportList := make([]*english_report.SearchEnglishReportItem, 0)
+	searchResp, total, err := elasticService.SearchESEnglishReport(req.KeyWord, req.Current, req.PageSize)
+	if err != nil {
+		resp.FailMsg("报告搜索失败", "报告搜索失败,Err:" + err.Error(), c)
+		return
+	}
+	if searchResp.Hits != nil {
+		for _, v := range searchResp.Hits.Hits {
+			temp := new(english_report.SearchEnglishReportItem)
+			itemJson, tmpErr := v.Source.MarshalJSON()
+			if tmpErr != nil {
+				resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:" + tmpErr.Error(), c)
+				return
+			}
+			reportItem := new(english_report.ElasticEnglishReportDetail)
+			tmpErr = json.Unmarshal(itemJson, &reportItem)
+			if tmpErr != nil {
+				resp.FailMsg("报告搜索失败 解析出错", "报告搜索失败,Err:" + tmpErr.Error(), c)
+				return
+			}
+			temp.Id = reportItem.ReportId
+			temp.ClassifyIdFirst = reportItem.ClassifyIdFirst
+			temp.ClassifyNameFirst = reportItem.ClassifyNameFirst
+			temp.ClassifyIdSecond = reportItem.ClassifyIdSecond
+			temp.ClassifyNameSecond = reportItem.ClassifyNameSecond
+			temp.Title = reportItem.Title
+			temp.Author = reportItem.Author
+			temp.CreateTime = reportItem.CreateTime
+			// 默认应该从数据库中查询
+			temp.ContentSub = reportItem.ContentSub
+			temp.PublishTime = reportItem.PublishTime
+			temp.ReportCode = reportItem.ReportCode
+			if len(v.Highlight["Title"]) > 0 {
+				temp.Title = v.Highlight["Title"][0]
+			}
+			if len(v.Highlight["BodyContent"]) > 0 {
+				temp.ContentSub = v.Highlight["BodyContent"][0]
+			}
+
+			temp.ContentSub = "<div style=\"-webkit-line-clamp: 3;-webkit-box-orient: vertical;display: -webkit-box;overflow: hidden;text-overflow: ellipsis;\">" + temp.ContentSub + "</div>"
+			reportList = append(reportList, temp)
+		}
+	}
+	page.SetTotal(total)
+	baseData := new(base.BaseData)
+	baseData.SetPage(page)
+	baseData.SetList(reportList)
+	resp.OkData("搜索成功", baseData, c)
 }

+ 58 - 0
models/english_classify/classify.go

@@ -0,0 +1,58 @@
+package english_classify
+
+import (
+	"hongze/hongze_yb_en_api/global"
+	"hongze/hongze_yb_en_api/models/base"
+)
+
+type Classify struct {
+	Id            int       `gorm:"primaryKey;column:id" json:"id"`
+	ClassifyName  string    `gorm:"column:classify_name" json:"classify_name"` //分类名称
+	Sort          int8      `gorm:"column:sort" json:"sort"`                   //排序
+	ParentId      int       `gorm:"column:parent_id" json:"parent_id"`         //父级分类id
+	ClassifyLabel string    `gorm:"column:classify_label" json:"classify_label"`
+	ShowType      uint8     `gorm:"column:show_type" json:"show_type"` //展示类型:1-列表 2-专栏
+	IsShow        int8      `gorm:"column:is_show" json:"is_show"`     //是否展示报告:1,展示该分类下的报告,0隐藏分类下的报告
+	base.TimeBase
+}
+// TableName get sql table name.获取数据库表名
+func (c *Classify) TableName() string {
+	return "english_classify"
+}
+
+type ClassifyItem struct {
+	Id            int    `json:"id"`
+	ClassifyName  string `json:"classify_name"` //分类名称
+	Sort          int8   `json:"sort"`          //排序
+	ParentId      int    `json:"parent_id"`     //父级分类id
+	ClassifyLabel string `json:"classify_label"`
+	ShowType      uint8  `json:"show_type"`   //展示类型:1-列表 2-专栏
+	IsShow        int8   `json:"is_show"`     //是否展示报告:1,展示该分类下的报告,0隐藏分类下的报告
+	CreateTime    string `json:"create_time"` //创建时间
+	ModifyTime    string `json:"modify_time"` //最后更新时间
+}
+
+type ClassifyListItem struct {
+	Id            int             `json:"id"`
+	ClassifyName  string          `json:"classify_name"` //分类名称
+	Sort          int8            `json:"sort"`          //排序
+	ParentId      int             `json:"parent_id"`     //父级分类id
+	ClassifyLabel string          `json:"classify_label"`
+	ShowType      uint8           `json:"show_type"`   //展示类型:1-列表 2-专栏
+	IsShow        int8            `json:"is_show"`     //是否展示报告:1,展示该分类下的报告,0隐藏分类下的报告
+	CreateTime    string          `json:"create_time"` //创建时间
+	ModifyTime    string          `json:"modify_time"` //最后更新时间
+	Child         []*ClassifyItem `json:"child"`
+}
+
+// GetParent 获取一级分类列表
+func (c *Classify) GetParent() (list []*Classify, err error) {
+	err = global.DEFAULT_MYSQL.Model(c).Where("parent_id=0").Order("sort ASC,create_time ASC").Scan(&list).Error
+	return
+}
+
+// GetChild 获取二级分类列表
+func (c *Classify) GetChild() (list []*Classify, err error) {
+	err = global.DEFAULT_MYSQL.Model(c).Where("parent_id>0").Order("sort ASC,create_time ASC").Scan(&list).Error
+	return
+}

+ 43 - 4
models/english_report/english_report.go → models/english_report/report.go

@@ -6,7 +6,7 @@ import (
 	"time"
 )
 
-type EnglishReport struct {
+type Report struct {
 	Id                 int       `gorm:"primaryKey;column:id" json:"id"`
 	AddType            int8      `gorm:"column:add_type" json:"add_type"`                         //新增方式:1:新增报告,2:继承报告
 	ClassifyIdFirst    int       `gorm:"column:classify_id_first" json:"classify_id_first"`       //一级分类id
@@ -37,7 +37,7 @@ type EnglishReport struct {
 }
 
 // TableName get sql table name.获取数据库表名
-func (r *EnglishReport) TableName() string {
+func (r *Report) TableName() string {
 	return "english_report"
 }
 
@@ -69,8 +69,47 @@ type ListItem struct {
 	ModifyTime         string `json:"modify_time"`          //最后更新时间
 }
 
-func (r *EnglishReport) SelectPage(page base.IPage, condition string, pars []interface{}) (count int64, results []*EnglishReport, err error) {
-	results = make([]*EnglishReport, 0)
+type ReportSearchReq struct {
+	KeyWord  string `json:"key_word" form:"key_word"`
+	base.PageReq
+}
+
+type SearchEnglishReportItem struct {
+	Id                 int    `json:"id"`
+	ClassifyIdFirst    int    `json:"classify_id_first"`    //一级分类id
+	ClassifyNameFirst  string `json:"classify_name_first"`  //一级分类名称
+	ClassifyIdSecond   int    `json:"classify_id_second"`   //二级分类id
+	ClassifyNameSecond string `json:"classify_name_second"` //二级分类名称
+	Title              string `json:"title"`                //标题
+	Author             string `json:"author"`               //作者
+	PublishTime        string `json:"publish_time"`         //发布时间
+	ReportCode         string `json:"report_code"`          //报告唯一编码
+	CreateTime         string `json:"create_time"`          //创建时间
+	ContentSub         string `json:"content_sub"`          //内容前两个章节"`
+}
+
+type ElasticEnglishReportDetail struct {
+	ReportId           int
+	ClassifyIdFirst    int
+	ClassifyNameFirst  string
+	ClassifyIdSecond   int
+	ClassifyNameSecond string
+	StageStr           string
+	Title              string
+	Abstract           string
+	Author             string
+	Frequency          string
+	PublishState       int
+	BodyContent        string
+	ContentSub         string
+	CreateTime         string
+	PublishTime        string
+	ReportCode         string
+	Overview           string 
+}
+
+func (r *Report) SelectPage(page base.IPage, condition string, pars []interface{}) (count int64, results []*Report, err error) {
+	results = make([]*Report, 0)
 	query := global.DEFAULT_MYSQL.Model(r).
 		Select("*").
 		Where(condition, pars...)

+ 2 - 0
routers/english_report.go

@@ -10,4 +10,6 @@ func InitReport(baseGroup *gin.RouterGroup) {
 	controller := new(english_report.EnglishReportController)
 	group := baseGroup.Group("english_report/")
 	group.GET("list", controller.List)
+	group.GET("classify", controller.Classify)
+	group.GET("filter_by_es", controller.FilterByEs)
 }

+ 33 - 0
services/elastic/elastic.go

@@ -0,0 +1,33 @@
+package elastic
+
+import (
+	"github.com/olivere/elastic/v7"
+	"hongze/hongze_yb_en_api/global"
+)
+
+const (
+	ES_URL      = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
+	ES_USERNAME = "elastic"                                                               //<2>
+	ES_PASSWORD = "hongze@2021"                                                           //<3>
+	//Grafana pwd-> 20521bb9
+	//Grafana username-> emon
+)
+// es相关
+var (
+	EsEnglishReportIndexName string
+)
+
+func init()  {
+	if global.CONFIG.Serve.RunMode == "debug" {
+		EsEnglishReportIndexName = "test_english_report_v1" //报告
+	}else{
+		EsEnglishReportIndexName = "english_report_v1" //报告
+	}
+}
+func NewClient() (client *elastic.Client, err error) {
+	client, err = elastic.NewClient(
+		elastic.SetURL(ES_URL),
+		elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
+		elastic.SetSniff(false))
+	return
+}

+ 132 - 0
services/elastic/report.go

@@ -0,0 +1,132 @@
+package elastic
+
+import (
+	"context"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"github.com/olivere/elastic/v7"
+)
+
+// 首页搜索
+func SearchESEnglishReport(keyWord string, from, size int64) (searchResp *elastic.SearchResult, total int64, err error) {
+	indexName := EsEnglishReportIndexName
+	client, err := NewClient()
+	if err != nil {
+		return
+	}
+	var must  []map[string]interface{}
+	shouldSub := []map[string]interface{}{
+		map[string]interface{}{
+			"match": map[string]interface{}{
+				"Title": map[string]interface{}{
+					"query": keyWord,
+					//"minimum_should_match": "60%",
+					"boost": 3,
+				},
+			},
+		},
+		map[string]interface{}{
+			"match": map[string]interface{}{
+				"BodyContent": map[string]interface{}{
+					"query": keyWord,
+					//"minimum_should_match": "60%",
+					"boost": 1,
+				},
+			},
+		},
+		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{}{
+				"BodyContent": map[string]interface{}{
+					"query": keyWord,
+					"boost": 3,
+				},
+			},
+		},
+	}
+	mustMap := map[string]interface{}{
+		"bool": map[string]interface{}{
+			"should": shouldSub,
+		},
+	}
+	must = append(must, mustMap)
+	filterMust := []map[string]interface{}{
+		map[string]interface{}{
+			"term": map[string]interface{}{
+				"PublishState": 2,                     //必须是已发布的报告
+			},
+		},
+	}
+	filterMap := map[string]interface{}{
+		"bool": map[string]interface{}{
+			"must": filterMust,
+		},
+	}
+	source := map[string]interface{}{
+		"query": map[string]interface{}{
+			"bool": map[string]interface{}{
+				"must": must,
+				"filter": filterMap,
+			},
+		},
+	}
+	source["from"] = from
+	source["size"] = size
+	source["highlight"] = map[string]interface{}{
+		"fields": map[string]interface{}{
+			"Title":map[string]interface{}{},
+			"BodyContent":map[string]interface{}{
+				//	"pre_tags" : "{{highlight}}",
+				//	"post_tags": "{{/highlight}}",
+			},
+		},
+		"pre_tags" : "<span style=\"color:#00459F\">",
+		"post_tags": "</span>",
+	}
+
+	source["sort"] = []map[string]interface{}{
+		map[string]interface{}{
+			"PublishTime.keyword":map[string]interface{}{
+				"order":"desc",
+			},
+		},
+		map[string]interface{}{
+			"_score":map[string]interface{}{
+				"order":"desc",
+			},
+		},
+	}
+	jsonstr, err := json.Marshal(source)
+	fmt.Printf("%s",jsonstr)
+	request := client.Search(indexName).Source(source) // sets the JSON request
+
+	//requestJson, err := json.Marshal(request)
+	//if err != nil {
+	//	fmt.Println("requestJson err:", err)
+	//}
+	//fmt.Println("requestJson ", string(requestJson)
+
+	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
+}

+ 1 - 1
utils/constants.go

@@ -43,4 +43,4 @@ const (
 	HRSYSTEM_LOGIN_TOKEN = "hrSystem:login:token:"
 	HRSYSTEM_LOGIN_TOKEN_NO_TRUST = "hrSystem:login:no_trust:"        //管理后台登录(不可信登录态)
 	HRSYSTEM_LOGIN_ADMINID_IP = "hrSystem:login:admin_id:"
-)
+)