|
@@ -604,6 +604,10 @@ func AddMap() {
|
|
|
}`
|
|
|
//var esMappingIndex = "article_mapping"
|
|
|
var esIndex = "article_two"
|
|
|
+ a := client.IndexAnalyze().Index(esIndex)
|
|
|
+
|
|
|
+ analyzeBody, err := json.Marshal(a)
|
|
|
+ fmt.Println(string(analyzeBody))
|
|
|
|
|
|
exists, err := client.IndexExists(esIndex).Do(context.Background()) //<5>
|
|
|
if err != nil {
|
|
@@ -620,7 +624,7 @@ func AddMap() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- item, err := models.GetArticleDetailTestById(3584)
|
|
|
+ item, err := models.GetArticleDetailTestById(3546)
|
|
|
if err != nil {
|
|
|
fmt.Println("GetArticleDetailById Err:" + err.Error())
|
|
|
return
|
|
@@ -652,3 +656,98 @@ func AddMap() {
|
|
|
//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
|
|
|
+}
|