123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- 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) {
- 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))
- return
- }
- //indexName:索引名称
- //mappingJson:表结构
- func EsCreateIndex(indexName, mappingJson string) (err error) {
- client, err := NewClient()
- if err != nil {
- return
- }
- //定义表结构
- exists, err := client.IndexExists(indexName).Do(context.Background()) //<5>
- if err != nil {
- return
- }
- if !exists {
- resp, err := client.CreateIndex(indexName).BodyJson(mappingJson).Do(context.Background())
- //BodyJson(bodyJson).Do(context.Background())
- if err != nil {
- fmt.Println("CreateIndex Err:" + err.Error())
- return err
- }
- fmt.Println(resp.Index, resp.ShardsAcknowledged, resp.Acknowledged)
- } else {
- fmt.Println(indexName + " 已存在")
- }
- return
- }
- //新增数据
- func EsAddData(indexName, docId string, item interface{}) (err error) {
- client, err := NewClient()
- if err != nil {
- return
- }
- resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
- if err != nil {
- return
- }
- if resp.Status == 0 && resp.Result == "created" {
- fmt.Println("新增成功")
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- //删除数据
- func EsDeleteData(indexName, docId string) (err error) {
- client, err := NewClient()
- if err != nil {
- return
- }
- resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
- if err != nil {
- return
- }
- if resp.Status == 0 {
- fmt.Println("删除成功")
- } else {
- fmt.Println("AddData", resp.Status, resp.Result)
- }
- return
- }
- func MappingModify(indexName, mappingJson string) {
- client, err := NewClient()
- if err != nil {
- return
- }
- result, err := client.PutMapping().Index(indexName).BodyString(mappingJson).Do(context.Background())
- fmt.Println(err)
- fmt.Println(result)
- return
- }
- func EsMatchQuery(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")
- 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...)
- 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)
- if len(v.Highlight["BodyText"]) > 0 {
- searchItem.Body = v.Highlight["BodyText"]
- } else {
- bodyRune := []rune(article.BodyText)
- bodyRuneLen:=len(bodyRune)
- if bodyRuneLen>100 {
- bodyRuneLen=100
- }
- body:= string(bodyRune[:bodyRuneLen])
- fmt.Println(body)
- searchItem.Body = []string{body}
- }
- 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
- }
- 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.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart")
- matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord)
- matchArr = append(matchArr, matchq1)
- matchArr = append(matchArr, matchq2)
- //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())
- fmt.Println("err:", err, searchByMatch)
- return
- 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
- }
|