report.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_mini_bridge/utils"
  7. "fmt"
  8. "github.com/olivere/elastic/v7"
  9. )
  10. // 首页搜索
  11. func SearchReport(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  12. indexName := utils.EsReportIndexName
  13. var must []map[string]interface{}
  14. shouldSub := []map[string]interface{}{
  15. map[string]interface{}{
  16. "match_phrase": map[string]interface{}{
  17. "Title": map[string]interface{}{
  18. "query": keyWord,
  19. //"slop": "50",
  20. "boost": 5,
  21. },
  22. },
  23. },
  24. // map[string]interface{}{
  25. // "match_phrase": map[string]interface{}{
  26. // "Categories": map[string]interface{}{
  27. // "query": keyWord,
  28. // "boost": 4,
  29. // },
  30. // },
  31. // },
  32. // map[string]interface{}{
  33. // "match_phrase": map[string]interface{}{
  34. // "BodyContent": map[string]interface{}{
  35. // "query": keyWord,
  36. // "boost": 3,
  37. // },
  38. // },
  39. // },
  40. }
  41. mustMap := map[string]interface{}{
  42. "bool": map[string]interface{}{
  43. "should": shouldSub,
  44. },
  45. }
  46. must = append(must, mustMap)
  47. filterMust := []map[string]interface{}{
  48. map[string]interface{}{
  49. "term": map[string]interface{}{
  50. "PublishState": 2, //必须是已发布的报告
  51. },
  52. },
  53. // map[string]interface{}{
  54. // "terms": map[string]interface{}{
  55. // "ClassifyIdFirst": classifyIdFirsts, //分类必须是正常显示状态
  56. // },
  57. // },
  58. // map[string]interface{}{
  59. // "terms": map[string]interface{}{
  60. // "ClassifyIdSecond": classifyIdSeconds, //分类必须是正常显示状态
  61. // },
  62. // },
  63. }
  64. // filterMustNot := []map[string]interface{}{
  65. // map[string]interface{}{
  66. // "term": map[string]interface{}{
  67. // "BodyContent.keyword": map[string]interface{}{
  68. // "value": "", //过滤没有内容的报告(晨报和周报)bodyContent 不能为空
  69. // },
  70. // },
  71. // },
  72. // }
  73. filterMap := map[string]interface{}{
  74. "bool": map[string]interface{}{
  75. "must": filterMust,
  76. // "must_not": filterMustNot,
  77. },
  78. }
  79. source := map[string]interface{}{
  80. "query": map[string]interface{}{
  81. "bool": map[string]interface{}{
  82. "must": must,
  83. "filter": filterMap,
  84. },
  85. },
  86. }
  87. source["from"] = (pageIndex - 1) * pageSize
  88. source["size"] = pageSize
  89. source["highlight"] = map[string]interface{}{
  90. "fields": map[string]interface{}{
  91. "Title": map[string]interface{}{},
  92. // "Categories": map[string]interface{}{},
  93. // "BodyContent": map[string]interface{}{
  94. // // "pre_tags" : "{{highlight}}",
  95. // // "post_tags": "{{/highlight}}",
  96. // },
  97. },
  98. // "pre_tags": "<span style=\"color:#E3B377\">",
  99. "pre_tags": "<span class=\"report-title_color\">",
  100. "post_tags": "</span>",
  101. }
  102. source["sort"] = []map[string]interface{}{
  103. map[string]interface{}{
  104. "PublishTime.keyword": map[string]interface{}{
  105. "order": "desc",
  106. },
  107. },
  108. map[string]interface{}{
  109. "_score": map[string]interface{}{
  110. "order": "desc",
  111. },
  112. },
  113. }
  114. jsonstr, err := json.Marshal(source)
  115. fmt.Printf("%s", jsonstr)
  116. request := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
  117. // request := utils.EsClient.Search().Index(indice...).Source(source) // sets the JSON request
  118. fmt.Println(source)
  119. searchResp, err = request.Do(context.Background())
  120. if err != nil {
  121. fmt.Print("结果err:")
  122. fmt.Println(err.Error())
  123. return
  124. }
  125. fmt.Print("结果正常:")
  126. fmt.Println(searchResp.Status)
  127. if searchResp.Status != 0 {
  128. err = errors.New("查询失败")
  129. }
  130. total = searchResp.TotalHits()
  131. return
  132. }
  133. func SearchReportV2(keyWord string, classifyIdFirsts []int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  134. indices := []string{utils.EsReportIndexName, utils.MINI_REPORT_INDEX_NAME}
  135. query := elastic.NewBoolQuery().
  136. Filter(
  137. elastic.NewBoolQuery().Should(
  138. elastic.NewTermQuery("PublishState", 2),
  139. elastic.NewTermQuery("State", 1),
  140. ),
  141. ).
  142. Must(
  143. elastic.NewBoolQuery().Should(
  144. elastic.NewMatchPhraseQuery("Title", keyWord).Boost(5),
  145. ),
  146. )
  147. sort1 := elastic.NewFieldSort("PublishTime.keyword").Desc()
  148. sort2 := elastic.NewScoreSort().Desc()
  149. searchResp, err = utils.EsClient.Search().
  150. Index(indices...).
  151. Query(query).
  152. SortBy(sort1, sort2).
  153. Highlight(elastic.NewHighlight().
  154. PreTags("<span class=\"report-title_color\">").
  155. PostTags("</span>").
  156. Field("Title")).
  157. From((pageIndex - 1) * pageSize).
  158. Size(pageSize).
  159. Pretty(true).
  160. Do(context.Background())
  161. // searchResp, err = request.Do(context.Background())
  162. if err != nil {
  163. fmt.Print("结果err:")
  164. fmt.Println(err.Error())
  165. return
  166. }
  167. total = searchResp.TotalHits()
  168. if searchResp.Status != 0 {
  169. err = errors.New("查询失败")
  170. }
  171. return
  172. }
  173. // ReportListSearch 报告列表页的搜索
  174. func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  175. indexName := utils.EsReportIndexName
  176. var must []map[string]interface{}
  177. shouldSub := []map[string]interface{}{
  178. map[string]interface{}{
  179. "match": map[string]interface{}{
  180. "Title": map[string]interface{}{
  181. "query": keyWord,
  182. "boost": 3,
  183. },
  184. },
  185. },
  186. map[string]interface{}{
  187. "match": map[string]interface{}{
  188. "StageStr": map[string]interface{}{
  189. "query": keyWord,
  190. "boost": 2,
  191. },
  192. },
  193. },
  194. map[string]interface{}{
  195. "match": map[string]interface{}{
  196. "Abstract": map[string]interface{}{
  197. "query": keyWord,
  198. "boost": 2,
  199. },
  200. },
  201. },
  202. // map[string]interface{}{
  203. // "match": map[string]interface{}{
  204. // "ClassifyNameSecond": map[string]interface{}{
  205. // "query": keyWord,
  206. // "boost": 2,
  207. // },
  208. // },
  209. // },
  210. map[string]interface{}{
  211. "match_phrase": map[string]interface{}{
  212. "Title": map[string]interface{}{
  213. "query": keyWord,
  214. //"slop": "50",
  215. "boost": 5,
  216. },
  217. },
  218. },
  219. map[string]interface{}{
  220. "match_phrase": map[string]interface{}{
  221. "Abstract": map[string]interface{}{
  222. "query": keyWord,
  223. //"slop": "50",
  224. "boost": 5,
  225. },
  226. },
  227. },
  228. }
  229. mustMap := map[string]interface{}{
  230. "bool": map[string]interface{}{
  231. "should": shouldSub,
  232. },
  233. }
  234. must = append(must, mustMap)
  235. filter := []map[string]interface{}{
  236. map[string]interface{}{
  237. "term": map[string]interface{}{
  238. "PublishState": 2,
  239. },
  240. },
  241. map[string]interface{}{
  242. "term": map[string]interface{}{
  243. "ReportChapterId": 0, //排除章节内容
  244. },
  245. },
  246. // map[string]interface{}{
  247. // "term": map[string]interface{}{
  248. // "ClassifyIdFirst": classifyIdFirst,
  249. // },
  250. // },
  251. // map[string]interface{}{
  252. // "terms": map[string]interface{}{
  253. // "ClassifyIdSecond": classifyIdSeconds,
  254. // },
  255. // },
  256. }
  257. source := map[string]interface{}{
  258. "query": map[string]interface{}{
  259. "bool": map[string]interface{}{
  260. "must": must,
  261. "filter": filter,
  262. },
  263. },
  264. }
  265. // source["from"] = (pageIndex - 1) * pageSize
  266. // source["size"] = pageSize
  267. // source["highlight"] = map[string]interface{}{
  268. // "fields": map[string]interface{}{
  269. // "Title": map[string]interface{}{},
  270. // "Abstract": map[string]interface{}{},
  271. // "StageStr": map[string]interface{}{},
  272. // "ClassifyNameSecond": map[string]interface{}{},
  273. // },
  274. // "pre_tags": "<span style=\"color:#E3B377\">",
  275. // "post_tags": "</span>",
  276. // }
  277. // source["sort"] = []map[string]interface{}{
  278. // map[string]interface{}{
  279. // "PublishTime.keyword": map[string]interface{}{
  280. // "order": "desc",
  281. // },
  282. // },
  283. // map[string]interface{}{
  284. // "_score": map[string]interface{}{
  285. // "order": "desc",
  286. // },
  287. // },
  288. // }
  289. jsonstr, err := json.Marshal(source)
  290. fmt.Printf("%s", jsonstr)
  291. request := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
  292. searchResp, err = request.Do(context.Background())
  293. if err != nil {
  294. fmt.Print("结果err:")
  295. fmt.Println(err.Error())
  296. return
  297. }
  298. fmt.Print("结果正常:")
  299. fmt.Println(searchResp.Status)
  300. if searchResp.Status != 0 {
  301. err = errors.New("查询失败")
  302. }
  303. total = searchResp.TotalHits()
  304. return
  305. }