Search.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace addons\blog\controller;
  3. use addons\blog\library\FulltextSearch;
  4. /**
  5. * 搜索控制器
  6. * Class Search
  7. * @package addons\blog\controller
  8. */
  9. class Search extends Base
  10. {
  11. public function index()
  12. {
  13. $config = get_addon_config('blog');
  14. if ($config['searchtype'] == 'xunsearch') {
  15. return $this->xunsearch();
  16. }
  17. $q = $this->request->get('q', $this->request->get('search', ''));
  18. $filterlist = [];
  19. $orderlist = [];
  20. $orderby = $this->request->get('orderby', '');
  21. $orderway = $this->request->get('orderway', '', 'strtolower');
  22. $params = ['q' => $q];
  23. if ($orderby) {
  24. $params['orderby'] = $orderby;
  25. }
  26. if ($orderway) {
  27. $params['orderway'] = $orderway;
  28. }
  29. $sortrank = [
  30. ['name' => 'default', 'field' => 'weigh', 'title' => __('Default')],
  31. ['name' => 'views', 'field' => 'views', 'title' => __('Views')],
  32. ['name' => 'id', 'field' => 'id', 'title' => __('Post date')],
  33. ];
  34. $orderby = $orderby && in_array($orderby, ['default', 'id', 'views']) ? $orderby : 'default';
  35. $orderway = $orderway ? $orderway : 'desc';
  36. foreach ($sortrank as $k => $v) {
  37. $url = '?' . http_build_query(array_merge($params, ['orderby' => $v['name'], 'orderway' => ($orderway == 'desc' ? 'asc' : 'desc')]));
  38. $v['active'] = $orderby == $v['name'] ? true : false;
  39. $v['orderby'] = $orderway;
  40. $v['url'] = $url;
  41. $orderlist[] = $v;
  42. }
  43. $orderby = $orderby == 'default' ? 'weigh' : $orderby;
  44. $postlist = \addons\blog\model\Post::where('status', 'normal')
  45. ->where('title', 'like', "%{$q}%")
  46. ->with(['category'])
  47. ->order($orderby, $orderway)
  48. ->paginate($this->view->config['listpagesize'], false, ['type' => '\\addons\\blog\\library\\Bootstrap']);
  49. $postlist->appends($params);
  50. $this->view->assign("orderlist", $orderlist);
  51. $this->view->assign("postlist", $postlist);
  52. $this->view->assign('title', __("Search for %s", $q));
  53. if ($this->request->isAjax()) {
  54. return $this->view->fetch('/common/postlist');
  55. }
  56. return $this->view->fetch('/search');
  57. }
  58. /**
  59. * Xunsearch搜索
  60. * @return string
  61. * @throws \think\Exception
  62. */
  63. public function xunsearch()
  64. {
  65. $orderList = [
  66. 'relevance' => '默认排序',
  67. 'createtime_desc' => '发布时间从新到旧',
  68. 'createtime_asc' => '发布时间从旧到新',
  69. 'views_desc' => '浏览次数从多到少',
  70. 'views_asc' => '浏览次数从少到多',
  71. 'comments_desc' => '评论次数从多到少',
  72. 'comments_asc' => '评论次数从少到多',
  73. ];
  74. $q = $this->request->get('q', $this->request->get('search', ''));
  75. $page = $this->request->get('page/d', '1', 'trim');
  76. $order = $this->request->get('order', '', 'trim');
  77. $fulltext = $this->request->get('fulltext/d', '1', 'trim');
  78. $fuzzy = $this->request->get('fuzzy/d', '0', 'trim');
  79. $synonyms = $this->request->get('synonyms/d', '0', 'trim');
  80. $total_begin = microtime(true);
  81. $search = null;
  82. $pagesize = 10;
  83. $result = FulltextSearch::search($q, $page, $pagesize, $order, $fulltext, $fuzzy, $synonyms);
  84. // 计算总耗时
  85. $total_cost = microtime(true) - $total_begin;
  86. //获取热门搜索
  87. $hot = FulltextSearch::hot();
  88. $data = [
  89. 'q' => $q,
  90. 'error' => '',
  91. 'total' => $result['total'],
  92. 'count' => $result['count'],
  93. 'search_cost' => $result['microseconds'],
  94. 'docs' => $result['list'],
  95. 'pager' => $result['pager'],
  96. 'corrected' => $result['corrected'],
  97. 'highlight' => $result['highlight'],
  98. 'related' => $result['related'],
  99. 'search' => $search,
  100. 'fulltext' => $fulltext,
  101. 'synonyms' => $synonyms,
  102. 'fuzzy' => $fuzzy,
  103. 'order' => $order,
  104. 'orderList' => $orderList,
  105. 'hot' => $hot,
  106. 'total_cost' => $total_cost,
  107. ];
  108. \think\Config::set('blog.title', __("Search for %s", $q));
  109. $this->view->assign("title", $q);
  110. $this->view->assign($data);
  111. return $this->view->fetch('/xunsearch');
  112. }
  113. public function suggestion()
  114. {
  115. $q = trim($this->request->get('q', ''));
  116. $terms = [];
  117. $config = get_addon_config('blog');
  118. if ($config['searchtype'] == 'xunsearch') {
  119. $terms = FulltextSearch::suggestion($q);
  120. }
  121. return json($terms);
  122. }
  123. }