FulltextSearch.php 4.9 KB

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