fix: not returning all the results

This commit is contained in:
Guillermo Pages
2025-02-20 17:05:36 +01:00
parent 7e8302c830
commit 3c896ec409

View File

@@ -285,23 +285,23 @@ class BookSearch
* @throws WebException * @throws WebException
*/ */
public static function getBooks(array $codes, $field = 'code') { 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; $limit = 15;
$count = count($codes);
$bs = self::getLastInstance(); $bs = self::getLastInstance();
$parts = $count <= $limit ? [$codes] : array_chunk($codes, $limit);
$books = []; $books = [];
foreach($parts as $p) { foreach (array_chunk($codes, $limit) as $chunk) {
// if we use array_merge here the numerical keys (book code) will be lost // Reset query state for this chunk
$bs->addOrQuery($p, $field); $bs->clearQueryParts();
$results = $bs->getResults(0, $count); $bs->addOrQuery($chunk, $field);
$foundBooks = $results['books']; // Use count($chunk) so you only request as many rows as there are codes in this chunk
$books += $foundBooks; $results = $bs->getResults(0, count($chunk));
$books += $results['books'];
}
return $books;
} }
return $books; public function clearQueryParts(): void {
$this->queryParts = [];
$this->filterQueryParts = [];
} }
/** /**