report.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "Title": 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_phrase": map[string]interface{}{
  38. "Title": map[string]interface{}{
  39. "query": keyWord,
  40. //"slop": "50",
  41. "boost": 5,
  42. },
  43. },
  44. },
  45. map[string]interface{}{
  46. "match_phrase": map[string]interface{}{
  47. "BodyContent": map[string]interface{}{
  48. "query": keyWord,
  49. "boost": 3,
  50. },
  51. },
  52. },
  53. }
  54. mustMap := map[string]interface{}{
  55. "bool": map[string]interface{}{
  56. "should": shouldSub,
  57. },
  58. }
  59. must = append(must, mustMap)
  60. filterMust := []map[string]interface{}{
  61. map[string]interface{}{
  62. "term": map[string]interface{}{
  63. "PublishState": 2, //必须是已发布的报告
  64. },
  65. },
  66. }
  67. filterMap := map[string]interface{}{
  68. "bool": map[string]interface{}{
  69. "must": filterMust,
  70. },
  71. }
  72. source := map[string]interface{}{
  73. "query": map[string]interface{}{
  74. "bool": map[string]interface{}{
  75. "must": must,
  76. "filter": filterMap,
  77. },
  78. },
  79. }
  80. source["from"] = from
  81. source["size"] = size
  82. source["highlight"] = map[string]interface{}{
  83. "fields": map[string]interface{}{
  84. "Title":map[string]interface{}{},
  85. "BodyContent":map[string]interface{}{
  86. // "pre_tags" : "{{highlight}}",
  87. // "post_tags": "{{/highlight}}",
  88. },
  89. },
  90. "pre_tags" : "<span style=\"color:#00459F\">",
  91. "post_tags": "</span>",
  92. }
  93. source["sort"] = []map[string]interface{}{
  94. map[string]interface{}{
  95. "PublishTime.keyword":map[string]interface{}{
  96. "order":"desc",
  97. },
  98. },
  99. map[string]interface{}{
  100. "_score":map[string]interface{}{
  101. "order":"desc",
  102. },
  103. },
  104. }
  105. jsonstr, err := json.Marshal(source)
  106. fmt.Printf("%s",jsonstr)
  107. request := client.Search(indexName).Source(source) // sets the JSON request
  108. //requestJson, err := json.Marshal(request)
  109. //if err != nil {
  110. // fmt.Println("requestJson err:", err)
  111. //}
  112. //fmt.Println("requestJson ", string(requestJson)
  113. searchResp, err = request.Do(context.Background())
  114. if err != nil {
  115. fmt.Print("结果err:")
  116. fmt.Println(err.Error())
  117. return
  118. }
  119. fmt.Print("结果正常:")
  120. fmt.Println(searchResp.Status)
  121. if searchResp.Status != 0 {
  122. err = errors.New("查询失败")
  123. }
  124. total = searchResp.TotalHits()
  125. return
  126. }