FulltextSearch.php 5.0 KB

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