|
@@ -36,11 +36,15 @@ func EsCreateIndex(indexName, mappingJson string) (err error) {
|
|
|
return
|
|
|
}
|
|
|
if !exists {
|
|
|
- _, err = client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
|
|
|
+ resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
|
|
|
//BodyJson(bodyJson).Do(context.Background())
|
|
|
if err != nil {
|
|
|
- return
|
|
|
+ fmt.Println("CreateIndex Err:"+err.Error())
|
|
|
+ return err
|
|
|
}
|
|
|
+ fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
|
|
|
+ }else{
|
|
|
+ fmt.Println(indexName+" 已存在")
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -92,7 +96,7 @@ func MappingModify(indexName, mappingJson string) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func EsQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
|
|
|
+func EsMatchQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
|
|
|
client, err := NewClient()
|
|
|
pageSize := 20
|
|
|
keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
|
|
@@ -156,3 +160,68 @@ func EsQuery(indexName, keyWord string) (result []*models.SearchItem, err error)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func EsMatchPhraseQuery(indexName, keyWord string) (result []*models.SearchItem, err error) {
|
|
|
+ client, err := NewClient()
|
|
|
+ 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.NewTermQuery("BodyText", keyWord)
|
|
|
+ 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--
|
|
|
+ //}
|
|
|
+ //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())
|
|
|
+ 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
|
|
|
+}
|