控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FulltextSearch.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | cms全文搜索类
  13. // +----------------------------------------------------------------------
  14. namespace addons\cms\library;
  15. use addons\xunsearch\library\Xunsearch;
  16. use think\Db;
  17. class FulltextSearch
  18. {
  19. //重置搜索索引数据库
  20. public static function reset()
  21. {
  22. $models = array_values(cache('Model'));
  23. $category = Db::name('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'] = isset($data['title']) ? $data['title'] : '';
  57. $res['content'] = isset($dataExt['content']) ? strip_tags(htmlspecialchars_decode($dataExt['content'])) : '';
  58. $res['create_time'] = isset($data['create_time']) ? $data['create_time'] : 0;
  59. $res['url'] = buildContentUrl($catid, $id);
  60. Xunsearch::instance('cms')->update($res);
  61. }
  62. /**
  63. * 删除
  64. */
  65. public static function del($catid, $id)
  66. {
  67. Xunsearch::instance('cms')->del($catid . '_' . $id);
  68. }
  69. public static function setQuery($query, $fulltext = true, $fuzzy = false, $synonyms = false)
  70. {
  71. return Xunsearch::instance('cms')->setQuery($query, $fulltext, $fuzzy, $synonyms);
  72. }
  73. /**
  74. * 获取搜索结果
  75. */
  76. public static function search($page = 1, $pagesize = 20, $order = '', $query = [])
  77. {
  78. return Xunsearch::instance('cms')->search($page, $pagesize, $order, $query);
  79. }
  80. /**
  81. * 获取搜索热门关键字
  82. */
  83. public static function hot()
  84. {
  85. return Xunsearch::instance('cms')->getXS()->search->getHotQuery();
  86. }
  87. }