report.go 6.9 KB

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