|
@@ -463,7 +463,6 @@ func SearchByKeyWordQuery(keyWord string) (result []*models.SearchItem, err erro
|
|
|
if searchByMatch.Hits != nil {
|
|
|
for _, v := range searchByMatch.Hits.Hits {
|
|
|
articleJson, _ := v.Source.MarshalJSON()
|
|
|
- utils.FileLog.Info("%s", string(articleJson))
|
|
|
fmt.Println(string(articleJson))
|
|
|
}
|
|
|
}
|
|
@@ -505,9 +504,6 @@ func SearchByKeyWord(keyWord string) (result []*models.SearchItem, err error) {
|
|
|
//.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()
|
|
|
|
|
|
keyLen := len(keyWordArr)
|
|
@@ -644,110 +640,4 @@ func AddMap() {
|
|
|
return
|
|
|
}
|
|
|
fmt.Println(resp)
|
|
|
- //mappping := make(map[string]interface{})
|
|
|
- //mappping[esMappingIndex] = bodyJson
|
|
|
- //putResult,err:=client.PutMapping().Index(esIndex).BodyJson(mappping).Do(context.Background())
|
|
|
- //mapping,err := client.GetMapping().Index(esIndex).Do(context.Background())
|
|
|
- //fmt.Println(mapping)
|
|
|
- //mapJson, err := json.Marshal(mapping)
|
|
|
- //utils.FileLog.Info("%s", string(mapJson))
|
|
|
- //fmt.Println(esMappingIndex)
|
|
|
- //
|
|
|
- //fmt.Println(err)
|
|
|
- //fmt.Println(putResult)
|
|
|
-}
|
|
|
-
|
|
|
-func Search(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"
|
|
|
- var esIndex = "article_two"
|
|
|
- searchMap := make(map[int]int)
|
|
|
-
|
|
|
- boolquery := elastic.NewBoolQuery()
|
|
|
-
|
|
|
- keyLen := len(keyWordArr)
|
|
|
- n := float64(keyLen)
|
|
|
- matchArr := make([]elastic.Query, 0)
|
|
|
- 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--
|
|
|
- }
|
|
|
- //matchArr=append(matchArr,matchq1)
|
|
|
- //matchArr=append(matchArr,matchq2)
|
|
|
- boolquery.Should().Must(matchArr...)
|
|
|
- //boolquery.Must(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
|
|
|
- }
|
|
|
-
|
|
|
- jsonResult,err:=json.Marshal(searchByMatch.Hits.Hits)
|
|
|
- utils.FileLog.Info("%s",string(jsonResult))
|
|
|
- return
|
|
|
- if searchByMatch.Hits != nil {
|
|
|
- for _, v := range searchByMatch.Hits.Hits {
|
|
|
- articleJson, err := v.Source.MarshalJSON()
|
|
|
- fmt.Println(string(articleJson))
|
|
|
- utils.FileLog.Info("%s",string(articleJson))
|
|
|
- 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
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- body, err := json.Marshal(searchMap)
|
|
|
- utils.FileLog.Info("%s", string(body))
|
|
|
- return
|
|
|
}
|