|
@@ -425,491 +425,6 @@ func EsDeleteData(indexName, docId string) (err error) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-func MappingModify(indexName, mappingJson string) {
|
|
|
|
- client := utils.Client
|
|
|
|
- //if err != nil {
|
|
|
|
- // return
|
|
|
|
- //}
|
|
|
|
- result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
|
|
|
|
- fmt.Println(err)
|
|
|
|
- fmt.Println(result)
|
|
|
|
- return
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
|
|
|
|
- client := utils.Client
|
|
|
|
- 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)
|
|
|
|
- n := 5.0 * float64(keyLen)
|
|
|
|
- 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)
|
|
|
|
- for _, v := range keyWordArr {
|
|
|
|
- if v != "" {
|
|
|
|
- matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
|
|
|
|
- matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
|
|
|
|
- matchArr = append(matchArr, matchq1)
|
|
|
|
- matchArr = append(matchArr, matchq2)
|
|
|
|
- }
|
|
|
|
- n = n - 5
|
|
|
|
- }
|
|
|
|
- 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.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 EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
|
|
|
|
- client := utils.Client
|
|
|
|
- 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)
|
|
|
|
- //n := float64(keyLen)
|
|
|
|
- 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")
|
|
|
|
- matchq1 := elastic.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart")
|
|
|
|
- matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord)
|
|
|
|
- matchArr = append(matchArr, matchq1)
|
|
|
|
- matchArr = append(matchArr, matchq2)
|
|
|
|
- //matchArr = append(matchArr, matchq2)
|
|
|
|
- //for _, v := range keyWordArr {
|
|
|
|
- // if v != "" {
|
|
|
|
- // matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
|
|
|
|
- // matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
|
|
|
|
- // matchArr = append(matchArr, matchq1)
|
|
|
|
- // matchArr = append(matchArr, matchq2)
|
|
|
|
- // }
|
|
|
|
- // n--
|
|
|
|
- //}
|
|
|
|
- //boolquery.Should(matchArr...)
|
|
|
|
-
|
|
|
|
- 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())
|
|
|
|
- fmt.Println("err:", err, searchByMatch)
|
|
|
|
- return
|
|
|
|
- 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)
|
|
|
|
- searchItem.Body = v.Highlight["BodyText"]
|
|
|
|
- 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 EsMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
- client := utils.Client
|
|
|
|
- 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)
|
|
|
|
- if keyWordLen <= 0 {
|
|
|
|
- keyWordArr = append(keyWordArr, keyWord)
|
|
|
|
- keyWordLen = len(keyWordArr)
|
|
|
|
- }
|
|
|
|
- keyWordWeight := GetWeight(keyWordLen)
|
|
|
|
-
|
|
|
|
- fmt.Println(keyWordArr)
|
|
|
|
- fmt.Println(keyWordWeight)
|
|
|
|
- for k, v := range keyWordArr {
|
|
|
|
- if v != "" {
|
|
|
|
- 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(weight)
|
|
|
|
- titleFunctionQuery.AddScoreFunc(titleFunctions)
|
|
|
|
- titleFunctionQuery.BoostMode("replace")
|
|
|
|
-
|
|
|
|
- bodyFunctionQuery := elastic.NewFunctionScoreQuery()
|
|
|
|
- bodyFunctionQuery.Query(bodyMatchq)
|
|
|
|
- bodyFunctions := elastic.NewWeightFactorFunction(weight)
|
|
|
|
- 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).From(startSize).Size(pageSize).Query(boolquery)
|
|
|
|
- searchByMatch, err := request.Do(context.Background())
|
|
|
|
- //searchJson, err := json.Marshal(searchByMatch)
|
|
|
|
- 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 EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
- client := utils.Client
|
|
|
|
- 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
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func EsMultiMatchFunctionScoreQueryFix(indexName, keyWord string, startSize, pageSize int) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
- client := utils.Client
|
|
|
|
- keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
|
|
|
|
-
|
|
|
|
- keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
|
- boolquery := elastic.NewBoolQuery()
|
|
|
|
- matchArr := make([]elastic.Query, 0)
|
|
|
|
-
|
|
|
|
- n := 0
|
|
|
|
- keyWordLen := len(keyWordArr)
|
|
|
|
- if keyWordLen <= 0 {
|
|
|
|
- keyWordArr = append(keyWordArr, keyWord)
|
|
|
|
- keyWordLen = len(keyWordArr)
|
|
|
|
- }
|
|
|
|
- fmt.Println(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 {
|
|
|
|
-
|
|
|
|
- matchResult, _ := json.Marshal(searchByMatch)
|
|
|
|
- utils.FileLog.Info("%s", string(matchResult))
|
|
|
|
-
|
|
|
|
- if searchByMatch.Hits != nil {
|
|
|
|
- for _, v := range searchByMatch.Hits.Hits {
|
|
|
|
- articleJson, err := v.Source.MarshalJSON()
|
|
|
|
- utils.FileLog.Info("%s", string(articleJson))
|
|
|
|
- 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
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func GetWeight(length int) []int {
|
|
func GetWeight(length int) []int {
|
|
steep := 10
|
|
steep := 10
|
|
intArr := make([]int, 0)
|
|
intArr := make([]int, 0)
|
|
@@ -937,15 +452,6 @@ func GetArrSum(intArr []int) (sum int) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-//func init() {
|
|
|
|
-// fmt.Println("start")
|
|
|
|
-// keyWord:="阿里巴巴"
|
|
|
|
-// keyWordArr, _ := GetIndustryMapNameSliceV2(keyWord)
|
|
|
|
-// keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
|
-// fmt.Println(keyWordArr)
|
|
|
|
-// fmt.Println("end")
|
|
|
|
-//}
|
|
|
|
-
|
|
|
|
func EsMultiMatchFunctionScoreQuerySort(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 := utils.Client
|
|
client := utils.Client
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
@@ -1378,3 +884,157 @@ func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize
|
|
total += searchByMatch.Hits.TotalHits.Value
|
|
total += searchByMatch.Hits.TotalHits.Value
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func init23423() {
|
|
|
|
+ EsArticleSearch("立高食品", 0, 10, "34")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
|
|
|
|
+ indexName := utils.IndexName
|
|
|
|
+ client := utils.Client
|
|
|
|
+ keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
|
|
|
|
+ keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
|
+ keyWordLen := len(keyWordArr)
|
|
|
|
+ if keyWordLen <= 0 {
|
|
|
|
+ keyWordArr = append(keyWordArr, keyWord)
|
|
|
|
+ keyWordLen = len(keyWordArr)
|
|
|
|
+ }
|
|
|
|
+ //fmt.Println(keyWordArr)
|
|
|
|
+ mustMap := make([]interface{}, 0)
|
|
|
|
+ shouldMap := make(map[string]interface{}, 0)
|
|
|
|
+ //shouldMapquery := make(map[string]interface{}, 0)
|
|
|
|
+ shouldMapquery := make([]interface{}, 0)
|
|
|
|
+ // @Param OrderColumn query int true "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
|
|
|
|
+ //keyWordWeight := GetWeight(keyWordLen)
|
|
|
|
+ var boost int
|
|
|
|
+ lenkeyWordArr := len(keyWordArr)
|
|
|
|
+ for k, v := range keyWordArr {
|
|
|
|
+ if k == 0 {
|
|
|
|
+ boost = 2 * 1000
|
|
|
|
+ } else {
|
|
|
|
+ boost = 1000
|
|
|
|
+ }
|
|
|
|
+ if v != "" {
|
|
|
|
+ shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
|
+ "query": map[string]interface{}{
|
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
|
+ "boost": (lenkeyWordArr - k) * boost, //给查询的值赋予权重
|
|
|
|
+ "fields": []interface{}{"Title"},
|
|
|
|
+ "query": v,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ shouldMapquery = append(shouldMapquery, map[string]interface{}{
|
|
|
|
+ "function_score": map[string]interface{}{
|
|
|
|
+ "query": map[string]interface{}{
|
|
|
|
+ "multi_match": map[string]interface{}{
|
|
|
|
+ "boost": (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
|
|
|
|
+ "fields": []interface{}{"BodyText"},
|
|
|
|
+ "query": v,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ shouldMap = map[string]interface{}{
|
|
|
|
+ "should": shouldMapquery,
|
|
|
|
+ }
|
|
|
|
+ //排序
|
|
|
|
+ sortMap := make([]interface{}, 0)
|
|
|
|
+ //时间
|
|
|
|
+ sortMap = append(sortMap, map[string]interface{}{
|
|
|
|
+ "PublishDate": map[string]interface{}{
|
|
|
|
+ "order": "desc",
|
|
|
|
+ },
|
|
|
|
+ })
|
|
|
|
+ //高亮
|
|
|
|
+ highlightMap := make(map[string]interface{}, 0)
|
|
|
|
+ highlightMap = map[string]interface{}{
|
|
|
|
+ "fields": map[string]interface{}{
|
|
|
|
+ "BodyText": map[string]interface{}{},
|
|
|
|
+ "Title": map[string]interface{}{},
|
|
|
|
+ },
|
|
|
|
+ //样式 红色
|
|
|
|
+ "post_tags": []interface{}{"</font>"},
|
|
|
|
+ "pre_tags": []interface{}{"<font color='red'>"},
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mustMap = append(mustMap, map[string]interface{}{
|
|
|
|
+ "bool": shouldMap,
|
|
|
|
+ })
|
|
|
|
+ queryMap := map[string]interface{}{
|
|
|
|
+ "query": map[string]interface{}{
|
|
|
|
+ "bool": map[string]interface{}{
|
|
|
|
+ "must": mustMap,
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ }
|
|
|
|
+ if orderColumn == "PublishDate" {
|
|
|
|
+ queryMap["sort"] = sortMap
|
|
|
|
+ }
|
|
|
|
+ queryMap["from"] = startSize
|
|
|
|
+ queryMap["size"] = pageSize
|
|
|
|
+ queryMap["highlight"] = highlightMap
|
|
|
|
+ jsonBytes, _ := json.Marshal(queryMap)
|
|
|
|
+ fmt.Println(string(jsonBytes))
|
|
|
|
+ //utils.FileLog.Info(string(jsonBytes))
|
|
|
|
+ request := client.Search(indexName).Source(queryMap) // sets the JSON request
|
|
|
|
+ searchByMatch, err := request.Do(context.Background())
|
|
|
|
+ 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.CygxArticleEs)
|
|
|
|
+ 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
|
|
|
|
+ searchItem.ExpertBackground = article.ExpertBackground
|
|
|
|
+ searchItem.CategoryId = article.CategoryId
|
|
|
|
+ for _, v_result := range result {
|
|
|
|
+ if v_result.ArticleId == searchItem.ArticleId {
|
|
|
|
+ isAppend = true
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if !isAppend {
|
|
|
|
+ result = append(result, searchItem)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ total = searchByMatch.Hits.TotalHits.Value
|
|
|
|
+ }
|
|
|
|
+ //fmt.Println(result)
|
|
|
|
+ //for _, v := range result {
|
|
|
|
+ // fmt.Println(v)
|
|
|
|
+ //}
|
|
|
|
+ return
|
|
|
|
+}
|