|
@@ -387,11 +387,9 @@ func EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize in
|
|
|
func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
|
|
|
client, err := NewClient()
|
|
|
keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
|
|
|
-
|
|
|
keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
- boolquery := elastic.NewBoolQuery()
|
|
|
- matchArr := make([]elastic.Query, 0)
|
|
|
-
|
|
|
+ //artidArr := make([]elastic.Query, 0)
|
|
|
+ //matchArr := make([]elastic.Query, 0)
|
|
|
n := 0
|
|
|
keyWordLen := len(keyWordArr)
|
|
|
if keyWordLen <= 0 {
|
|
@@ -402,6 +400,16 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
keyWordWeight := GetWeight(keyWordLen)
|
|
|
for k, 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()
|
|
@@ -410,54 +418,111 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
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
|
|
|
- }
|
|
|
- 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
|
|
|
+ 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)
|
|
|
+ }
|
|
|
}
|
|
|
- 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
|
|
|
}
|
|
|
}
|
|
|
- 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
|
|
|
}
|
|
|
|