diff --git a/Lib/Search/BookSearch.php b/Lib/Search/BookSearch.php index e678ca9..73c5364 100644 --- a/Lib/Search/BookSearch.php +++ b/Lib/Search/BookSearch.php @@ -86,4 +86,34 @@ class BookSearch 'books' => $books, ); } + + + /** + * Retrive books from Solr based on their code (NoticeNr). + * + * @param array $codes + * @param string $field the field to use for the search + * @return array Books information + * @throws WebException + */ + public static function GetBooks(array $codes, $field = 'code') { + // it is faster to do multiple small request to Solr rather than one big so separate + // in chunks if we are above the limit. 15 was found by testing and seems to be a sweet spot + $limit = 15; + $count = count($codes); + if($count > $limit) { + $parts = array_chunk($codes, $limit); + $books = array(); + foreach($parts as $p) { + $books = array_merge($books, self::GetBooks($p)); + } + return $books; + } + + $bs = new static(); + $query = sprintf('%s:(%s)', $field, implode(' OR ', $codes)); + $bs->addQuery($query, null, false); + $results = $bs->getResults(0, $count); + return $results['books']; + } } diff --git a/Lib/db/User.php b/Lib/db/User.php index 82e7ecf..487e3ef 100644 --- a/Lib/db/User.php +++ b/Lib/db/User.php @@ -2,6 +2,7 @@ namespace BSR\Lib\db; use BSR\Lib\Configuration; +use BSR\Lib\Search\BookSearch; /** * User is mapped on the Useraccounts table. Contains user information : id, login, firstName, lastName, displayName. @@ -103,8 +104,8 @@ class User extends DbMapping $result = Connection::execute($sql); $circulations = $result->to_array(); - $books = array_map(function($c) { return $c['NoticeID']; }, $circulations); - $books = AudioBook::findBy('NoticeID', $books, true); + $ids = array_map(function($c) { return $c['NoticeID']; }, $circulations); + $books = BookSearch::GetBooks($ids, 'id'); foreach($circulations as $c) { $books[$c['NoticeID']]['date'] = $c['CheckOutDate']; @@ -176,7 +177,7 @@ class User extends DbMapping /** * Wishes are all the books that this user want to read. * @param int $limit - * @return AudioBook[] + * @return array */ public function getWishes($limit = 50) { @@ -189,7 +190,7 @@ class User extends DbMapping $result = Connection::execute($sql); $ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array()); - return AudioBook::findBy('NoticeID', $ids, true); + return BookSearch::GetBooks($ids, 'id'); } /** diff --git a/NetBiblio.php b/NetBiblio.php index 05a2fab..77f2d9c 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -384,13 +384,13 @@ class NetBiblio extends WebService public function FindBooks($codes) { $this->CheckSession(); - return $this->AddBookData($this->GetBooks(json_decode($codes))); + return $this->AddBookData(BookSearch::GetBooks(json_decode($codes))); } public function FindBook($code) { $this->CheckSession(); - return reset($this->AddBookData($this->GetBooks(array($code)))); + return reset($this->AddBookData(BookSearch::GetBooks(array($code)))); } public function GetRandomBooks($number = 100, $seed = null) {