rdluck 4 years ago
parent
commit
9e4c669d7e
4 changed files with 160 additions and 21 deletions
  1. 19 1
      controllers/search.go
  2. 0 18
      controllers/wechat.go
  3. 138 2
      services/elastic.go
  4. 3 0
      services/task.go

+ 19 - 1
controllers/search.go

@@ -1,8 +1,11 @@
 package controllers
 
 import (
+	"fmt"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
+	"hongze/hongze_cygx/utils"
+	"rdluck_tools/paging"
 	"time"
 )
 
@@ -12,6 +15,8 @@ type SearchController struct {
 
 // @Title 搜索接口
 // @Description 搜索接口
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
 // @Param   KeyWord   query   string  true       "搜索关键词"
 // @Success 200 {object} models.SearchItem
 // @router /list [get]
@@ -21,6 +26,18 @@ func (this *SearchController) SearchList() {
 		this.Data["json"] = br
 		this.ServeJSON()
 	}()
+	pageSize, _ := this.GetInt("PageSize")
+	currentIndex, _ := this.GetInt("CurrentIndex")
+	var startSize int
+	if pageSize <= 0 {
+		pageSize = utils.PageSize20
+	}
+	if currentIndex <= 0 {
+		currentIndex = 1
+	}
+	startSize = paging.StartIndex(currentIndex, pageSize)
+
+	fmt.Println(startSize)
 	keyWord := this.GetString("KeyWord")
 	if keyWord == "" {
 		br.Msg = "请输入搜索词"
@@ -48,7 +65,8 @@ func (this *SearchController) SearchList() {
 		categoryName := strings.Join(categoryNameArr, ",")
 	*/
 	indexName := "article_list"
-	result, err := services.EsMatchQuery(indexName, keyWord)
+	result, err := services.EsMatchFunctionScoreQuery(indexName, keyWord)//services.EsMatchQuery(indexName, keyWord)
+	//result, err := services.EsMatchQuery(indexName, keyWord)//services.EsMatchQuery(indexName, keyWord)
 	//indexName := "article_list"
 	//result, err := services.EsMatchPhraseQuery(indexName, keyWord)
 	if err != nil {

+ 0 - 18
controllers/wechat.go

@@ -294,15 +294,6 @@ func (this *WechatController) GetUserInfo() {
 		token = tokenItem.AccessToken
 	}
 	resp := new(models.WxGetUserInfoResp)
-	//resp.NickName = userInfo.Nickname
-	//resp.Gender = userInfo.Gender
-	//resp.Province = userInfo.Province
-	//resp.Language = userInfo.Language
-	//resp.Country = userInfo.Country
-	//resp.City = userInfo.City
-	//resp.AvatarUrl = userInfo.Avatar
-	//resp.Appid = userInfo.Watermark.AppID
-	//resp.Timestamp = userInfo.Watermark.Timestamp
 	resp.Authorization = token
 	br.Msg = "获取成功!"
 	br.Ret = 200
@@ -351,15 +342,6 @@ func (this *WechatController) GetPhoneNumber() {
 		return
 	}
 
-	//更新用户记录信息
-	{
-		updateParams := make(map[string]interface{})
-		updateParams["Mobile"]=wxMobile.PhoneNumber
-		updateParams["BindAccount"]=wxMobile.PhoneNumber
-		whereParam := map[string]interface{}{"user_id": user.UserId, "create_platform": 4}
-		go models.UpdateByExpr(models.UserRecord{}, whereParam, updateParams)
-	}
-
 	resp := new(models.WxGetPhoneNumberResp)
 	resp.PhoneNumber = wxMobile.PhoneNumber
 	resp.PurePhoneNumber = wxMobile.PurePhoneNumber

+ 138 - 2
services/elastic.go

@@ -6,9 +6,12 @@ import (
 	"fmt"
 	"github.com/olivere/elastic/v7"
 	"hongze/hongze_cygx/models"
+	"hongze/hongze_cygx/utils"
 	"log"
 	"os"
 	"strconv"
+
+	"github.com/emirpasic/gods/lists/arraylist"
 )
 
 func NewClient() (client *elastic.Client, err error) {
@@ -132,7 +135,7 @@ func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err e
 	searchMap := make(map[int]int)
 	boolquery := elastic.NewBoolQuery()
 	keyLen := len(keyWordArr)
-	n := 2.0 * float64(keyLen)
+	n := 5.0 * float64(keyLen)
 	matchArr := make([]elastic.Query, 0)
 	//
 	//matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
@@ -147,7 +150,7 @@ func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err e
 			matchArr = append(matchArr, matchq1)
 			matchArr = append(matchArr, matchq2)
 		}
-		n = n - 2
+		n = n - 5
 	}
 	boolquery.Should(matchArr...)
 	highlight := elastic.NewHighlight()
@@ -267,3 +270,136 @@ func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem,
 	}
 	return
 }
+
+func EsMatchFunctionScoreQuery(indexName, keyWord string) (result []*models.SearchItem, 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")
+	//matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
+	//
+	//matchArr = append(matchArr, matchq1)
+	//matchArr = append(matchArr, matchq2)
+	n := 0
+	for i := keyLen - 1; i >= 0; i-- {
+		v := keyWordArr[i]
+		if v != "" {
+			n := float64(n*10 - n)
+			if n < 0 {
+				n = 1
+			}
+			fmt.Println("n=", n)
+			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)
+			titleFunctionQuery.AddScoreFunc(titleFunctions)
+			titleFunctionQuery.BoostMode("replace")
+
+			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
+			bodyFunctionQuery.Query(bodyMatchq)
+			bodyFunctions := elastic.NewWeightFactorFunction(n)
+			bodyFunctionQuery.AddScoreFunc(bodyFunctions)
+			bodyFunctionQuery.BoostMode("replace")
+
+			matchArr = append(matchArr, titleFunctionQuery)
+			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).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
+				}
+				article := new(models.CygxArticle)
+				err = json.Unmarshal(articleJson, &article)
+				if err != nil {
+					return nil, 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
+				}
+			}
+		}
+	}
+	return
+}
+
+func init() {
+	lenArr := 10
+	for i := 0; i < lenArr; i++ {
+
+	}
+}
+
+func init() {
+	fmt.Println("start")
+	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")
+}
+
+func GetArrSum(intArr []int) (sum int) {
+	for _, val := range intArr {
+		//累计求和
+		sum += val
+	}
+	return
+}

+ 3 - 0
services/task.go

@@ -150,6 +150,9 @@ func Task() {
 
 	//同步策略文章
 	//SyncTacticsList()
+
+	//indexName := "article_list"
+	//EsDeleteData(indexName,)
 	fmt.Println("end")
 }