X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/dcde599709ba14637f9170f8a42fc83f9e39bce9..refs/pull/432/head:/app/Services/SearchService.php diff --git a/app/Services/SearchService.php b/app/Services/SearchService.php index ec4889e50..3d1d45c3b 100644 --- a/app/Services/SearchService.php +++ b/app/Services/SearchService.php @@ -50,6 +50,15 @@ class SearchService $this->permissionService = $permissionService; } + /** + * Set the database connection + * @param Connection $connection + */ + public function setConnection(Connection $connection) + { + $this->db = $connection; + } + /** * Search all entities in the system. * @param string $searchString @@ -97,10 +106,16 @@ class SearchService public function searchBook($bookId, $searchString) { $terms = $this->parseSearchString($searchString); + $entityTypes = ['page', 'chapter']; + $entityTypesToSearch = isset($terms['filters']['type']) ? explode('|', $terms['filters']['type']) : $entityTypes; + $results = collect(); - $pages = $this->buildEntitySearchQuery($terms, 'page')->where('book_id', '=', $bookId)->take(20)->get(); - $chapters = $this->buildEntitySearchQuery($terms, 'chapter')->where('book_id', '=', $bookId)->take(20)->get(); - return $results->merge($pages)->merge($chapters)->sortByDesc('score')->take(20); + foreach ($entityTypesToSearch as $entityType) { + if (!in_array($entityType, $entityTypes)) continue; + $search = $this->buildEntitySearchQuery($terms, $entityType)->where('book_id', '=', $bookId)->take(20)->get(); + $results = $results->merge($search); + } + return $results->sortByDesc('score')->take(20); } /** @@ -148,6 +163,7 @@ class SearchService // Handle normal search terms if (count($terms['search']) > 0) { $subQuery = $this->db->table('search_terms')->select('entity_id', 'entity_type', \DB::raw('SUM(score) as score')); + $subQuery->where('entity_type', '=', 'BookStack\\' . ucfirst($entityType)); $subQuery->where(function(Builder $query) use ($terms) { foreach ($terms['search'] as $inputTerm) { $query->orWhere('term', 'like', $inputTerm .'%');