FulltextSearch.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace addons\cms\library;
  3. use addons\cms\model\Modelx;
  4. use addons\xunsearch\library\Xunsearch;
  5. use think\Config;
  6. use think\Exception;
  7. use think\Log;
  8. use think\View;
  9. class FulltextSearch
  10. {
  11. public static function config()
  12. {
  13. $data = [
  14. [
  15. 'name' => 'cms',
  16. 'title' => 'CMS内容管理系统',
  17. 'fields' => [
  18. ['name' => 'pid', 'type' => 'id', 'title' => '主键'],
  19. ['name' => 'id', 'type' => 'numeric', 'title' => 'ID'],
  20. ['name' => 'title', 'type' => 'title', 'title' => '标题'],
  21. ['name' => 'content', 'type' => 'body', 'title' => '内容',],
  22. ['name' => 'type', 'type' => 'string', 'title' => '类型', 'index' => 'self'],
  23. ['name' => 'category_id', 'type' => 'numeric', 'title' => '分类ID', 'index' => 'self',],
  24. ['name' => 'user_id', 'type' => 'numeric', 'title' => '会员ID', 'index' => 'self',],
  25. ['name' => 'url', 'type' => 'string', 'title' => '链接',],
  26. ['name' => 'views', 'type' => 'numeric', 'title' => '浏览次数',],
  27. ['name' => 'comments', 'type' => 'numeric', 'title' => '评论次数',],
  28. ['name' => 'createtime', 'type' => 'date', 'title' => '发布时间',],
  29. ]
  30. ]
  31. ];
  32. return $data;
  33. }
  34. /**
  35. * 重置搜索索引数据库
  36. */
  37. public static function reset()
  38. {
  39. \addons\cms\model\Archives::where('status', 'normal')->chunk(100, function ($list) {
  40. foreach ($list as $item) {
  41. self::add($item);
  42. }
  43. });
  44. return true;
  45. }
  46. /**
  47. * 添加索引
  48. * @param $row
  49. */
  50. public static function add($row)
  51. {
  52. self::update($row, true);
  53. }
  54. /**
  55. * 更新索引
  56. * @param $row
  57. * @param bool $add
  58. */
  59. public static function update($row, $add = false)
  60. {
  61. $info = get_addon_info('xunsearch');
  62. if (!$info || !$info['state']) {
  63. return;
  64. }
  65. if (is_numeric($row)) {
  66. $row = \addons\cms\model\Archives::get($row);
  67. if (!$row) {
  68. return;
  69. }
  70. }
  71. if (isset($row['status']) && $row['status'] != 'normal') {
  72. self::del($row);
  73. return;
  74. }
  75. $data = [];
  76. if ($row instanceof \addons\cms\model\Archives || $row instanceof \app\admin\model\cms\Archives) {
  77. $content = '';
  78. $model = Modelx::get($row['model_id']);
  79. if ($model) {
  80. $content = \think\Db::name($model['table'])->where('id', $row['id'])->value("content");
  81. $content = $content ? strip_tags($content) : '';
  82. }
  83. $data['id'] = isset($row['id']) ? $row['id'] : 0;
  84. $data['title'] = isset($row['title']) ? $row['title'] : '';
  85. $data['category_id'] = isset($row['category_id']) ? $row['category_id'] : 0;
  86. $data['user_id'] = isset($row['user_id']) ? $row['user_id'] : 0;
  87. $data['content'] = $content;
  88. $data['comments'] = isset($row['comments']) ? $row['comments'] : 0;
  89. $data['createtime'] = isset($row['createtime']) ? $row['createtime'] : 0;
  90. $data['views'] = isset($row['views']) ? $row['views'] : 0;
  91. $data['type'] = 'archives';
  92. $data['url'] = $row->fullurl;
  93. }
  94. if ($data) {
  95. $data['pid'] = substr($data['type'], 0, 1) . $data['id'];
  96. try {
  97. Xunsearch::instance('cms')->update($data, $add);
  98. } catch (\Exception $e) {
  99. Log::record($e->getMessage());
  100. }
  101. }
  102. }
  103. /**
  104. * 删除
  105. * @param $row
  106. */
  107. public static function del($row)
  108. {
  109. $info = get_addon_info('xunsearch');
  110. if (!$info || !$info['state']) {
  111. return;
  112. }
  113. $pid = "a" . (is_numeric($row) ? $row : ($row && isset($row['id']) ? $row['id'] : 0));
  114. if ($pid) {
  115. try {
  116. Xunsearch::instance('cms')->del($pid);
  117. } catch (\Exception $e) {
  118. Log::record($e->getMessage());
  119. }
  120. }
  121. }
  122. /**
  123. * 获取搜索结果
  124. * @return array
  125. */
  126. public static function search($q, $page = 1, $pagesize = 20, $order = '', $fulltext = true, $fuzzy = false, $synonyms = false)
  127. {
  128. $info = get_addon_info('xunsearch');
  129. if (!$info || !$info['state']) {
  130. return [];
  131. }
  132. return Xunsearch::instance('cms')->search($q, $page, $pagesize, $order, $fulltext, $fuzzy, $synonyms);
  133. }
  134. /**
  135. * 获取建议搜索关键字
  136. * @param string $q 关键字
  137. * @param int $limit 返回条数
  138. * @return array
  139. */
  140. public static function suggestion($q, $limit = 10)
  141. {
  142. $info = get_addon_info('xunsearch');
  143. if (!$info || !$info['state']) {
  144. return [];
  145. }
  146. return Xunsearch::instance('cms')->suggestion($q, $limit);
  147. }
  148. /**
  149. * 获取搜索热门关键字
  150. * @return array
  151. * @throws \XSException
  152. */
  153. public static function hot()
  154. {
  155. $info = get_addon_info('xunsearch');
  156. if (!$info || !$info['state']) {
  157. return [];
  158. }
  159. return Xunsearch::instance('cms')->getXS()->search->getHotQuery();
  160. }
  161. }