3 namespace BookStack\Search;
5 use BookStack\Api\ApiEntityListFormatter;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Http\ApiController;
8 use Illuminate\Http\Request;
10 class SearchApiController extends ApiController
14 'query' => ['required'],
15 'page' => ['integer', 'min:1'],
16 'count' => ['integer', 'min:1', 'max:100'],
20 public function __construct(
21 protected SearchRunner $searchRunner,
22 protected SearchResultsFormatter $resultsFormatter
27 * Run a search query against all main content types (shelves, books, chapters & pages)
28 * in the system. Takes the same input as the main search bar within the BookStack
29 * interface as a 'query' parameter. See https://www.bookstackapp.com/docs/user/searching/
30 * for a full list of search term options. Results contain a 'type' property to distinguish
31 * between: bookshelf, book, chapter & page.
33 * The paging parameters and response format emulates a standard listing endpoint
34 * but standard sorting and filtering cannot be done on this endpoint. If a count value
35 * is provided this will only be taken as a suggestion. The results in the response
36 * may currently be up to 4x this value.
38 public function all(Request $request)
40 $this->validate($request, $this->rules['all']);
42 $options = SearchOptions::fromString($request->get('query') ?? '');
43 $page = intval($request->get('page', '0')) ?: 1;
44 $count = min(intval($request->get('count', '0')) ?: 20, 100);
46 $results = $this->searchRunner->searchEntities($options, 'all', $page, $count);
47 $this->resultsFormatter->format($results['results']->all(), $options);
49 $data = (new ApiEntityListFormatter($results['results']->all()))
50 ->withType()->withTags()->withParents()
51 ->withField('preview_html', function (Entity $entity) {
53 'name' => (string) $entity->getAttribute('preview_name'),
54 'content' => (string) $entity->getAttribute('preview_content'),
58 return response()->json([
60 'total' => $results['total'],