|
@@ -446,8 +446,10 @@ func SearchByKeyWordQuery(keyWord string) (result []*models.SearchItem, err erro
|
|
|
|
|
|
var esIndex = "cygx_article"
|
|
|
|
|
|
- keyWordStr := "医疗"
|
|
|
- queryString := elastic.NewQueryStringQuery(`Title:(` + keyWordStr + `) BodyText:(` + keyWordStr + `)`).Analyzer("ik_smart")
|
|
|
+ //keyWordStr := "医疗"
|
|
|
+ //字段相等
|
|
|
+ queryString := elastic.NewQueryStringQuery("Title:医疗")
|
|
|
+ //.Analyzer("ik_smart")
|
|
|
highlight := elastic.NewHighlight()
|
|
|
highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
@@ -455,7 +457,7 @@ func SearchByKeyWordQuery(keyWord string) (result []*models.SearchItem, err erro
|
|
|
|
|
|
searchByMatch, err := request.Do(context.Background())
|
|
|
if err != nil {
|
|
|
- fmt.Println("request.Do err:",err.Error())
|
|
|
+ fmt.Println("request.Do err:", err.Error())
|
|
|
return result, err
|
|
|
}
|
|
|
if searchByMatch.Hits != nil {
|
|
@@ -467,3 +469,89 @@ func SearchByKeyWordQuery(keyWord string) (result []*models.SearchItem, err erro
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func SearchByKeyWordBack02(keyWord string) (result []*models.SearchItem, err error) {
|
|
|
+ fmt.Println("keyWord:", keyWord)
|
|
|
+ pageSize := 20
|
|
|
+ keyWordArr, err := GetIndustryMapNameSlice(keyWord)
|
|
|
+ keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
|
|
|
+
|
|
|
+ fmt.Println(keyWordArr)
|
|
|
+ fmt.Println(" keyWordArr ")
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "GetIndustryMapNameSlice:"+err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
|
|
|
+ file := "./rdlucklog/eslog.log"
|
|
|
+ logFile, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
|
|
|
+
|
|
|
+ client, err := elastic.NewClient(
|
|
|
+ elastic.SetURL(ES_URL),
|
|
|
+ elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
|
|
|
+ elastic.SetTraceLog(log.New(logFile, "ES-TRACE: ", 0)),
|
|
|
+ elastic.SetSniff(false), elastic.SetErrorLog(errorlog))
|
|
|
+
|
|
|
+ var esIndex = "cygx_article"
|
|
|
+ searchMap := make(map[int]int)
|
|
|
+ keyWordStr := strings.Join(keyWordArr, ",")
|
|
|
+
|
|
|
+ fmt.Println("keyWordStr ", keyWordStr)
|
|
|
+ keyWordStr = "费用"
|
|
|
+ keyWord=keyWordStr
|
|
|
+ //queryString := elastic.NewQueryStringQuery(`Title:医疗 BodyText:医疗`).Analyzer("ik_smart")
|
|
|
+
|
|
|
+ //queryTeerms:=elastic.NewTermsQuery("Title","医疗","费用")
|
|
|
+
|
|
|
+ //queryMatchPhrase:=elastic.NewMatchPhraseQuery("Title","费用")
|
|
|
+ //.Analyzer("ik_smart")
|
|
|
+ //.Analyzer("ik_smart")
|
|
|
+ //boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
|
|
|
+ mapping:=client.GetMapping()
|
|
|
+ mapJson,err:=json.Marshal(mapping)
|
|
|
+ utils.FileLog.Info("%s",string(mapJson))
|
|
|
+ boolquery := elastic.NewBoolQuery()
|
|
|
+ boolquery.Must(elastic.NewMatchQuery("Title", keyWord).Boost(2).Analyzer("ik_smart"), elastic.NewMatchQuery("BodyText", keyWord).Boost(1).Analyzer("ik_smart"))
|
|
|
+
|
|
|
+ highlight := elastic.NewHighlight()
|
|
|
+ highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
|
+ highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
+ //request := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(queryString)
|
|
|
+ request := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery)
|
|
|
+ searchByMatch, err := request.Do(context.Background())
|
|
|
+ fmt.Println(searchByMatch, err)
|
|
|
+ //searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Do(context.Background())
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+ fmt.Println(searchByMatch.Status, searchByMatch.TotalHits())
|
|
|
+ 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
|
|
|
+}
|