Explorar o código

Es搜索添加分页

xingzai %!s(int64=2) %!d(string=hai) anos
pai
achega
52a087a392
Modificáronse 3 ficheiros con 182 adicións e 53 borrados
  1. 168 0
      controllers/search.go
  2. 9 0
      routers/commentsRouter.go
  3. 5 53
      services/elastic.go

+ 168 - 0
controllers/search.go

@@ -481,3 +481,171 @@ func (this *SearchController) ListHomeArtAndChart() {
 	br.Msg = "获取成功"
 	br.Data = resp
 }
+
+// @Title 搜索接口
+// @Description 搜索接口
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
+// @Param   KeyWord   query   string  true       "搜索关键词"
+// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
+// @Success 200 {object} models.SearchItem
+// @router /artAndChart/listPage [get]
+func (this *SearchController) ListHomeArtAndChartPage() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	// @Param   ListType   query   int  true       "列表类型,1最新/全部,2 纪要 ,3图表 默认1"
+	listType, _ := this.GetInt("ListType")
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	listType = 1
+	startSize = paging.StartIndex(currentIndex, pageSize)
+	keyWord := this.GetString("KeyWord")
+	orderColumn := this.GetString("OrderColumn")
+	if keyWord == "" {
+		br.Msg = "请输入搜索词"
+		br.ErrMsg = "请输入搜索词"
+		return
+	}
+	user := this.User
+	if user == nil {
+		br.Msg = "请重新登录"
+		br.Ret = 408
+		return
+	}
+
+	//研选的五张图片
+	detailResearch, errConfig := models.GetConfigByCode("category_research_img_url")
+	if errConfig != nil {
+		br.Msg = "获取数据失败"
+		br.ErrMsg = "获取数据研选分类图片失败,Err:" + errConfig.Error()
+		return
+	}
+	researchList := strings.Split(detailResearch.ConfigValue, "{|}")
+	//对应分类的所图片
+	detailCategoryUrl, errConfig := models.GetConfigByCode("category_map_img_url")
+	if errConfig != nil {
+		br.Msg = "获取数据失败"
+		br.ErrMsg = "行业配置信息失败,Err:" + errConfig.Error()
+		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
+	}
+	if orderColumn == "" {
+		orderColumn = "Matching"
+	}
+	var chartTotal int
+	resp := new(models.SearchResp)
+	var chartList []*models.HomeChartListResp
+	var err error
+	var condition string
+	var pars []interface{}
+	if listType == 1 || listType == 3 {
+		if currentIndex <= 2 {
+			condition = ` AND title LIKE '%` + keyWord + `%'`
+			chartList, err = models.GetChartList(condition, pars, startSize, pageSize)
+			if err != nil {
+				br.Msg = "获取信息失败"
+				br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
+				return
+			}
+			chartTotal, err = models.GetChartCount(condition, pars)
+			if err != nil {
+				br.Msg = "获取信息失败"
+				br.Msg = "获取帖子总数失败,Err:" + err.Error()
+				return
+			}
+		}
+	}
+	for k, v := range chartList {
+		chartList[k].IsNeedJump = true
+		chartList[k].Source = 2
+		if v.PtagName != "" {
+			chartList[k].CtagNamePc = v.PtagName
+		}
+		if v.CtagName != "" {
+			chartList[k].CtagNamePc += "," + v.CtagName
+		}
+		if v.PtagNameTwo != "" {
+			chartList[k].CtagNamePc += "," + v.PtagNameTwo
+		}
+		if v.CtagNameTwo != "" {
+			chartList[k].CtagNamePc += "," + v.CtagNameTwo
+		}
+	}
+	if len(chartList) == 0 {
+		chartList = make([]*models.HomeChartListResp, 0)
+	}
+	resp.ChartList = chartList
+	var result []*models.SearchItem
+	var total int64
+	if listType == 1 || listType == 2 {
+		tmpResult, tmpTotal, tmpErr := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn)
+		result = tmpResult
+		total = tmpTotal
+		err = tmpErr
+		if err != nil {
+			br.Msg = "检索失败"
+			br.ErrMsg = "检索失败,Err:" + err.Error()
+			return
+		}
+		if len(result) == 0 {
+			result = make([]*models.SearchItem, 0)
+		}
+
+		for k, v := range result {
+			//如果是研选系列的任意取五张图片的中的一张
+			if v.CategoryId == "0" {
+				knum := v.ArticleId % 5
+				result[k].ImgUrlPc = researchList[knum]
+			} else {
+				result[k].ImgUrlPc = mapCategoryUrl[v.CategoryId]
+			}
+			result[k].Source = 1
+		}
+	}
+	//记录用户搜索关键词
+	var source int
+	if listType == 1 {
+		source = 3
+	} else if listType == 2 {
+		source = 1
+	} else {
+		source = 2
+	}
+	go services.AddSearchKeyWord(user, keyWord, source)
+
+	if chartTotal > int(total) {
+		total = int64(chartTotal)
+	}
+	if listType == 1 {
+		total = total + int64(chartTotal)
+	}
+	if len(result) == 0 {
+		result = make([]*models.SearchItem, 0)
+	}
+	page := paging.GetPaging(currentIndex, pageSize, int(total))
+	resp.Paging = page
+	resp.List = result
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 9 - 0
routers/commentsRouter.go

@@ -988,6 +988,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:SearchController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:SearchController"],
+        beego.ControllerComments{
+            Method: "ListHomeArtAndChartPage",
+            Router: `/artAndChart/listPage`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:SearchController"] = append(beego.GlobalControllerRouter["hongze/hongze_cygx/controllers:SearchController"],
         beego.ControllerComments{
             Method: "SearchList",

+ 5 - 53
services/elastic.go

@@ -886,7 +886,7 @@ func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize
 	return
 }
 
-func init3424() {
+func init23423() {
 	EsArticleSearch("立高食品", 0, 10, "34")
 }
 
@@ -900,7 +900,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 		keyWordArr = append(keyWordArr, keyWord)
 		keyWordLen = len(keyWordArr)
 	}
-	fmt.Println(keyWordArr)
+	//fmt.Println(keyWordArr)
 	mustMap := make([]interface{}, 0)
 	shouldMap := make(map[string]interface{}, 0)
 	//shouldMapquery := make(map[string]interface{}, 0)
@@ -934,56 +934,6 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 					},
 				},
 			})
-
-			//shouldMapquery = append(shouldMapquery, map[string]interface{}{
-			//	"match": map[string]interface{}{
-			//		"Title": map[string]interface{}{
-			//			"query": v,
-			//			"boost": math.Pow(10, float64(lenkeyWordArr-k)), //给查询的值赋予权重 10的n次方
-			//		},
-			//	},
-			//})
-			//shouldMapquery = append(shouldMapquery, map[string]interface{}{
-			//	"match": map[string]interface{}{
-			//		"BodyText": map[string]interface{}{
-			//			"query": v,
-			//			"boost": math.Pow(10, float64(lenkeyWordArr-k)) - 1, //给查询的值赋予权重 10的n次方
-			//		},
-			//	},
-			//})
-
-			//shouldMapquery = append(shouldMapquery, map[string]interface{}{
-			//	"wildcard": map[string]interface{}{
-			//		"BodyText": "*" + v + "*",
-			//		"boost":    math.Pow(10, float64(lenkeyWordArr-k)) - 1, //给查询的值赋予权重 10的n次方
-			//	},
-			//})
-			//shouldMap = map[string]interface{}{
-			//	"should": []interface{}{
-			//		map[string]interface{}{
-			//			"function_score": map[string]interface{}{
-			//				"query": map[string]interface{}{
-			//					"multi_match": map[string]interface{}{
-			//						"boost":  2000,
-			//						"fields": []interface{}{"Title"},
-			//						"query":  "立高食品",
-			//					},
-			//				},
-			//			},
-			//		},
-			//		map[string]interface{}{
-			//			"function_score": map[string]interface{}{
-			//				"query": map[string]interface{}{
-			//					"multi_match": map[string]interface{}{
-			//						"boost":  2000 - 1,
-			//						"fields": []interface{}{"BodyText"},
-			//						"query":  "立高食品",
-			//					},
-			//				},
-			//			},
-			//		},
-			//	},
-			//}
 		}
 	}
 	shouldMap = map[string]interface{}{
@@ -1019,7 +969,9 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 			},
 		},
 	}
-	queryMap["sort"] = sortMap
+	if orderColumn == "PublishDate" {
+		queryMap["sort"] = sortMap
+	}
 	queryMap["from"] = startSize
 	queryMap["size"] = pageSize
 	queryMap["highlight"] = highlightMap