Use Solr everywhere to retrieve books

This commit is contained in:
Gilles Crettenand
2015-06-02 23:00:34 +02:00
parent 3b66d628d8
commit f2f5078dbd
3 changed files with 37 additions and 6 deletions

View File

@@ -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'];
}
}