go-elasticsearch.go 3.2 KB

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