|
@@ -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
|
|
|
+}
|