+ return [
+ 'total' => $total,
+ 'count' => count($results),
+ 'results' => $results->sortByDesc('score')
+ ];
+ }
+
+
+ /**
+ * Search a book for entities
+ * @param integer $bookId
+ * @param string $searchString
+ * @return Collection
+ */
+ public function searchBook($bookId, $searchString)
+ {
+ $terms = $this->parseSearchString($searchString);
+ $entityTypes = ['page', 'chapter'];
+ $entityTypesToSearch = isset($terms['filters']['type']) ? explode('|', $terms['filters']['type']) : $entityTypes;
+
+ $results = collect();
+ foreach ($entityTypesToSearch as $entityType) {
+ if (!in_array($entityType, $entityTypes)) continue;
+ $search = $this->buildEntitySearchQuery($terms, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
+ $results = $results->merge($search);
+ }
+ return $results->sortByDesc('score')->take(20);
+ }
+
+ /**
+ * Search a book for entities
+ * @param integer $chapterId
+ * @param string $searchString
+ * @return Collection
+ */
+ public function searchChapter($chapterId, $searchString)
+ {
+ $terms = $this->parseSearchString($searchString);
+ $pages = $this->buildEntitySearchQuery($terms, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
+ return $pages->sortByDesc('score');