]> BookStack Code Mirror - bookstack/blob - app/Search/SearchController.php
Vectors: Got basic LLM querying working using vector search context
[bookstack] / app / Search / SearchController.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Entities\Queries\QueryPopular;
7 use BookStack\Entities\Tools\SiblingFetcher;
8 use BookStack\Http\Controller;
9 use BookStack\Search\Vectors\VectorSearchRunner;
10 use Illuminate\Http\Request;
11
12 class SearchController extends Controller
13 {
14     public function __construct(
15         protected SearchRunner $searchRunner,
16         protected PageQueries $pageQueries,
17     ) {
18     }
19
20     /**
21      * Searches all entities.
22      */
23     public function search(Request $request, SearchResultsFormatter $formatter)
24     {
25         $searchOpts = SearchOptions::fromRequest($request);
26         $fullSearchString = $searchOpts->toString();
27         $this->setPageTitle(trans('entities.search_for_term', ['term' => $fullSearchString]));
28
29         $page = intval($request->get('page', '0')) ?: 1;
30         $nextPageLink = url('/search?term=' . urlencode($fullSearchString) . '&page=' . ($page + 1));
31
32         $results = $this->searchRunner->searchEntities($searchOpts, 'all', $page, 20);
33         $formatter->format($results['results']->all(), $searchOpts);
34
35         return view('search.all', [
36             'entities'     => $results['results'],
37             'totalResults' => $results['total'],
38             'searchTerm'   => $fullSearchString,
39             'hasNextPage'  => $results['has_more'],
40             'nextPageLink' => $nextPageLink,
41             'options'      => $searchOpts,
42         ]);
43     }
44
45     /**
46      * Searches all entities within a book.
47      */
48     public function searchBook(Request $request, int $bookId)
49     {
50         $term = $request->get('term', '');
51         $results = $this->searchRunner->searchBook($bookId, $term);
52
53         return view('entities.list', ['entities' => $results]);
54     }
55
56     /**
57      * Searches all entities within a chapter.
58      */
59     public function searchChapter(Request $request, int $chapterId)
60     {
61         $term = $request->get('term', '');
62         $results = $this->searchRunner->searchChapter($chapterId, $term);
63
64         return view('entities.list', ['entities' => $results]);
65     }
66
67     /**
68      * Search for a list of entities and return a partial HTML response of matching entities.
69      * Returns the most popular entities if no search is provided.
70      */
71     public function searchForSelector(Request $request, QueryPopular $queryPopular)
72     {
73         $entityTypes = $request->filled('types') ? explode(',', $request->get('types')) : ['page', 'chapter', 'book'];
74         $searchTerm = $request->get('term', false);
75         $permission = $request->get('permission', 'view');
76
77         // Search for entities otherwise show most popular
78         if ($searchTerm !== false) {
79             $searchTerm .= ' {type:' . implode('|', $entityTypes) . '}';
80             $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 20)['results'];
81         } else {
82             $entities = $queryPopular->run(20, 0, $entityTypes);
83         }
84
85         return view('search.parts.entity-selector-list', ['entities' => $entities, 'permission' => $permission]);
86     }
87
88     /**
89      * Search for a list of templates to choose from.
90      */
91     public function templatesForSelector(Request $request)
92     {
93         $searchTerm = $request->get('term', false);
94
95         if ($searchTerm !== false) {
96             $searchOptions = SearchOptions::fromString($searchTerm);
97             $searchOptions->setFilter('is_template');
98             $entities = $this->searchRunner->searchEntities($searchOptions, 'page', 1, 20)['results'];
99         } else {
100             $entities = $this->pageQueries->visibleTemplates()
101                 ->where('draft', '=', false)
102                 ->orderBy('updated_at', 'desc')
103                 ->take(20)
104                 ->get();
105         }
106
107         return view('search.parts.entity-selector-list', [
108             'entities' => $entities,
109             'permission' => 'view'
110         ]);
111     }
112
113     /**
114      * Search for a list of entities and return a partial HTML response of matching entities
115      * to be used as a result preview suggestion list for global system searches.
116      */
117     public function searchSuggestions(Request $request)
118     {
119         $searchTerm = $request->get('term', '');
120         $entities = $this->searchRunner->searchEntities(SearchOptions::fromString($searchTerm), 'all', 1, 5)['results'];
121
122         foreach ($entities as $entity) {
123             $entity->setAttribute('preview_content', '');
124         }
125
126         return view('search.parts.entity-suggestion-list', [
127             'entities' => $entities->slice(0, 5)
128         ]);
129     }
130
131     /**
132      * Search siblings items in the system.
133      */
134     public function searchSiblings(Request $request, SiblingFetcher $siblingFetcher)
135     {
136         $type = $request->get('entity_type', null);
137         $id = $request->get('entity_id', null);
138
139         $entities = $siblingFetcher->fetch($type, $id);
140
141         return view('entities.list-basic', ['entities' => $entities, 'style' => 'compact']);
142     }
143
144     public function searchQuery(Request $request, VectorSearchRunner $runner)
145     {
146         $query = $request->get('query', '');
147
148         if ($query) {
149             $results = $runner->run($query);
150         } else {
151             $results = null;
152         }
153
154         return view('search.query', [
155             'results' => $results,
156         ]);
157     }
158 }