FulltextSearch.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace addons\blog\library;
  3. use addons\xunsearch\library\Xunsearch;
  4. use think\Config;
  5. use think\Exception;
  6. use think\View;
  7. class FulltextSearch
  8. {
  9. public static function config()
  10. {
  11. $data = [
  12. [
  13. 'name' => 'blog',
  14. 'title' => '简洁博客系统',
  15. 'fields' => [
  16. ['name' => 'pid', 'type' => 'id', 'title' => '主键'],
  17. ['name' => 'id', 'type' => 'numeric', 'title' => 'ID'],
  18. ['name' => 'title', 'type' => 'title', 'title' => '标题'],
  19. ['name' => 'content', 'type' => 'body', 'title' => '内容',],
  20. ['name' => 'type', 'type' => 'string', 'title' => '类型', 'index' => 'self'],
  21. ['name' => 'url', 'type' => 'string', 'title' => '链接',],
  22. ['name' => 'createtime', 'type' => 'date', 'title' => '发布日期',],
  23. ['name' => 'views', 'type' => 'numeric', 'title' => '浏览次数',],
  24. ['name' => 'comments', 'type' => 'numeric', 'title' => '评论次数',],
  25. ]
  26. ]
  27. ];
  28. return $data;
  29. }
  30. /**
  31. * 重置搜索索引数据库
  32. */
  33. public static function reset()
  34. {
  35. \addons\blog\model\Post::where('status', 'normal')->chunk(100, function ($list) {
  36. foreach ($list as $item) {
  37. self::add($item);
  38. }
  39. });
  40. return true;
  41. }
  42. /**
  43. * 添加索引
  44. * @param $row
  45. */
  46. public static function add($row)
  47. {
  48. self::update($row, true);
  49. }
  50. /**
  51. * 更新索引
  52. * @param $row
  53. * @param bool $add
  54. */
  55. public static function update($row, $add = false)
  56. {
  57. if (is_numeric($row)) {
  58. $row = \addons\blog\model\Post::get($row);
  59. if (!$row) {
  60. return;
  61. }
  62. }
  63. if (isset($row['status']) && $row['status'] != 'normal') {
  64. self::del($row);
  65. return;
  66. }
  67. $data = [];
  68. if ($row instanceof \addons\blog\model\Post || $row instanceof \app\admin\model\BlogPost) {
  69. $data['id'] = isset($row['id']) ? $row['id'] : 0;
  70. $data['title'] = isset($row['title']) ? $row['title'] : '';
  71. $data['content'] = isset($row['content']) ? strip_tags($row['content']) : '';
  72. $data['type'] = isset($row['type']) ? $row['type'] : '';
  73. $data['url'] = isset($row['fullurl']) ? $row['fullurl'] : addon_url('blog/post/index', [':id' => $data['id']], true, true);
  74. $data['createtime'] = isset($row['createtime']) ? $row['createtime'] : '';
  75. $data['views'] = isset($row['views']) ? $row['views'] : '0';
  76. $data['comments'] = isset($row['comments']) ? $row['comments'] : '0';
  77. $data['pid'] = "p" . $data['id'];
  78. }
  79. if ($data) {
  80. Xunsearch::instance('blog')->update($data, $add);
  81. }
  82. }
  83. /**
  84. * 删除
  85. * @param $row
  86. */
  87. public static function del($row)
  88. {
  89. $pid = "p" . (is_numeric($row) ? $row : ($row && isset($row['id']) ? $row['id'] : 0));
  90. if ($pid) {
  91. Xunsearch::instance('blog')->del($pid);
  92. }
  93. }
  94. /**
  95. * 获取搜索结果
  96. * @return array
  97. */
  98. public static function search($q, $page = 1, $pagesize = 20, $order = '', $fulltext = true, $fuzzy = false, $synonyms = false)
  99. {
  100. return Xunsearch::instance('blog')->search($q, $page, $pagesize, $order, $fulltext, $fuzzy, $synonyms);
  101. }
  102. /**
  103. * 获取建议搜索关键字
  104. * @param string $q 关键字
  105. * @param int $limit 返回条数
  106. */
  107. public static function suggestion($q, $limit = 10)
  108. {
  109. return Xunsearch::instance('blog')->suggestion($q, $limit);
  110. }
  111. /**
  112. * 获取搜索热门关键字
  113. * @return array
  114. * @throws \XSException
  115. */
  116. public static function hot()
  117. {
  118. return Xunsearch::instance('blog')->getXS()->search->getHotQuery();
  119. }
  120. }