Use Solr everywhere to retrieve books
This commit is contained in:
@@ -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'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user