elastic.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package services
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/olivere/elastic/v7"
  8. "hongze/hongze_api/utils"
  9. )
  10. const (
  11. ES_URL = "http://es-cn-nif227b580019rgw6.public.elasticsearch.aliyuncs.com:9200" //<1>
  12. ES_USERNAME = "elastic" //<2>
  13. ES_PASSWORD = "hongze@2021" //<3>
  14. //Grafana pwd-> 20521bb9
  15. //Grafana username-> emon
  16. )
  17. func NewClient() (client *elastic.Client, err error) {
  18. client, err = elastic.NewClient(
  19. elastic.SetURL(ES_URL),
  20. elastic.SetBasicAuth(ES_USERNAME, ES_PASSWORD),
  21. elastic.SetSniff(false))
  22. return
  23. }
  24. // SearchESEnglishReport 查询es中的指标数据
  25. func SearchESEnglishReport(keyWord string, from, size int) (searchResp *elastic.SearchResult, total int64, err error) {
  26. indexName := utils.EsEnglishReportIndexName
  27. client, err := NewClient()
  28. if err != nil {
  29. return
  30. }
  31. var must []map[string]interface{}
  32. shouldSub := []map[string]interface{}{
  33. map[string]interface{}{
  34. "match": map[string]interface{}{
  35. "Title": map[string]interface{}{
  36. "query": keyWord,
  37. //"minimum_should_match": "60%",
  38. "boost": 3,
  39. },
  40. },
  41. },
  42. map[string]interface{}{
  43. "match": map[string]interface{}{
  44. "BodyContent": map[string]interface{}{
  45. "query": keyWord,
  46. //"minimum_should_match": "60%",
  47. "boost": 1,
  48. },
  49. },
  50. },
  51. map[string]interface{}{
  52. "match_phrase": map[string]interface{}{
  53. "Title": map[string]interface{}{
  54. "query": keyWord,
  55. //"slop": "50",
  56. "boost": 5,
  57. },
  58. },
  59. },
  60. map[string]interface{}{
  61. "match_phrase": map[string]interface{}{
  62. "BodyContent": map[string]interface{}{
  63. "query": keyWord,
  64. "boost": 3,
  65. },
  66. },
  67. },
  68. }
  69. mustMap := map[string]interface{}{
  70. "bool": map[string]interface{}{
  71. "should": shouldSub,
  72. },
  73. }
  74. must = append(must, mustMap)
  75. filterMust := []map[string]interface{}{
  76. map[string]interface{}{
  77. "term": map[string]interface{}{
  78. "PublishState": 2, //必须是已发布的报告
  79. },
  80. },
  81. }
  82. filterMap := map[string]interface{}{
  83. "bool": map[string]interface{}{
  84. "must": filterMust,
  85. },
  86. }
  87. source := map[string]interface{}{
  88. "query": map[string]interface{}{
  89. "bool": map[string]interface{}{
  90. "must": must,
  91. "filter": filterMap,
  92. },
  93. },
  94. }
  95. source["from"] = from
  96. source["size"] = size
  97. source["highlight"] = map[string]interface{}{
  98. "fields": map[string]interface{}{
  99. "Title":map[string]interface{}{},
  100. "BodyContent":map[string]interface{}{
  101. // "pre_tags" : "{{highlight}}",
  102. // "post_tags": "{{/highlight}}",
  103. },
  104. },
  105. "pre_tags" : "<span style=\"color:#E3B377\">",
  106. "post_tags": "</span>",
  107. }
  108. source["sort"] = []map[string]interface{}{
  109. map[string]interface{}{
  110. "PublishTime.keyword":map[string]interface{}{
  111. "order":"desc",
  112. },
  113. },
  114. map[string]interface{}{
  115. "_score":map[string]interface{}{
  116. "order":"desc",
  117. },
  118. },
  119. }
  120. jsonstr, err := json.Marshal(source)
  121. fmt.Printf("%s",jsonstr)
  122. request := client.Search(indexName).Source(source) // sets the JSON request
  123. //requestJson, err := json.Marshal(request)
  124. //if err != nil {
  125. // fmt.Println("requestJson err:", err)
  126. //}
  127. //fmt.Println("requestJson ", string(requestJson)
  128. searchResp, err = request.Do(context.Background())
  129. if err != nil {
  130. fmt.Print("结果err:")
  131. fmt.Println(err.Error())
  132. return
  133. }
  134. fmt.Print("结果正常:")
  135. fmt.Println(searchResp.Status)
  136. if searchResp.Status != 0 {
  137. err = errors.New("查询失败")
  138. }
  139. total = searchResp.TotalHits()
  140. return
  141. }