]> BookStack Code Mirror - bookstack/blobdiff - app/Services/SearchService.php
Polish translation
[bookstack] / app / Services / SearchService.php
index ec4889e50b1aee47345ad51cd94274c93ca1b320..3d1d45c3b77d47ab356285f0f45ffa9c58b90115 100644 (file)
@@ -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 .'%');