report.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, classifyIdList []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. "ClassifyId": classifyIdList, //分类必须是正常显示状态
  84. },
  85. },
  86. }
  87. filterMustNot := []map[string]interface{}{
  88. map[string]interface{}{
  89. "term": map[string]interface{}{
  90. "BodyContent.keyword": map[string]interface{}{
  91. "value": "", //过滤没有内容的报告(晨报和周报)bodyContent 不能为空
  92. },
  93. },
  94. },
  95. }
  96. //某章节的报告,若是继承上一期的报告内容且未作修改,搜索匹配项仅展示最新一期该章节的报告。 需求池p2_838
  97. filteCollapse := map[string]interface{}{
  98. "field": "BodyMd5",
  99. }
  100. filterMap := map[string]interface{}{
  101. "bool": map[string]interface{}{
  102. "must": filterMust,
  103. "must_not": filterMustNot,
  104. },
  105. }
  106. source := map[string]interface{}{
  107. "query": map[string]interface{}{
  108. "bool": map[string]interface{}{
  109. "must": must,
  110. "filter": filterMap,
  111. },
  112. },
  113. }
  114. source["from"] = (pageIndex - 1) * pageSize
  115. source["size"] = pageSize
  116. source["collapse"] = filteCollapse
  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. // "pre_tags" : "{{highlight}}",
  123. // "post_tags": "{{/highlight}}",
  124. },
  125. },
  126. "pre_tags": "<span style=\"color:#E3B377\">",
  127. "post_tags": "</span>",
  128. }
  129. source["sort"] = []map[string]interface{}{
  130. map[string]interface{}{
  131. "PublishTime.keyword": map[string]interface{}{
  132. "order": "desc",
  133. },
  134. },
  135. map[string]interface{}{
  136. "_score": map[string]interface{}{
  137. "order": "desc",
  138. },
  139. },
  140. }
  141. jsonstr, err := json.Marshal(source)
  142. fmt.Printf("%s", jsonstr)
  143. request := global.EsClient.Search(indexName).Source(source) // sets the JSON request
  144. searchResp, err = request.Do(context.Background())
  145. if err != nil {
  146. fmt.Print("结果err:")
  147. fmt.Println(err.Error())
  148. return
  149. }
  150. fmt.Print("结果正常:")
  151. fmt.Println(searchResp.Status)
  152. if searchResp.Status != 0 {
  153. err = errors.New("查询失败")
  154. }
  155. total = searchResp.TotalHits()
  156. return
  157. }
  158. // ReportListSearch 报告列表页的搜索
  159. func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  160. indexName := global.CONFIG.EsClient.Prefix + utils.ES_INDEX_RDDP_REPORT
  161. var must []map[string]interface{}
  162. shouldSub := []map[string]interface{}{
  163. map[string]interface{}{
  164. "match": map[string]interface{}{
  165. "Title": map[string]interface{}{
  166. "query": keyWord,
  167. "boost": 3,
  168. },
  169. },
  170. },
  171. map[string]interface{}{
  172. "match": map[string]interface{}{
  173. "StageStr": map[string]interface{}{
  174. "query": keyWord,
  175. "boost": 2,
  176. },
  177. },
  178. },
  179. map[string]interface{}{
  180. "match": map[string]interface{}{
  181. "Abstract": map[string]interface{}{
  182. "query": keyWord,
  183. "boost": 2,
  184. },
  185. },
  186. },
  187. map[string]interface{}{
  188. "match": map[string]interface{}{
  189. "ClassifyNameSecond": map[string]interface{}{
  190. "query": keyWord,
  191. "boost": 2,
  192. },
  193. },
  194. },
  195. map[string]interface{}{
  196. "match_phrase": map[string]interface{}{
  197. "Title": map[string]interface{}{
  198. "query": keyWord,
  199. //"slop": "50",
  200. "boost": 5,
  201. },
  202. },
  203. },
  204. map[string]interface{}{
  205. "match_phrase": map[string]interface{}{
  206. "Abstract": map[string]interface{}{
  207. "query": keyWord,
  208. //"slop": "50",
  209. "boost": 5,
  210. },
  211. },
  212. },
  213. }
  214. mustMap := map[string]interface{}{
  215. "bool": map[string]interface{}{
  216. "should": shouldSub,
  217. },
  218. }
  219. must = append(must, mustMap)
  220. filter := []map[string]interface{}{
  221. map[string]interface{}{
  222. "term": map[string]interface{}{
  223. "PublishState": 2,
  224. },
  225. },
  226. map[string]interface{}{
  227. "term": map[string]interface{}{
  228. "ReportChapterId": 0, //排除章节内容
  229. },
  230. },
  231. map[string]interface{}{
  232. "term": map[string]interface{}{
  233. "ClassifyIdFirst": classifyIdFirst,
  234. },
  235. },
  236. map[string]interface{}{
  237. "terms": map[string]interface{}{
  238. "ClassifyIdSecond": classifyIdSeconds,
  239. },
  240. },
  241. }
  242. source := map[string]interface{}{
  243. "query": map[string]interface{}{
  244. "bool": map[string]interface{}{
  245. "must": must,
  246. "filter": filter,
  247. },
  248. },
  249. }
  250. source["from"] = (pageIndex - 1) * pageSize
  251. source["size"] = pageSize
  252. source["highlight"] = map[string]interface{}{
  253. "fields": map[string]interface{}{
  254. "Title": map[string]interface{}{},
  255. "Abstract": map[string]interface{}{},
  256. "StageStr": map[string]interface{}{},
  257. "ClassifyNameSecond": map[string]interface{}{},
  258. },
  259. "pre_tags": "<span style=\"color:#E3B377\">",
  260. "post_tags": "</span>",
  261. }
  262. source["sort"] = []map[string]interface{}{
  263. map[string]interface{}{
  264. "PublishTime.keyword": map[string]interface{}{
  265. "order": "desc",
  266. },
  267. },
  268. map[string]interface{}{
  269. "_score": map[string]interface{}{
  270. "order": "desc",
  271. },
  272. },
  273. }
  274. jsonstr, err := json.Marshal(source)
  275. fmt.Printf("%s", jsonstr)
  276. request := global.EsClient.Search(indexName).Source(source) // sets the JSON request
  277. searchResp, err = request.Do(context.Background())
  278. if err != nil {
  279. fmt.Print("结果err:")
  280. fmt.Println(err.Error())
  281. return
  282. }
  283. fmt.Print("结果正常:")
  284. fmt.Println(searchResp.Status)
  285. if searchResp.Status != 0 {
  286. err = errors.New("查询失败")
  287. }
  288. total = searchResp.TotalHits()
  289. return
  290. }