go-elasticsearch.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package services
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/elastic/go-elasticsearch/v7"
  8. "hongze/hongze_cygx/models"
  9. "hongze/hongze_cygx/utils"
  10. "io/ioutil"
  11. )
  12. /*
  13. Addresses []string // A list of Elasticsearch nodes to use.
  14. Logger estransport.Logger // The logger object.
  15. Selector estransport.Selector // The selector object.
  16. */
  17. //官方库
  18. func NewEsV7Client() (client *elasticsearch.Client, err error) {
  19. conf := elasticsearch.Config{
  20. Addresses: []string{ES_URL},
  21. Username: ES_USERNAME,
  22. Password: ES_PASSWORD,
  23. }
  24. client, err = elasticsearch.NewClient(conf)
  25. return
  26. }
  27. func EsV7Search(indexName, keyWord string) (result []*models.SearchItem, err error) {
  28. //pageSize := 20
  29. client, err := NewEsV7Client()
  30. if err != nil {
  31. fmt.Println("NewEsV7Client Err:", err.Error())
  32. return
  33. }
  34. var buf bytes.Buffer
  35. query := map[string]interface{}{
  36. "query": map[string]interface{}{
  37. "match_phrase": map[string]interface{}{
  38. "BodyText": map[string]interface{}{
  39. "query": "康立明",
  40. },
  41. },
  42. },
  43. "highlight": map[string]interface{}{
  44. "pre_tags": []string{"<font color='red'>"},
  45. "post_tags": []string{"</font>"},
  46. "fields": map[string]interface{}{
  47. "BodyText": map[string]interface{}{},
  48. },
  49. },
  50. }
  51. queryJson,err:=json.Marshal(query)
  52. utils.FileLog.Info("%s",string(queryJson))
  53. if err := json.NewEncoder(&buf).Encode(query); err != nil {
  54. fmt.Println(err, "Error encoding query")
  55. }
  56. res, err := client.Search(
  57. client.Search.WithIndex(indexName),
  58. client.Search.WithBody(&buf),
  59. client.Search.WithTrackTotalHits(true),
  60. client.Search.WithPretty(),
  61. client.Search.WithContext(context.Background()),
  62. )
  63. if err != nil {
  64. fmt.Println("search err:", err)
  65. }
  66. defer res.Body.Close()
  67. body, err := ioutil.ReadAll(res.Body)
  68. utils.FileLog.Info("%s", string(body))
  69. //boolQuery := elastic.NewBoolQuery()
  70. //
  71. //matchArr := make([]elastic.Query, 0)
  72. ////matchq1 := elastic.NewMatchQuery("Title", keyWord).Boost(n + 1).Analyzer("ik_smart")
  73. ////matchq2 := elastic.NewMatchQuery("BodyText", keyWord).Boost(n + 1).Analyzer("ik_smart")
  74. //matchq1 := elastic.NewMatchPhraseQuery("Title", keyWord) //.Analyzer("ik_smart")
  75. //matchq2 := elastic.NewMatchPhraseQuery("BodyText", keyWord)
  76. //matchArr = append(matchArr, matchq1)
  77. //matchArr = append(matchArr, matchq2)
  78. ////matchArr = append(matchArr, matchq2)
  79. ////for _, v := range keyWordArr {
  80. //// if v != "" {
  81. //// matchq1 := elastic.NewMatchQuery("Title", v).Boost(n).Analyzer("ik_smart")
  82. //// matchq2 := elastic.NewMatchQuery("BodyText", v).Boost(n).Analyzer("ik_smart")
  83. //// matchArr = append(matchArr, matchq1)
  84. //// matchArr = append(matchArr, matchq2)
  85. //// }
  86. //// n--
  87. ////}
  88. ////boolquery.Should(matchArr...)
  89. //
  90. //boolQuery.Should(matchArr...)
  91. //
  92. //highlight := elastic.NewHighlight()
  93. //highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
  94. //highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  95. //
  96. //service := client.Search().Index(indexName).Highlight(highlight).Query(boolQuery).Size(pageSize)
  97. //resp, err := service.Do(context.Background())
  98. //if err != nil {
  99. // return nil, err
  100. //}
  101. //
  102. //if resp.TotalHits() == 0 {
  103. // return nil, nil
  104. //}
  105. //for _, e := range resp.Each(reflect.TypeOf(&models.SearchItem{})) {
  106. // us := e.(*models.SearchItem)
  107. // result = append(result, us)
  108. //}
  109. //return result, nil
  110. return
  111. }