rdluck před 4 roky
rodič
revize
34952a04bb
3 změnil soubory, kde provedl 122 přidání a 33 odebrání
  1. 11 6
      controllers/search.go
  2. 9 1
      models/search.go
  3. 102 26
      services/elastic.go

+ 11 - 6
controllers/search.go

@@ -65,7 +65,7 @@ func (this *SearchController) SearchList() {
 		categoryName := strings.Join(categoryNameArr, ",")
 	*/
 	indexName := "article_list"
-	result, err := services.EsMatchFunctionScoreQuery(indexName, keyWord)//services.EsMatchQuery(indexName, keyWord)
+	result, total, err := services.EsMultiMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchFunctionScoreQuery(indexName, keyWord, startSize, pageSize) //services.EsMatchQuery(indexName, keyWord)
 	//result, err := services.EsMatchQuery(indexName, keyWord)//services.EsMatchQuery(indexName, keyWord)
 	//indexName := "article_list"
 	//result, err := services.EsMatchPhraseQuery(indexName, keyWord)
@@ -78,16 +78,21 @@ func (this *SearchController) SearchList() {
 		result = make([]*models.SearchItem, 0)
 	}
 	{
-		keyWordItem:=new(models.CygxSearchKeyWord)
-		keyWordItem.UserId=user.UserId
-		keyWordItem.KeyWord=keyWord
-		keyWordItem.CreateTime=time.Now()
+		keyWordItem := new(models.CygxSearchKeyWord)
+		keyWordItem.UserId = user.UserId
+		keyWordItem.KeyWord = keyWord
+		keyWordItem.CreateTime = time.Now()
 		go models.AddSearchKeyWord(keyWordItem)
 	}
+
+	resp := new(models.SearchResp)
+	page := paging.GetPaging(currentIndex, pageSize, int(total))
+	resp.Paging = page
+	resp.List = result
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
-	br.Data = result
+	br.Data = resp
 }
 
 //https://blog.csdn.net/my_miuye/article/details/110496025

+ 9 - 1
models/search.go

@@ -1,6 +1,9 @@
 package models
 
-import "rdluck_tools/orm"
+import (
+	"rdluck_tools/orm"
+	"rdluck_tools/paging"
+)
 
 type SearchItem struct {
 	ArticleId   int      `description:"文章id"`
@@ -23,3 +26,8 @@ func GetCategoryByCompanyId(companyId int) (items []*CategoryItem, err error) {
 	_, err = o.Raw(sql, companyId).QueryRows(&items)
 	return
 }
+
+type SearchResp struct {
+	Paging *paging.PagingItem
+	List   []*SearchItem
+}

+ 102 - 26
services/elastic.go

@@ -9,9 +9,8 @@ import (
 	"hongze/hongze_cygx/utils"
 	"log"
 	"os"
+	"sort"
 	"strconv"
-
-	"github.com/emirpasic/gods/lists/arraylist"
 )
 
 func NewClient() (client *elastic.Client, err error) {
@@ -271,17 +270,16 @@ func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem,
 	return
 }
 
-func EsMatchFunctionScoreQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
+func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
 	client, err := NewClient()
-	pageSize := 20
 	keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
 	fmt.Println(keyWordArr)
 	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
 	fmt.Println("-------------------------------")
 	fmt.Println(keyWordArr)
+
 	searchMap := make(map[int]int)
 	boolquery := elastic.NewBoolQuery()
-	keyLen := len(keyWordArr)
 	matchArr := make([]elastic.Query, 0)
 	//
 	//matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
@@ -290,27 +288,24 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string) (result []*models.Sear
 	//matchArr = append(matchArr, matchq1)
 	//matchArr = append(matchArr, matchq2)
 	n := 0
-	for i := keyLen - 1; i >= 0; i-- {
-		v := keyWordArr[i]
+	keyWordLen := len(keyWordArr)
+	keyWordWeight := GetWeight(keyWordLen)
+	for k, v := range keyWordArr {
 		if v != "" {
-			n := float64(n*10 - n)
-			if n < 0 {
-				n = 1
-			}
-			fmt.Println("n=", n)
+			weight := float64(keyWordWeight[k])
 			titleMatchq := elastic.NewMatchQuery("Title", v).Analyzer("ik_smart")
 
 			bodyMatchq := elastic.NewMatchQuery("BodyText", v).Analyzer("ik_smart")
 
 			titleFunctionQuery := elastic.NewFunctionScoreQuery()
 			titleFunctionQuery.Query(titleMatchq)
-			titleFunctions := elastic.NewWeightFactorFunction(n)
+			titleFunctions := elastic.NewWeightFactorFunction(weight)
 			titleFunctionQuery.AddScoreFunc(titleFunctions)
 			titleFunctionQuery.BoostMode("replace")
 
 			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
 			bodyFunctionQuery.Query(bodyMatchq)
-			bodyFunctions := elastic.NewWeightFactorFunction(n)
+			bodyFunctions := elastic.NewWeightFactorFunction(weight)
 			bodyFunctionQuery.AddScoreFunc(bodyFunctions)
 			bodyFunctionQuery.BoostMode("replace")
 
@@ -323,19 +318,19 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string) (result []*models.Sear
 	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).Size(pageSize).Query(boolquery)
+	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, err
+					return nil, 0, err
 				}
 				article := new(models.CygxArticle)
 				err = json.Unmarshal(articleJson, &article)
 				if err != nil {
-					return nil, err
+					return nil, 0, err
 				}
 				if _, ok := searchMap[article.ArticleId]; !ok {
 					searchItem := new(models.SearchItem)
@@ -366,34 +361,115 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string) (result []*models.Sear
 			}
 		}
 	}
+	total=searchByMatch.Hits.TotalHits.Value
 	return
 }
 
-func init() {
-	lenArr := 10
-	for i := 0; i < lenArr; i++ {
 
+
+func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
+	client, err := NewClient()
+	keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
+	fmt.Println(keyWordArr)
+	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
+	fmt.Println("-------------------------------")
+	fmt.Println(keyWordArr)
+
+	searchMap := make(map[int]int)
+	boolquery := elastic.NewBoolQuery()
+	matchArr := make([]elastic.Query, 0)
+	//
+	//matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
+	//matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
+	//
+	//matchArr = append(matchArr, matchq1)
+	//matchArr = append(matchArr, matchq2)
+	n := 0
+	keyWordLen := len(keyWordArr)
+	keyWordWeight := GetWeight(keyWordLen)
+	for k, v := range keyWordArr {
+		if v != "" {
+			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)
+		}
+		n++
 	}
+	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
+				}
+				if _, ok := searchMap[article.ArticleId]; !ok {
+					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])
+						fmt.Println(body)
+						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)
+					searchMap[article.ArticleId] = article.ArticleId
+				}
+			}
+		}
+	}
+	total=searchByMatch.Hits.TotalHits.Value
+	return
 }
 
-func init() {
-	fmt.Println("start")
+
+func GetWeight(length int) []int {
 	steep := 5
 	intArr := make([]int, 0)
-	list := arraylist.New()
 	for i := 1; i <= 10; i++ {
 		if i == 1 {
 			intArr = append(intArr, 1)
-			list.Add(1)
 		} else {
 			min := GetArrSum(intArr)
 			maxVal := utils.GetRandInt(min, min+steep)
 			intArr = append(intArr, maxVal)
-			list.Add(maxVal)
 		}
 		fmt.Println("intArr:", intArr)
 	}
-	fmt.Println("end")
+	//数组排序
+	sort.Slice(intArr, func(i, j int) bool {
+		return intArr[i] > intArr[j]
+	})
+	return intArr
 }
 
 func GetArrSum(intArr []int) (sum int) {