report.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/olivere/elastic/v7"
  8. "hongze/hongze_yb/global"
  9. "hongze/hongze_yb/utils"
  10. )
  11. // 首页搜索
  12. func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  13. indexName := global.CONFIG.EsClient.Prefix+utils.ES_INDEX_RDDP_REPORT
  14. var must []map[string]interface{}
  15. shouldSub := []map[string]interface{}{
  16. map[string]interface{}{
  17. "match": map[string]interface{}{
  18. "Title": map[string]interface{}{
  19. "query": keyWord,
  20. //"minimum_should_match": "60%",
  21. "boost": 3,
  22. },
  23. },
  24. },
  25. map[string]interface{}{
  26. "match": map[string]interface{}{
  27. "Categories": map[string]interface{}{
  28. "query": keyWord,
  29. //"minimum_should_match": "60%",
  30. "boost": 2,
  31. },
  32. },
  33. },
  34. map[string]interface{}{
  35. "match": map[string]interface{}{
  36. "BodyContent": map[string]interface{}{
  37. "query": keyWord,
  38. //"minimum_should_match": "60%",
  39. "boost": 1,
  40. },
  41. },
  42. },
  43. map[string]interface{}{
  44. "match_phrase": map[string]interface{}{
  45. "Title": map[string]interface{}{
  46. "query": keyWord,
  47. //"slop": "50",
  48. "boost": 5,
  49. },
  50. },
  51. },
  52. map[string]interface{}{
  53. "match_phrase": map[string]interface{}{
  54. "Categories": map[string]interface{}{
  55. "query": keyWord,
  56. "boost": 4,
  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. map[string]interface{}{
  82. "terms": map[string]interface{}{
  83. "ClassifyIdFirst":classifyIdFirsts, //分类必须是正常显示状态
  84. },
  85. },
  86. map[string]interface{}{
  87. "terms": map[string]interface{}{
  88. "ClassifyIdSecond":classifyIdSeconds, //分类必须是正常显示状态
  89. },
  90. },
  91. }
  92. filterMustNot := []map[string]interface{}{
  93. map[string]interface{}{
  94. "term": map[string]interface{}{
  95. "BodyContent.keyword": map[string]interface{}{
  96. "value": "", //过滤没有内容的报告(晨报和周报)bodyContent 不能为空
  97. },
  98. },
  99. },
  100. }
  101. filterMap := map[string]interface{}{
  102. "bool": map[string]interface{}{
  103. "must": filterMust,
  104. "must_not": filterMustNot,
  105. },
  106. }
  107. source := map[string]interface{}{
  108. "query": map[string]interface{}{
  109. "bool": map[string]interface{}{
  110. "must": must,
  111. "filter": filterMap,
  112. },
  113. },
  114. }
  115. source["from"] = (pageIndex - 1) * pageSize
  116. source["size"] = pageSize
  117. source["highlight"] = map[string]interface{}{
  118. "fields": map[string]interface{}{
  119. "Title":map[string]interface{}{},
  120. "Categories":map[string]interface{}{},
  121. "BodyContent":map[string]interface{}{},
  122. },
  123. "pre_tags" : "<span style=\"color:#efefef\">",
  124. "post_tags": "</span>",
  125. }
  126. source["sort"] = []map[string]interface{}{
  127. map[string]interface{}{
  128. "_score":map[string]interface{}{
  129. "order":"desc",
  130. },
  131. },
  132. map[string]interface{}{
  133. "PublishTime.keyword":map[string]interface{}{
  134. "order":"desc",
  135. },
  136. },
  137. }
  138. jsonstr, err := json.Marshal(source)
  139. fmt.Printf("%s",jsonstr)
  140. request := global.EsClient.Search(indexName).Source(source) // sets the JSON request
  141. searchResp, err = request.Do(context.Background())
  142. if err != nil {
  143. fmt.Print("结果err:")
  144. fmt.Println(err.Error())
  145. return
  146. }
  147. fmt.Print("结果正常:")
  148. fmt.Println(searchResp.Status)
  149. if searchResp.Status != 0 {
  150. err = errors.New("查询失败")
  151. }
  152. total = searchResp.TotalHits()
  153. return
  154. }
  155. // ReportListSearch 报告列表页的搜索
  156. func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  157. indexName := global.CONFIG.EsClient.Prefix+utils.ES_INDEX_RDDP_REPORT
  158. var must []map[string]interface{}
  159. shouldSub := []map[string]interface{}{
  160. map[string]interface{}{
  161. "match": map[string]interface{}{
  162. "Title": map[string]interface{}{
  163. "query": keyWord,
  164. "boost": 3,
  165. },
  166. },
  167. },
  168. map[string]interface{}{
  169. "match": map[string]interface{}{
  170. "StageStr": map[string]interface{}{
  171. "query": keyWord,
  172. "boost": 2,
  173. },
  174. },
  175. },
  176. map[string]interface{}{
  177. "match": map[string]interface{}{
  178. "Abstract": map[string]interface{}{
  179. "query": keyWord,
  180. "boost": 2,
  181. },
  182. },
  183. },
  184. map[string]interface{}{
  185. "match_phrase": map[string]interface{}{
  186. "Title": map[string]interface{}{
  187. "query": keyWord,
  188. //"slop": "50",
  189. "boost": 5,
  190. },
  191. },
  192. },
  193. map[string]interface{}{
  194. "match_phrase": map[string]interface{}{
  195. "Abstract": map[string]interface{}{
  196. "query": keyWord,
  197. //"slop": "50",
  198. "boost": 5,
  199. },
  200. },
  201. },
  202. }
  203. mustMap := map[string]interface{}{
  204. "bool": map[string]interface{}{
  205. "should": shouldSub,
  206. },
  207. }
  208. must = append(must, mustMap)
  209. filter := []map[string]interface{}{
  210. map[string]interface{}{
  211. "term": map[string]interface{}{
  212. "PublishState": 2,
  213. },
  214. },
  215. map[string]interface{}{
  216. "term": map[string]interface{}{
  217. "ReportChapterId":0, //排除章节内容
  218. },
  219. },
  220. map[string]interface{}{
  221. "term": map[string]interface{}{
  222. "ClassifyIdFirst":classifyIdFirst,
  223. },
  224. },
  225. map[string]interface{}{
  226. "terms": map[string]interface{}{
  227. "ClassifyIdSecond": classifyIdSeconds,
  228. },
  229. },
  230. }
  231. source := map[string]interface{}{
  232. "query": map[string]interface{}{
  233. "bool": map[string]interface{}{
  234. "must": must,
  235. "filter": filter,
  236. },
  237. },
  238. }
  239. source["from"] = (pageIndex - 1) * pageSize
  240. source["size"] = pageSize
  241. source["highlight"] = map[string]interface{}{
  242. "fields": map[string]interface{}{
  243. "Title":map[string]interface{}{},
  244. "Abstract":map[string]interface{}{},
  245. "StageStr":map[string]interface{}{},
  246. },
  247. "pre_tags" : "<span style=\"color:#efefef\">",
  248. "post_tags": "</span>",
  249. }
  250. source["sort"] = []map[string]interface{}{
  251. map[string]interface{}{
  252. "_score":map[string]interface{}{
  253. "order":"desc",
  254. },
  255. },
  256. map[string]interface{}{
  257. "PublishTime.keyword":map[string]interface{}{
  258. "order":"desc",
  259. },
  260. },
  261. }
  262. jsonstr, err := json.Marshal(source)
  263. fmt.Printf("%s",jsonstr)
  264. request := global.EsClient.Search(indexName).Source(source) // sets the JSON request
  265. searchResp, err = request.Do(context.Background())
  266. if err != nil {
  267. fmt.Print("结果err:")
  268. fmt.Println(err.Error())
  269. return
  270. }
  271. fmt.Print("结果正常:")
  272. fmt.Println(searchResp.Status)
  273. if searchResp.Status != 0 {
  274. err = errors.New("查询失败")
  275. }
  276. total = searchResp.TotalHits()
  277. return
  278. }