report.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. query = query.Must(elastic.NewMatchPhraseQuery("Title", keyWord)).Boost(5)
  137. query = query.Should(elastic.NewMatchPhraseQuery("State", 1))
  138. query = query.Should(elastic.NewMatchPhraseQuery("PublishState", 2))
  139. sort := elastic.NewFieldSort("PublishTime.keyword").Desc()
  140. searchResp, err = utils.EsClient.Search().
  141. Index(indices...).
  142. Query(query).
  143. SortBy(sort).
  144. Highlight(elastic.NewHighlight().
  145. PreTags("<span class=\"report-title_color\">").
  146. PostTags("</span>").
  147. Field("Title")).
  148. From((pageIndex - 1) * pageSize).
  149. Size(pageSize).
  150. Pretty(true).
  151. Do(context.Background())
  152. // searchResp, err = request.Do(context.Background())
  153. if err != nil {
  154. fmt.Print("结果err:")
  155. fmt.Println(err.Error())
  156. return
  157. }
  158. total = searchResp.TotalHits()
  159. if searchResp.Status != 0 {
  160. err = errors.New("查询失败")
  161. }
  162. return
  163. }
  164. // ReportListSearch 报告列表页的搜索
  165. func ReportListSearch(keyWord string, classifyIdFirst int, classifyIdSeconds []int, pageIndex, pageSize int) (searchResp *elastic.SearchResult, total int64, err error) {
  166. indexName := utils.EsReportIndexName
  167. var must []map[string]interface{}
  168. shouldSub := []map[string]interface{}{
  169. map[string]interface{}{
  170. "match": map[string]interface{}{
  171. "Title": map[string]interface{}{
  172. "query": keyWord,
  173. "boost": 3,
  174. },
  175. },
  176. },
  177. map[string]interface{}{
  178. "match": map[string]interface{}{
  179. "StageStr": map[string]interface{}{
  180. "query": keyWord,
  181. "boost": 2,
  182. },
  183. },
  184. },
  185. map[string]interface{}{
  186. "match": map[string]interface{}{
  187. "Abstract": map[string]interface{}{
  188. "query": keyWord,
  189. "boost": 2,
  190. },
  191. },
  192. },
  193. // map[string]interface{}{
  194. // "match": map[string]interface{}{
  195. // "ClassifyNameSecond": map[string]interface{}{
  196. // "query": keyWord,
  197. // "boost": 2,
  198. // },
  199. // },
  200. // },
  201. map[string]interface{}{
  202. "match_phrase": map[string]interface{}{
  203. "Title": map[string]interface{}{
  204. "query": keyWord,
  205. //"slop": "50",
  206. "boost": 5,
  207. },
  208. },
  209. },
  210. map[string]interface{}{
  211. "match_phrase": map[string]interface{}{
  212. "Abstract": map[string]interface{}{
  213. "query": keyWord,
  214. //"slop": "50",
  215. "boost": 5,
  216. },
  217. },
  218. },
  219. }
  220. mustMap := map[string]interface{}{
  221. "bool": map[string]interface{}{
  222. "should": shouldSub,
  223. },
  224. }
  225. must = append(must, mustMap)
  226. filter := []map[string]interface{}{
  227. map[string]interface{}{
  228. "term": map[string]interface{}{
  229. "PublishState": 2,
  230. },
  231. },
  232. map[string]interface{}{
  233. "term": map[string]interface{}{
  234. "ReportChapterId": 0, //排除章节内容
  235. },
  236. },
  237. // map[string]interface{}{
  238. // "term": map[string]interface{}{
  239. // "ClassifyIdFirst": classifyIdFirst,
  240. // },
  241. // },
  242. // map[string]interface{}{
  243. // "terms": map[string]interface{}{
  244. // "ClassifyIdSecond": classifyIdSeconds,
  245. // },
  246. // },
  247. }
  248. source := map[string]interface{}{
  249. "query": map[string]interface{}{
  250. "bool": map[string]interface{}{
  251. "must": must,
  252. "filter": filter,
  253. },
  254. },
  255. }
  256. // source["from"] = (pageIndex - 1) * pageSize
  257. // source["size"] = pageSize
  258. // source["highlight"] = map[string]interface{}{
  259. // "fields": map[string]interface{}{
  260. // "Title": map[string]interface{}{},
  261. // "Abstract": map[string]interface{}{},
  262. // "StageStr": map[string]interface{}{},
  263. // "ClassifyNameSecond": map[string]interface{}{},
  264. // },
  265. // "pre_tags": "<span style=\"color:#E3B377\">",
  266. // "post_tags": "</span>",
  267. // }
  268. // source["sort"] = []map[string]interface{}{
  269. // map[string]interface{}{
  270. // "PublishTime.keyword": map[string]interface{}{
  271. // "order": "desc",
  272. // },
  273. // },
  274. // map[string]interface{}{
  275. // "_score": map[string]interface{}{
  276. // "order": "desc",
  277. // },
  278. // },
  279. // }
  280. jsonstr, err := json.Marshal(source)
  281. fmt.Printf("%s", jsonstr)
  282. request := utils.EsClient.Search(indexName).Source(source) // sets the JSON request
  283. searchResp, err = request.Do(context.Background())
  284. if err != nil {
  285. fmt.Print("结果err:")
  286. fmt.Println(err.Error())
  287. return
  288. }
  289. fmt.Print("结果正常:")
  290. fmt.Println(searchResp.Status)
  291. if searchResp.Status != 0 {
  292. err = errors.New("查询失败")
  293. }
  294. total = searchResp.TotalHits()
  295. return
  296. }