|
@@ -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) {
|