report.go 3.3 KB

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