diff --git a/app/Caches/HelpList.php b/app/Caches/HelpList.php index 2b4c8ce2..0d0b31e6 100644 --- a/app/Caches/HelpList.php +++ b/app/Caches/HelpList.php @@ -42,13 +42,13 @@ class HelpList extends Cache 'name' => $category->name, ]; - $item['list'] = []; + $item['helps'] = []; $helps = $this->findHelps($category->id); if ($helps->count() > 0) { foreach ($helps as $help) { - $item['list'][] = [ + $item['helps'][] = [ 'id' => $help->id, 'title' => $help->title, ]; diff --git a/app/Caches/IndexFreeCourseList.php b/app/Caches/IndexFreeCourseList.php index 0f4dcc47..b6c69d67 100644 --- a/app/Caches/IndexFreeCourseList.php +++ b/app/Caches/IndexFreeCourseList.php @@ -28,8 +28,6 @@ class IndexFreeCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexFreeCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexFreeCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Caches/IndexNewCourseList.php b/app/Caches/IndexNewCourseList.php index 76582509..7cb627cf 100644 --- a/app/Caches/IndexNewCourseList.php +++ b/app/Caches/IndexNewCourseList.php @@ -28,8 +28,6 @@ class IndexNewCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexNewCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexNewCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Caches/IndexVipCourseList.php b/app/Caches/IndexVipCourseList.php index e97d1879..ab869db0 100644 --- a/app/Caches/IndexVipCourseList.php +++ b/app/Caches/IndexVipCourseList.php @@ -28,8 +28,6 @@ class IndexVipCourseList extends Cache public function getContent($id = null) { - $result = []; - $categoryLimit = 5; $courseLimit = 10; @@ -40,13 +38,19 @@ class IndexVipCourseList extends Cache return []; } + $result = []; + foreach ($categories as $category) { - $categoryItem = [ + $item = []; + + $item['category'] = [ 'id' => $category->id, 'name' => $category->name, ]; + $item['courses'] = []; + $courses = $this->findCategoryCourses($category->id, $courseLimit); if ($courses->count() == 0) { @@ -69,9 +73,9 @@ class IndexVipCourseList extends Cache ]; } - $categoryItem['courses'] = $categoryCourses; + $item['courses'] = $categoryCourses; - $result[] = $categoryItem; + $result[] = $item; } return $result; diff --git a/app/Console/Tasks/GroupIndexTask.php b/app/Console/Tasks/GroupIndexTask.php new file mode 100644 index 00000000..2cd79419 --- /dev/null +++ b/app/Console/Tasks/GroupIndexTask.php @@ -0,0 +1,128 @@ +searchGroups($query); + + var_export($result); + } + + /** + * 清空索引 + * + * @command: php console.php group_index clean + */ + public function cleanAction() + { + $this->cleanGroupIndex(); + } + + /** + * 重建索引 + * + * @command: php console.php group_index rebuild + */ + public function rebuildAction() + { + $this->rebuildGroupIndex(); + } + + /** + * 清空索引 + */ + protected function cleanGroupIndex() + { + $handler = new GroupSearcher(); + + $index = $handler->getXS()->getIndex(); + + echo "start clean index" . PHP_EOL; + + $index->clean(); + + echo "end clean index" . PHP_EOL; + } + + /** + * 重建索引 + */ + protected function rebuildGroupIndex() + { + $groups = $this->findGroups(); + + if ($groups->count() == 0) { + return; + } + + $handler = new GroupSearcher(); + + $documenter = new GroupDocument(); + + $index = $handler->getXS()->getIndex(); + + echo "start rebuild index" . PHP_EOL; + + $index->beginRebuild(); + + foreach ($groups as $group) { + $document = $documenter->setDocument($group); + $index->add($document); + } + + $index->endRebuild(); + + echo "end rebuild index" . PHP_EOL; + } + + /** + * 搜索课程 + * + * @param string $query + * @return array + * @throws \XSException + */ + protected function searchGroups($query) + { + $handler = new GroupSearcher(); + + return $handler->search($query); + } + + /** + * 查找课程 + * + * @return ResultsetInterface|Resultset|GroupModel[] + */ + protected function findGroups() + { + return GroupModel::query() + ->where('published = 1') + ->execute(); + } + +} diff --git a/app/Console/Tasks/UserIndexTask.php b/app/Console/Tasks/UserIndexTask.php new file mode 100644 index 00000000..3ec4ce51 --- /dev/null +++ b/app/Console/Tasks/UserIndexTask.php @@ -0,0 +1,128 @@ +searchUsers($query); + + var_export($result); + } + + /** + * 清空索引 + * + * @command: php console.php user_index clean + */ + public function cleanAction() + { + $this->cleanUserIndex(); + } + + /** + * 重建索引 + * + * @command: php console.php user_index rebuild + */ + public function rebuildAction() + { + $this->rebuildUserIndex(); + } + + /** + * 清空索引 + */ + protected function cleanUserIndex() + { + $handler = new UserSearcher(); + + $index = $handler->getXS()->getIndex(); + + echo "start clean index" . PHP_EOL; + + $index->clean(); + + echo "end clean index" . PHP_EOL; + } + + /** + * 重建索引 + */ + protected function rebuildUserIndex() + { + $users = $this->findUsers(); + + if ($users->count() == 0) { + return; + } + + $handler = new UserSearcher(); + + $documenter = new UserDocument(); + + $index = $handler->getXS()->getIndex(); + + echo "start rebuild index" . PHP_EOL; + + $index->beginRebuild(); + + foreach ($users as $user) { + $document = $documenter->setDocument($user); + $index->add($document); + } + + $index->endRebuild(); + + echo "end rebuild index" . PHP_EOL; + } + + /** + * 搜索课程 + * + * @param string $query + * @return array + * @throws \XSException + */ + protected function searchUsers($query) + { + $handler = new UserSearcher(); + + return $handler->search($query); + } + + /** + * 查找课程 + * + * @return ResultsetInterface|Resultset|UserModel[] + */ + protected function findUsers() + { + return UserModel::query() + ->where('deleted = 0') + ->execute(); + } + +} diff --git a/app/Http/Web/Controllers/HelpController.php b/app/Http/Web/Controllers/HelpController.php index 8f833609..cc3e300f 100644 --- a/app/Http/Web/Controllers/HelpController.php +++ b/app/Http/Web/Controllers/HelpController.php @@ -18,11 +18,11 @@ class HelpController extends Controller { $service = new HelpListService(); - $helps = $service->handle(); + $items = $service->handle(); $this->seo->prependTitle('帮助'); - $this->view->setVar('helps', $helps); + $this->view->setVar('items', $items); } /** diff --git a/app/Http/Web/Controllers/SearchController.php b/app/Http/Web/Controllers/SearchController.php index 5900ae20..6891a2b4 100644 --- a/app/Http/Web/Controllers/SearchController.php +++ b/app/Http/Web/Controllers/SearchController.php @@ -2,9 +2,9 @@ namespace App\Http\Web\Controllers; -use App\Services\Frontend\Search\CourseHotQuery as CourseHotQueryService; -use App\Services\Frontend\Search\CourseList as CourseListService; -use App\Services\Frontend\Search\CourseRelatedQuery as CourseRelatedQueryService; +use App\Services\Frontend\Search\Course as CourseSearchService; +use App\Services\Frontend\Search\Group as GroupSearchService; +use App\Services\Frontend\Search\User as UserSearchService; use App\Traits\Response as ResponseTrait; /** @@ -20,7 +20,8 @@ class SearchController extends Controller */ public function indexAction() { - $query = $this->request->get('query', ['trim']); + $query = $this->request->get('query', ['trim', 'string']); + $type = $this->request->get('type', ['trim'], 'course'); if (empty($query)) { return $this->response->redirect(['for' => 'web.course.list']); @@ -28,21 +29,38 @@ class SearchController extends Controller $this->seo->prependTitle(['搜索', $query]); - $service = new CourseHotQueryService(); + $service = $this->getSearchService($type); - $hotQueries = $service->handle(); + $hotQueries = $service->hotQuery(); - $service = new CourseRelatedQueryService(); + $relatedQueries = $service->relatedQuery($query); - $relatedQueries = $service->handle($query); - - $service = new CourseListService(); - - $pager = $service->handle(); + $pager = $service->search(); $this->view->setVar('hot_queries', $hotQueries); $this->view->setVar('related_queries', $relatedQueries); $this->view->setVar('pager', $pager); } + /** + * @param string $type + * @return CourseSearchService|GroupSearchService|UserSearchService + */ + protected function getSearchService($type) + { + switch ($type) { + case 'group': + $service = new GroupSearchService; + break; + case 'user': + $service = new UserSearchService(); + break; + default: + $service = new CourseSearchService(); + break; + } + + return $service; + } + } diff --git a/app/Http/Web/Views/help/index.volt b/app/Http/Web/Views/help/index.volt index 97cae244..32bb00a8 100644 --- a/app/Http/Web/Views/help/index.volt +++ b/app/Http/Web/Views/help/index.volt @@ -13,12 +13,12 @@