FulltextSearch.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | cms全文搜索类
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\library;
  15. use addons\xunsearch\library\Xunsearch;
  16. use think\facade\Db;
  17. class FulltextSearch
  18. {
  19. //重置搜索索引数据库
  20. public static function reset()
  21. {
  22. $models = array_values(cms_cache('Model'));
  23. $category = Db::name('cms_category')->where('type', 2)->column('modelid', 'id');
  24. if (count($models) > 0) {
  25. foreach ($models as $k => $v) {
  26. if ($v['type'] == 1) {
  27. //独立表
  28. Db::name($v['tablename'])->where('status', 1)->chunk(100, function ($list) use ($category) {
  29. foreach ($list as $val) {
  30. self::update(($category[$val['catid']] ?? 0), $val['catid'], $val['id'], $val);
  31. }
  32. });
  33. } elseif ($v['type'] == 2) {
  34. Db::view($v['tablename'], '*')
  35. ->where('status', 1)->view($v['tablename'] . '_data', '*', $v['tablename'] . '.id=' . $v['tablename'] . '_data' . '.did', 'LEFT')
  36. ->chunk(100, function ($list) use ($category) {
  37. foreach ($list as $val) {
  38. self::update(($category[$val['catid']] ?? 0), $val['catid'], $val['id'], $val, $val);
  39. }
  40. });
  41. }
  42. }
  43. }
  44. return true;
  45. }
  46. /**
  47. * 新增/更新索引
  48. */
  49. public static function update($modelid, $catid, $id, $data, $dataExt)
  50. {
  51. $res = [];
  52. $res['pid'] = $catid . '_' . $id;
  53. $res['catid'] = $catid;
  54. $res['id'] = $id;
  55. $res['modelid'] = $modelid;
  56. $res['title'] = $data['title'] ?? '';
  57. $res['thumb'] = $data['thumb'] ?? '';
  58. $res['content'] = isset($dataExt['content']) ? strip_tags(htmlspecialchars_decode($dataExt['content'])) : '';
  59. $res['publish_time'] = $data['publish_time'] ?? 0;
  60. $res['url'] = buildContentUrl($catid, $id);
  61. Xunsearch::instance(self::getProjectName())->update($res);
  62. }
  63. /**
  64. * 删除
  65. */
  66. public static function del($catid, $id)
  67. {
  68. Xunsearch::instance(self::getProjectName())->del($catid . '_' . $id);
  69. }
  70. public static function setQuery($query, $fulltext = true, $fuzzy = false, $synonyms = false)
  71. {
  72. return Xunsearch::instance(self::getProjectName())->setQuery($query, $fulltext, $fuzzy, $synonyms);
  73. }
  74. /**
  75. * 获取搜索结果
  76. */
  77. public static function search($page = 1, $pagesize = 20, $order = '', $query = [])
  78. {
  79. return Xunsearch::instance(self::getProjectName())->search($page, $pagesize, $order, $query);
  80. }
  81. /**
  82. * 获取搜索热门关键字
  83. */
  84. public static function hot()
  85. {
  86. return Xunsearch::instance(self::getProjectName())->getXS()->search->getHotQuery();
  87. }
  88. // 获取项目名
  89. public static function getProjectName()
  90. {
  91. $config = get_addon_config('cms');
  92. return $config['xunsearch_project_name'] ?? 'cms';
  93. }
  94. }