|
@@ -2,10 +2,13 @@ package services
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/olivere/elastic/v7"
|
|
|
+ "hongze/hongze_cygx/models"
|
|
|
"log"
|
|
|
"os"
|
|
|
+ "strconv"
|
|
|
)
|
|
|
|
|
|
func NewClient() (client *elastic.Client, err error) {
|
|
@@ -22,7 +25,7 @@ func NewClient() (client *elastic.Client, err error) {
|
|
|
|
|
|
//indexName:索引名称
|
|
|
//mappingJson:表结构
|
|
|
-func CreateIndex(indexName, mappingJson string) (err error) {
|
|
|
+func EsCreateIndex(indexName, mappingJson string) (err error) {
|
|
|
client, err := NewClient()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -43,7 +46,7 @@ func CreateIndex(indexName, mappingJson string) (err error) {
|
|
|
}
|
|
|
|
|
|
//新增数据
|
|
|
-func AddData(indexName, docId string, item interface{}) (err error) {
|
|
|
+func EsAddData(indexName, docId string, item interface{}) (err error) {
|
|
|
client, err := NewClient()
|
|
|
if err != nil {
|
|
|
return
|
|
@@ -52,7 +55,11 @@ func AddData(indexName, docId string, item interface{}) (err error) {
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
- fmt.Println("AddData", resp.Status, resp.Result)
|
|
|
+ if resp.Status == 0 && resp.Result == "created" {
|
|
|
+ fmt.Println("新增成功")
|
|
|
+ } else {
|
|
|
+ fmt.Println("AddData", resp.Status, resp.Result)
|
|
|
+ }
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -66,3 +73,64 @@ func MappingModify(indexName, mappingJson string) {
|
|
|
fmt.Println(result)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+func EsQuery(indexName, keyWord string)(result []*models.SearchItem,err error) {
|
|
|
+ client, err := NewClient()
|
|
|
+ pageSize := 20
|
|
|
+ keyWordArr, err := GetIndustryMapNameSlice(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)
|
|
|
+ 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...)
|
|
|
+
|
|
|
+ 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
|
|
|
+}
|