Use Solr everywhere to retrieve books

master
Gilles Crettenand 11 years ago
parent 3b66d628d8
commit f2f5078dbd

@ -86,4 +86,34 @@ class BookSearch
'books' => $books, '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'];
}
} }

@ -2,6 +2,7 @@
namespace BSR\Lib\db; namespace BSR\Lib\db;
use BSR\Lib\Configuration; use BSR\Lib\Configuration;
use BSR\Lib\Search\BookSearch;
/** /**
* User is mapped on the Useraccounts table. Contains user information : id, login, firstName, lastName, displayName. * 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); $result = Connection::execute($sql);
$circulations = $result->to_array(); $circulations = $result->to_array();
$books = array_map(function($c) { return $c['NoticeID']; }, $circulations); $ids = array_map(function($c) { return $c['NoticeID']; }, $circulations);
$books = AudioBook::findBy('NoticeID', $books, true); $books = BookSearch::GetBooks($ids, 'id');
foreach($circulations as $c) { foreach($circulations as $c) {
$books[$c['NoticeID']]['date'] = $c['CheckOutDate']; $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. * Wishes are all the books that this user want to read.
* @param int $limit * @param int $limit
* @return AudioBook[] * @return array
*/ */
public function getWishes($limit = 50) public function getWishes($limit = 50)
{ {
@ -189,7 +190,7 @@ class User extends DbMapping
$result = Connection::execute($sql); $result = Connection::execute($sql);
$ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array()); $ids = array_map(function($r) { return $r['NoticeID']; }, $result->to_array());
return AudioBook::findBy('NoticeID', $ids, true); return BookSearch::GetBooks($ids, 'id');
} }
/** /**

@ -384,13 +384,13 @@ class NetBiblio extends WebService
public function FindBooks($codes) public function FindBooks($codes)
{ {
$this->CheckSession(); $this->CheckSession();
return $this->AddBookData($this->GetBooks(json_decode($codes))); return $this->AddBookData(BookSearch::GetBooks(json_decode($codes)));
} }
public function FindBook($code) public function FindBook($code)
{ {
$this->CheckSession(); $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) { public function GetRandomBooks($number = 100, $seed = null) {

Loading…
Cancel
Save