]> BookStack Code Mirror - bookstack/blobdiff - app/Search/SearchController.php
Vectors: Got basic LLM querying working using vector search context
[bookstack] / app / Search / SearchController.php
index bc3f2ddb403e49bfd8b47e950c872fb4f6036598..a688385e7c37d43f90e1084e8415d37bffea80e6 100644 (file)
@@ -2,16 +2,18 @@
 
 namespace BookStack\Search;
 
-use BookStack\Entities\Models\Page;
-use BookStack\Entities\Queries\Popular;
+use BookStack\Entities\Queries\PageQueries;
+use BookStack\Entities\Queries\QueryPopular;
 use BookStack\Entities\Tools\SiblingFetcher;
 use BookStack\Http\Controller;
+use BookStack\Search\Vectors\VectorSearchRunner;
 use Illuminate\Http\Request;
 
 class SearchController extends Controller
 {
     public function __construct(
-        protected SearchRunner $searchRunner
+        protected SearchRunner $searchRunner,
+        protected PageQueries $pageQueries,
     ) {
     }
 
@@ -66,7 +68,7 @@ class SearchController extends Controller
      * Search for a list of entities and return a partial HTML response of matching entities.
      * Returns the most popular entities if no search is provided.
      */
-    public function searchForSelector(Request $request)
+    public function searchForSelector(Request $request, QueryPopular $queryPopular)
     {
         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
         $searchTerm = $request->get('term', false);
@@ -77,7 +79,7 @@ class SearchController extends Controller
             $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
         } else {
-            $entities = (new Popular())->run(20, 0, $entityTypes);
+            $entities = $queryPopular->run(20, 0, $entityTypes);
         }
 
         return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
@@ -95,12 +97,11 @@ class SearchController extends Controller
             $searchOptions->setFilter('is_template');
             $entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
         } else {
-            $entities = Page::visible()
-                ->where('template', '=', true)
+            $entities = $this->pageQueries->visibleTemplates()
                 ->where('draft', '=', false)
                 ->orderBy('updated_at', 'desc')
                 ->take(20)
-                ->get(Page::$listAttributes);
+                ->get();
         }
 
         return view('search.parts.entity-selector-list', [
@@ -139,4 +140,19 @@ class SearchController extends Controller
 
         return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);
     }
+
+    public function searchQuery(Request $request, VectorSearchRunner $runner)
+    {
+        $query = $request->get('query', '');
+
+        if ($query) {
+            $results = $runner->run($query);
+        } else {
+            $results = null;
+        }
+
+        return view('search.query', [
+            'results' => $results,
+        ]);
+    }
 }