|
@@ -644,7 +644,7 @@ func GetArrSum(intArr []int) (sum int) {
|
|
// fmt.Println("end")
|
|
// fmt.Println("end")
|
|
//}
|
|
//}
|
|
|
|
|
|
-func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
|
|
+func EsMultiMatchFunctionScoreQuerySort(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
|
|
client, err := NewClient()
|
|
client, err := NewClient()
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
@@ -665,20 +665,20 @@ func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pa
|
|
matchArr := make([]elastic.Query, 0)
|
|
matchArr := make([]elastic.Query, 0)
|
|
boolquery := elastic.NewBoolQuery()
|
|
boolquery := elastic.NewBoolQuery()
|
|
bodyFunctionQuery := elastic.NewFunctionScoreQuery()
|
|
bodyFunctionQuery := elastic.NewFunctionScoreQuery()
|
|
- if orderColumn == "PublishDate" {
|
|
|
|
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
|
|
|
|
- bodyFunctionQuery.Query(multiMatch)
|
|
|
|
- } else {
|
|
|
|
- multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
|
|
|
|
- bodyFunctionQuery.Query(multiMatch)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ //if orderColumn == "PublishDate" {
|
|
|
|
+ // multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
|
|
|
|
+ // bodyFunctionQuery.Query(multiMatch)
|
|
|
|
+ //} else {
|
|
|
|
+ // multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
|
|
|
|
+ // bodyFunctionQuery.Query(multiMatch)
|
|
|
|
+ //}
|
|
|
|
+ multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
|
|
|
|
+ bodyFunctionQuery.Query(multiMatch)
|
|
matchArr = append(matchArr, bodyFunctionQuery)
|
|
matchArr = append(matchArr, bodyFunctionQuery)
|
|
boolquery.Should(matchArr...)
|
|
boolquery.Should(matchArr...)
|
|
highlight := elastic.NewHighlight()
|
|
highlight := elastic.NewHighlight()
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
-
|
|
|
|
request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
|
|
request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
|
|
if orderColumn == "Matching" {
|
|
if orderColumn == "Matching" {
|
|
request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
|
|
request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
|
|
@@ -739,3 +739,145 @@ func EsMultiMatchFunctionScoreQueryTime(indexName, keyWord string, startSize, pa
|
|
total = int64(len(result))
|
|
total = int64(len(result))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
+ client, err := NewClient()
|
|
|
|
+ keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
|
|
+ keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
|
+ //artidArr := make([]elastic.Query, 0)
|
|
|
|
+ //matchArr := make([]elastic.Query, 0)
|
|
|
|
+ n := 0
|
|
|
|
+ keyWordLen := len(keyWordArr)
|
|
|
|
+ if keyWordLen <= 0 {
|
|
|
|
+ keyWordArr = append(keyWordArr, keyWord)
|
|
|
|
+ keyWordLen = len(keyWordArr)
|
|
|
|
+ }
|
|
|
|
+ utils.FileLog.Info("SearchKeyWord:%s, userId:%s", keyWordArr, strconv.Itoa(userId))
|
|
|
|
+ //keyWordWeight := GetWeight(keyWordLen)
|
|
|
|
+ for _, 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()
|
|
|
|
+ bodyFunctionQuery.Query(multiMatch)
|
|
|
|
+ //bodyFunctions := elastic.NewWeightFactorFunction(weight)
|
|
|
|
+ //bodyFunctionQuery.AddScoreFunc(bodyFunctions)
|
|
|
|
+ //bodyFunctionQuery.BoostMode("replace")
|
|
|
|
+ matchArr = append(matchArr, bodyFunctionQuery)
|
|
|
|
+ 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)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //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
|
|
|
|
+}
|