|
@@ -53,7 +53,7 @@ func EsCreateIndex(indexName, mappingJson string) (err error) {
|
|
|
}
|
|
|
|
|
|
//新增数据
|
|
|
-func EsAddOrEditData(indexName, docId string, item *ElasticArticleDetail) (err error) {
|
|
|
+func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (err error) {
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
fmt.Println("EsAddOrEditData Err:", err.Error())
|
|
@@ -70,11 +70,8 @@ func EsAddOrEditData(indexName, docId string, item *ElasticArticleDetail) (err e
|
|
|
}
|
|
|
if searchById != nil && searchById.Found {
|
|
|
resp, err := client.Update().Index(indexName).Id(docId).Doc(map[string]interface{}{
|
|
|
- "BodyText": item.BodyText,
|
|
|
- "Title": item.Title,
|
|
|
- "PublishDate": item.PublishDate,
|
|
|
- "TitleEn": item.TitleEn,
|
|
|
- "CreateDate": item.CreateDate,
|
|
|
+ "BodyText": item.BodyText,
|
|
|
+ "Title": item.Title,
|
|
|
}).Do(context.Background())
|
|
|
if err != nil {
|
|
|
return err
|
|
@@ -388,6 +385,82 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
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:", 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
|
|
|
+ }
|
|
|
+ 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, err := NewClient()
|
|
|
+ keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
|
|
|
+
|
|
|
+ keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
+ boolquery := elastic.NewBoolQuery()
|
|
|
+ matchArr := make([]elastic.Query, 0)
|
|
|
+
|
|
|
n := 0
|
|
|
keyWordLen := len(keyWordArr)
|
|
|
if keyWordLen <= 0 {
|
|
@@ -417,6 +490,10 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
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()
|
|
@@ -440,7 +517,6 @@ func EsMultiMatchFunctionScoreQuery(indexName, keyWord string, startSize, pageSi
|
|
|
bodyRuneLen = 100
|
|
|
}
|
|
|
body := string(bodyRune[:bodyRuneLen])
|
|
|
- fmt.Println(body)
|
|
|
searchItem.Body = []string{body}
|
|
|
}
|
|
|
var title string
|