Explorar o código

添加行业分类列表字段

xingzai %!s(int64=3) %!d(string=hai) anos
pai
achega
1fe9535caa
Modificáronse 5 ficheiros con 187 adicións e 15 borrados
  1. 15 1
      controllers/search.go
  2. 8 1
      controllers/tactics.go
  3. 9 1
      models/report_mapping.go
  4. 3 2
      models/tactics.go
  5. 152 10
      services/elastic.go

+ 15 - 1
controllers/search.go

@@ -73,7 +73,21 @@ func (this *SearchController) SearchList() {
 	//indexName := "article_list"
 	indexName := utils.IndexName
 	pageSize = 20
-	result, total, err := services.EsMultiMatchFunctionScoreQueryTime(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
+	var result []*models.SearchItem
+	var total int64
+	var err error
+	if orderColumn == "PublishDate" {
+		tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, pageSize, 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
+	}
+
 	//result, total, err := services.EsMultiMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize, user.UserId) //services.EsMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchQuery(indexName, keyWord)
 	//result, total, err := services.SearchByKeyWordBack(indexName, keyWord, startSize, pageSize,user.UserId)
 	//result, total, err := services.EsMultiMatchFunctionScoreQueryFix(indexName, keyWord, startSize, pageSize)

+ 8 - 1
controllers/tactics.go

@@ -61,7 +61,7 @@ func (this *TacticsController) List() {
 	resp := new(models.TacticsListResp)
 	page := paging.GetPaging(currentIndex, pageSize, total)
 
-	if categoryId < 1 {
+	if categoryId < 0 {
 		listTactics, err := models.GetReportMappingStrategyAll()
 		if err != nil && err.Error() != utils.ErrNoRow() {
 			br.Msg = "获取信息失败"
@@ -103,6 +103,13 @@ func (this *TacticsController) List() {
 			list[k].IsRed = true
 		}
 	}
+	detail, errCategory := models.GetdetailByCategoryId(categoryId)
+	if errCategory != nil && errCategory.Error() != utils.ErrNoRow() {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取信息失败,Err:" + errCategory.Error()
+		return
+	}
+	resp.MatchTypeName = detail.MatchTypeName
 	resp.List = list
 	resp.Paging = page
 	br.Ret = 200

+ 9 - 1
models/report_mapping.go

@@ -7,7 +7,7 @@ import (
 type ReportMapping struct {
 	CategoryId      int    `description:"分类ID"`
 	SubCategoryName string `description:"主题"`
-	MathTypeName    string `description:"分类名称"`
+	MatchTypeName   string `description:"分类名称"`
 	IsRed           bool   `description:"是否标红"`
 }
 
@@ -97,3 +97,11 @@ type IndustrialToArticleCategoryListRep struct {
 	IndustrialManagementId int    `description:"产业D"`
 	List                   []*IndustrialToArticleCategoryRep
 }
+
+//通过分类ID获取详情
+func GetdetailByCategoryId(categoryId int) (item *ReportMapping, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM cygx_report_mapping WHERE report_type=1 AND category_id=? `
+	err = o.Raw(sql, categoryId).QueryRow(&item)
+	return
+}

+ 3 - 2
models/tactics.go

@@ -113,8 +113,9 @@ func GetTacticsListAll2() (list []*Tactics2, err error) {
 }
 
 type TacticsListResp struct {
-	Paging *paging.PagingItem
-	List   []*ReportArticle
+	Paging        *paging.PagingItem
+	MatchTypeName string `description:"匹配类型"`
+	List          []*ReportArticle
 }
 
 func GetReportTacticsList(condition string, pars []interface{}, userId, startSize, pageSize int) (items []*ReportArticle, err error) {

+ 152 - 10
services/elastic.go

@@ -644,7 +644,7 @@ func GetArrSum(intArr []int) (sum int) {
 //	fmt.Println("end")
 //}
 
-func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
+func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
 	client, err := NewClient()
 	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
 	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
@@ -665,20 +665,20 @@ func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pa
 			matchArr := make([]elastic.Query, 0)
 			boolquery := elastic.NewBoolQuery()
 			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
-			if orderColumn == "PublishDate" {
-				multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
-				bodyFunctionQuery.Query(multiMatch)
-			} else {
-				multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
-				bodyFunctionQuery.Query(multiMatch)
-			}
-
+			//if orderColumn == "PublishDate" {
+			//	multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
+			//	bodyFunctionQuery.Query(multiMatch)
+			//} else {
+			//	multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
+			//	bodyFunctionQuery.Query(multiMatch)
+			//}
+			multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
+			bodyFunctionQuery.Query(multiMatch)
 			matchArr = append(matchArr, bodyFunctionQuery)
 			boolquery.Should(matchArr...)
 			highlight := elastic.NewHighlight()
 			highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
 			highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
-
 			request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
 			if orderColumn == "Matching" {
 				request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
@@ -739,3 +739,145 @@ func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pa
 	total = int64(len(result))
 	return
 }
+
+func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
+	client, err := NewClient()
+	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
+	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
+	//artidArr := make([]elastic.Query, 0)
+	//matchArr := make([]elastic.Query, 0)
+	n := 0
+	keyWordLen := len(keyWordArr)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
+		keyWordLen = len(keyWordArr)
+	}
+	utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
+	//keyWordWeight := GetWeight(keyWordLen)
+	for _, v := range keyWordArr {
+		if v != "" {
+			matchArr := make([]elastic.Query, 0)
+			boolquery := elastic.NewBoolQuery()
+			//weight := float64(keyWordWeight[k])
+			//multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
+			//bodyFunctionQuery := elastic.NewFunctionScoreQuery()
+			//bodyFunctionQuery.Query(multiMatch)
+			//bodyFunctions := elastic.NewWeightFactorFunction(weight)
+			//bodyFunctionQuery.AddScoreFunc(bodyFunctions)
+			//bodyFunctionQuery.BoostMode("replace")
+			//matchArr = append(matchArr, bodyFunctionQuery)
+			//weight := float64(keyWordWeight[k])
+			multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
+			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
+			bodyFunctionQuery.Query(multiMatch)
+			//bodyFunctions := elastic.NewWeightFactorFunction(weight)
+			//bodyFunctionQuery.AddScoreFunc(bodyFunctions)
+			//bodyFunctionQuery.BoostMode("replace")
+			matchArr = append(matchArr, bodyFunctionQuery)
+			boolquery.Should(matchArr...)
+			highlight := elastic.NewHighlight()
+			highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+			highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+			request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
+			searchByMatch, err := request.Do(context.Background())
+			if err != nil {
+				return nil, 0, err
+			}
+			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.CygxArticle)
+						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
+						for _, v_result := range result {
+							if v_result.ArticleId == searchItem.ArticleId {
+								isAppend = true
+							}
+						}
+						if !isAppend {
+							result = append(result, searchItem)
+						}
+					}
+				}
+				//total += searchByMatch.Hits.TotalHits.Value
+			}
+		}
+		n++
+	}
+	total = int64(len(result))
+	//fmt.Println(result)
+	//boolquery.Should(matchArr...)
+	//highlight := elastic.NewHighlight()
+	//highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+	//highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+	//request := client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
+	//searchByMatch, err := request.Do(context.Background())
+	//if searchByMatch != nil {
+	//	if searchByMatch.Hits != nil {
+	//		for _, v := range searchByMatch.Hits.Hits {
+	//			articleJson, err := v.Source.MarshalJSON()
+	//			if err != nil {
+	//				return nil, 0, err
+	//			}
+	//			article := new(models.CygxArticle)
+	//			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
+	//
+	//			result = append(result, searchItem)
+	//		}
+	//	}
+	//	total = searchByMatch.Hits.TotalHits.Value
+	//}
+	return
+}