]> BookStack Code Mirror - bookstack/commitdiff
Search: Improved result hydration performance
authorDan Brown <redacted>
Mon, 27 Oct 2025 18:02:54 +0000 (18:02 +0000)
committerDan Brown <redacted>
Mon, 27 Oct 2025 18:02:54 +0000 (18:02 +0000)
app/Entities/Tools/EntityHydrator.php
app/Search/SearchRunner.php

index fbfc8762e72fabbcfe84315c040508c151f1fe4e..cb62674237c2f9ade99a0c15bff592f22f3b921c 100644 (file)
@@ -37,7 +37,7 @@ class EntityHydrator
         $hydrated = [];
 
         foreach ($this->entities as $entity) {
         $hydrated = [];
 
         foreach ($this->entities as $entity) {
-            $data = $entity->toArray();
+            $data = $entity->getRawOriginal();
             $instance = Entity::instanceFromType($entity->type);
 
             if ($instance instanceof Page) {
             $instance = Entity::instanceFromType($entity->type);
 
             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 = [];
         foreach ($parents as $parent) {
             $parentMap[$parent->type . ':' . $parent->id] = $parent;
         $parentMap = [];
         foreach ($parents as $parent) {
             $parentMap[$parent->type . ':' . $parent->id] = $parent;
@@ -139,12 +140,12 @@ class EntityHydrator
 
         foreach ($entities as $entity) {
             if ($entity instanceof Page || $entity instanceof Chapter) {
 
         foreach ($entities as $entity) {
             if ($entity instanceof Page || $entity instanceof Chapter) {
-                $key = 'book:' . $entity->getAttribute('book_id');
-                $entity->setRelation('book', $parentMap[$key] ?? null);
+                $key = 'book:' . $entity->getRawAttribute('book_id');
+                $entity->setAttribute('book', $parentMap[$key] ?? null);
             }
             if ($entity instanceof Page) {
             }
             if ($entity instanceof Page) {
-                $key = 'chapter:' . $entity->getAttribute('chapter_id');
-                $entity->setRelation('chapter', $parentMap[$key] ?? null);
+                $key = 'chapter:' . $entity->getRawAttribute('chapter_id');
+                $entity->setAttribute('chapter', $parentMap[$key] ?? null);
             }
         }
     }
             }
         }
     }
index 72ebe96bb4e8688455ae8a62045ea7860590a884..4b19aceac7af85596342c534c1561f910c31d22a 100644 (file)
@@ -109,17 +109,12 @@ class SearchRunner
     protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int $count): Collection
     {
         $entities = $query->clone()
     protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page, int $count): Collection
     {
         $entities = $query->clone()
-//            ->with(array_filter($relations))
             ->skip(($page - 1) * $count)
             ->take($count)
             ->get();
 
         $hydrated = (new EntityHydrator($entities->all(), true, true))->hydrate();
 
             ->skip(($page - 1) * $count)
             ->take($count)
             ->get();
 
         $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);
     }