1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-11-04 13:31:45 +03:00

Search: Improved result hydration performance

This commit is contained in:
Dan Brown
2025-10-27 18:02:54 +00:00
parent 0b26573314
commit f0303de2e5
2 changed files with 8 additions and 12 deletions

View File

@@ -37,7 +37,7 @@ class EntityHydrator
$hydrated = []; $hydrated = [];
foreach ($this->entities as $entity) { foreach ($this->entities as $entity) {
$data = $entity->toArray(); $data = $entity->getRawOriginal();
$instance = Entity::instanceFromType($entity->type); $instance = Entity::instanceFromType($entity->type);
if ($instance instanceof Page) { if ($instance instanceof Page) {
@@ -45,7 +45,7 @@ class EntityHydrator
unset($data['description']); unset($data['description']);
} }
$instance->forceFill($data); $instance = $instance->setRawAttributes($data, true);
$hydrated[] = $instance; $hydrated[] = $instance;
} }
@@ -131,7 +131,8 @@ class EntityHydrator
} }
}); });
$parents = $filtered ? (new EntityHydrator($parentQuery->get()->all()))->hydrate() : []; $parentModels = $filtered ? $parentQuery->get()->all() : [];
$parents = (new EntityHydrator($parentModels))->hydrate();
$parentMap = []; $parentMap = [];
foreach ($parents as $parent) { foreach ($parents as $parent) {
$parentMap[$parent->type . ':' . $parent->id] = $parent; $parentMap[$parent->type . ':' . $parent->id] = $parent;
@@ -139,12 +140,12 @@ class EntityHydrator
foreach ($entities as $entity) { foreach ($entities as $entity) {
if ($entity instanceof Page || $entity instanceof Chapter) { if ($entity instanceof Page || $entity instanceof Chapter) {
$key = 'book:' . $entity->getAttribute('book_id'); $key = 'book:' . $entity->getRawAttribute('book_id');
$entity->setRelation('book', $parentMap[$key] ?? null); $entity->setAttribute('book', $parentMap[$key] ?? null);
} }
if ($entity instanceof Page) { if ($entity instanceof Page) {
$key = 'chapter:' . $entity->getAttribute('chapter_id'); $key = 'chapter:' . $entity->getRawAttribute('chapter_id');
$entity->setRelation('chapter', $parentMap[$key] ?? null); $entity->setAttribute('chapter', $parentMap[$key] ?? null);
} }
} }
} }

View File

@@ -109,17 +109,12 @@ class SearchRunner
protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int $count): Collection protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int $count): Collection
{ {
$entities = $query->clone() $entities = $query->clone()
// ->with(array_filter($relations))
->skip(($page - 1) * $count) ->skip(($page - 1) * $count)
->take($count) ->take($count)
->get(); ->get();
$hydrated = (new EntityHydrator($entities->all(), true, true))->hydrate(); $hydrated = (new EntityHydrator($entities->all(), true, true))->hydrate();
// TODO - Load in books for pages/chapters efficiently (scoped to visible)
// TODO - Load in chapters for pages efficiently (scoped to visible)
// TODO - Load in tags efficiently
return collect($hydrated); return collect($hydrated);
} }