rdluck 4 years ago
parent
commit
c25bc021e6
3 changed files with 49 additions and 6 deletions
  1. 25 2
      controllers/search.go
  2. 17 0
      models/search.go
  3. 7 4
      services/elasticsearch.go

+ 25 - 2
controllers/search.go

@@ -3,10 +3,11 @@ package controllers
 import (
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
+	"strings"
 )
 
 type SearchController struct {
-	BaseCommonController
+	BaseAuthController
 }
 
 // @Title 搜索接口
@@ -27,12 +28,34 @@ func (this *SearchController) SearchList() {
 		return
 	}
 	pageSize := 20
-	result, err := services.SearchByKeyWord(keyWord, pageSize)
+	user := this.User
+	if user == nil {
+		br.Msg = "请重新登录"
+		br.Ret = 408
+		return
+	}
+
+	categoryList, err := models.GetCategoryByCompanyId(user.CompanyId)
+	if err != nil {
+		br.Msg = "检索失败!"
+		br.ErrMsg = "检索分类信息失败,Err:" + err.Error()
+		return
+	}
+
+	var categoryNameArr []string
+	for _, v := range categoryList {
+		categoryNameArr = append(categoryNameArr, v.CategoryName)
+	}
+	categoryName := strings.Join(categoryNameArr, ",")
+	result, err := services.SearchByKeyWord(keyWord, categoryName, pageSize)
 	if err != nil {
 		br.Msg = "检索失败"
 		br.ErrMsg = "检索失败,Err:" + err.Error()
 		return
 	}
+	if len(result) == 0 {
+		result = make([]*models.SearchItem, 0)
+	}
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"

+ 17 - 0
models/search.go

@@ -1,8 +1,25 @@
 package models
 
+import "rdluck_tools/orm"
+
 type SearchItem struct {
 	ArticleId   int      `description:"文章id"`
 	Body        []string `description:"搜索结果"`
 	Title       string   `description:"标题"`
 	PublishDate string   `description:"发布时间"`
 }
+
+type CategoryItem struct {
+	CategoryName string
+}
+
+func GetCategoryByCompanyId(companyId int) (items []*CategoryItem, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT b.chart_permission_id,d.category_name FROM company_product AS a
+			INNER JOIN company_report_permission AS b ON a.company_id=b.company_id AND a.product_id=b.product_id
+			INNER JOIN cygx_permission_mapping AS d ON b.chart_permission_id=d.chart_permission_id
+			WHERE a.product_id=2
+			AND a.company_id=?`
+	_, err = o.Raw(sql, companyId).QueryRows(&items)
+	return
+}

+ 7 - 4
services/elasticsearch.go

@@ -60,7 +60,7 @@ func SaveData() {
 		3161,3190,3226,3244,3264,3285,3310,3334,3370,3397,3418,3446,3477,3497,3526,3554
 	*/
 
-	idStr := `3161,3190,3226,3244,3264,3285,3310,3334,3370,3397,3418,3446,3477,3497,3526,3554`
+	idStr := `3584,3644`
 	idArr := strings.Split(idStr, ",")
 	for _, v := range idArr {
 		id, _ := strconv.Atoi(v)
@@ -77,7 +77,7 @@ func SaveData() {
 		}
 		bodyText := doc.Text()
 		item.BodyText = bodyText
-		//删除
+		//新增
 		resp, err := client.Index().Index(esIndex).Id(strconv.Itoa(item.ArticleId)).BodyJson(item).Do(context.Background())
 		if err != nil {
 			fmt.Println("insert es failed", err.Error())
@@ -150,7 +150,7 @@ func SaveData() {
 	fmt.Println("end")
 }
 
-func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem, err error) {
+func SearchByKeyWord(keyWord,categoryName string, pageSize int) (result []*models.SearchItem, err error) {
 	if pageSize == 0 {
 		pageSize = 20
 	}
@@ -167,6 +167,9 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
 		return
 	}
 	var esIndex = "cygx_article"
+
+	termsQuery:=elastic.NewTermsQuery("category_name",categoryName)
+
 	boolquery := elastic.NewBoolQuery()
 
 	boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
@@ -175,7 +178,7 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
 	highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
 	highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
 
-	searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
+	searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(termsQuery).Query(boolquery).Do(context.Background())
 
 	if err != nil {
 		return