Skip to content

Add book and chapter titles to search API results #5280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions app/Api/ApiEntityListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BookStack\Api;

use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;

class ApiEntityListFormatter
{
Expand All @@ -20,8 +21,16 @@ class ApiEntityListFormatter
* @var array<string|int, string|callable>
*/
protected array $fields = [
'id', 'name', 'slug', 'book_id', 'chapter_id', 'draft',
'template', 'priority', 'created_at', 'updated_at',
'id',
'name',
'slug',
'book_id',
'chapter_id',
'draft',
'template',
'priority',
'created_at',
'updated_at',
];

public function __construct(array $list)
Expand Down Expand Up @@ -62,12 +71,36 @@ public function withTags(): self
return $this;
}

/**
* Enable the inclusion of related book and chapter titles in the response.
*/
public function withRelatedData(): self
{
$this->withField('book', function (Entity $entity) {
if (method_exists($entity, 'book')) {
return $entity->book()->select(['id', 'name', 'slug'])->first();
}
return null;
});

$this->withField('chapter', function (Entity $entity) {
if ($entity instanceof Page && $entity->chapter_id) {
return $entity->chapter()->select(['id', 'name', 'slug'])->first();
}
return null;
});

return $this;
}

/**
* Format the data and return an array of formatted content.
* @return array[]
*/
public function format(): array
{
$this->loadRelatedData();

$results = [];

foreach ($this->list as $item) {
Expand All @@ -77,6 +110,23 @@ public function format(): array
return $results;
}

/**
* Eager load the related book and chapter data when needed.
*/
protected function loadRelatedData(): void
{
$pages = collect($this->list)->filter(fn($item) => $item instanceof Page);

foreach ($this->list as $entity) {
if (method_exists($entity, 'book')) {
$entity->load('book');
}
if ($entity instanceof Page && $entity->chapter_id) {
$entity->load('chapter');
}
}
}

/**
* Format a single entity item to a plain array.
*/
Expand Down
12 changes: 6 additions & 6 deletions app/Search/SearchApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class SearchApiController extends ApiController

protected $rules = [
'all' => [
'query' => ['required'],
'page' => ['integer', 'min:1'],
'count' => ['integer', 'min:1', 'max:100'],
'query' => ['required'],
'page' => ['integer', 'min:1'],
'count' => ['integer', 'min:1', 'max:100'],
],
];

Expand Down Expand Up @@ -50,16 +50,16 @@ public function all(Request $request)
$this->resultsFormatter->format($results['results']->all(), $options);

$data = (new ApiEntityListFormatter($results['results']->all()))
->withType()->withTags()
->withType()->withTags()->withRelatedData()
->withField('preview_html', function (Entity $entity) {
return [
'name' => (string) $entity->getAttribute('preview_name'),
'name' => (string) $entity->getAttribute('preview_name'),
'content' => (string) $entity->getAttribute('preview_content'),
];
})->format();

return response()->json([
'data' => $data,
'data' => $data,
'total' => $results['total'],
]);
}
Expand Down
2 changes: 1 addition & 1 deletion dev/api/requests/search-all.http
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GET /api/search?query=cats+{created_by:me}&page=1&count=2
GET /api/search?query=cats+{created_by:me}&page=1&count=2
149 changes: 87 additions & 62 deletions dev/api/responses/search-all.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,92 @@
{
"data": [
{
"id": 84,
"book_id": 1,
"slug": "a-chapter-for-cats",
"name": "A chapter for cats",
"created_at": "2021-11-14T15:57:35.000000Z",
"updated_at": "2021-11-14T15:57:35.000000Z",
"type": "chapter",
"url": "https://example.com/books/my-book/chapter/a-chapter-for-cats",
"preview_html": {
"name": "A chapter for <strong>cats</strong>",
"content": "...once a bunch of <strong>cats</strong> named tony...behaviour of <strong>cats</strong> is unsuitable"
},
"tags": []
},
{
"name": "The hows and whys of cats",
"id": 396,
"slug": "the-hows-and-whys-of-cats",
"book_id": 1,
"chapter_id": 75,
"draft": false,
"template": false,
"created_at": "2021-05-15T16:28:10.000000Z",
"updated_at": "2021-11-14T15:56:49.000000Z",
"type": "page",
"url": "https://example.com/books/my-book/page/the-hows-and-whys-of-cats",
"preview_html": {
"name": "The hows and whys of <strong>cats</strong>",
"content": "...people ask why <strong>cats</strong>? but there are...the reason that <strong>cats</strong> are fast are due to..."
},
"tags": [
"data": [
{
"name": "Animal",
"value": "Cat",
"order": 0
"id": 84,
"book_id": 1,
"slug": "a-chapter-for-cats",
"name": "A chapter for cats",
"created_at": "2021-11-14T15:57:35.000000Z",
"updated_at": "2021-11-14T15:57:35.000000Z",
"type": "chapter",
"url": "https://example.com/books/my-book/chapter/a-chapter-for-cats",
"book": {
"id": 1,
"name": "Cats",
"slug": "cats"
},
"preview_html": {
"name": "A chapter for <strong>cats</strong>",
"content": "...once a bunch of <strong>cats</strong> named tony...behaviour of <strong>cats</strong> is unsuitable"
},
"tags": []
},
{
"name": "Category",
"value": "Top Content",
"order": 0
"name": "The hows and whys of cats",
"id": 396,
"slug": "the-hows-and-whys-of-cats",
"book_id": 1,
"chapter_id": 75,
"draft": false,
"template": false,
"created_at": "2021-05-15T16:28:10.000000Z",
"updated_at": "2021-11-14T15:56:49.000000Z",
"type": "page",
"url": "https://example.com/books/my-book/page/the-hows-and-whys-of-cats",
"book": {
"id": 1,
"name": "Cats",
"slug": "cats"
},
"chapter": {
"id": 84,
"name": "A chapter for cats",
"slug": "a-chapter-for-cats"
},
"preview_html": {
"name": "The hows and whys of <strong>cats</strong>",
"content": "...people ask why <strong>cats</strong>? but there are...the reason that <strong>cats</strong> are fast are due to..."
},
"tags": [
{
"name": "Animal",
"value": "Cat",
"order": 0
},
{
"name": "Category",
"value": "Top Content",
"order": 0
}
]
},
{
"name": "How advanced are cats?",
"id": 362,
"slug": "how-advanced-are-cats",
"book_id": 13,
"chapter_id": 73,
"draft": false,
"template": false,
"created_at": "2020-11-29T21:55:07.000000Z",
"updated_at": "2021-11-14T16:02:39.000000Z",
"type": "page",
"url": "https://example.com/books/my-book/page/how-advanced-are-cats",
"book": {
"id": 1,
"name": "Cats",
"slug": "cats"
},
"chapter": {
"id": 84,
"name": "A chapter for cats",
"slug": "a-chapter-for-cats"
},
"preview_html": {
"name": "How advanced are <strong>cats</strong>?",
"content": "<strong>cats</strong> are some of the most advanced animals in the world."
},
"tags": []
}
]
},
{
"name": "How advanced are cats?",
"id": 362,
"slug": "how-advanced-are-cats",
"book_id": 13,
"chapter_id": 73,
"draft": false,
"template": false,
"created_at": "2020-11-29T21:55:07.000000Z",
"updated_at": "2021-11-14T16:02:39.000000Z",
"type": "page",
"url": "https://example.com/books/my-book/page/how-advanced-are-cats",
"preview_html": {
"name": "How advanced are <strong>cats</strong>?",
"content": "<strong>cats</strong> are some of the most advanced animals in the world."
},
"tags": []
}
],
"total": 3
}
],
"total": 3
}
8 changes: 4 additions & 4 deletions tests/Api/SearchApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function test_all_endpoint_returns_entity_url()
$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
$resp->assertJsonFragment([
'type' => 'page',
'url' => $page->getUrl(),
'url' => $page->getUrl(),
]);
}

Expand All @@ -57,10 +57,10 @@ public function test_all_endpoint_returns_items_with_preview_html()

$resp = $this->actingAsApiAdmin()->getJson($this->baseEndpoint . '?query=superuniquevalue');
$resp->assertJsonFragment([
'type' => 'book',
'url' => $book->getUrl(),
'type' => 'book',
'url' => $book->getUrl(),
'preview_html' => [
'name' => 'name with <strong>superuniquevalue</strong> within',
'name' => 'name with <strong>superuniquevalue</strong> within',
'content' => 'Description with <strong>superuniquevalue</strong> within',
],
]);
Expand Down