|
@@ -461,8 +461,8 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
}
|
|
|
searchItem.Title = title
|
|
|
searchItem.PublishDate = article.PublishDate
|
|
|
- for _,v_result := range result{
|
|
|
- if v_result.ArticleId == searchItem.ArticleId{
|
|
|
+ for _, v_result := range result {
|
|
|
+ if v_result.ArticleId == searchItem.ArticleId {
|
|
|
isAppend = true
|
|
|
}
|
|
|
}
|
|
@@ -643,3 +643,89 @@ func GetArrSum(intArr []int) (sum int) {
|
|
|
// fmt.Println(keyWordArr)
|
|
|
// fmt.Println("end")
|
|
|
//}
|
|
|
+
|
|
|
+func EsMultiMatchFunctionScoreQueryTime(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()
|
|
|
+ //scoreSort := elastic.NewScoreSort()
|
|
|
+ multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText").Analyzer("ik_smart")
|
|
|
+ bodyFunctionQuery := elastic.NewFunctionScoreQuery()
|
|
|
+
|
|
|
+ bodyFunctionQuery.Query(multiMatch)
|
|
|
+ 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).Sort("PublishDate", false).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))
|
|
|
+ return
|
|
|
+}
|