From a15fa4f093e396aba42fd979b2334cd3bd214351 Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Tue, 2 Jun 2015 15:17:28 +0200 Subject: [PATCH] improve FindBooks for great number by operating in chunk --- Lib/Search/BookSearch.php | 2 +- NetBiblio.php | 40 +++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Lib/Search/BookSearch.php b/Lib/Search/BookSearch.php index f161965..1507a52 100644 --- a/Lib/Search/BookSearch.php +++ b/Lib/Search/BookSearch.php @@ -78,7 +78,7 @@ class BookSearch $books = isset($results['response']['docs']) && $results['response']['docs'] ? array_map(function($o) { return (array) $o; }, $results['response']['docs']) : - false; + array(); return array( 'count' => $results['response']['numFound'], diff --git a/NetBiblio.php b/NetBiblio.php index dbe6d50..2b74541 100644 --- a/NetBiblio.php +++ b/NetBiblio.php @@ -212,16 +212,33 @@ class NetBiblio extends WebService $this->getUser()->deleteWish($bookNr); } + private function GetBooks(array $codes) { + $bs = new BookSearch(); + $bs->addQuery('code:('.implode(' OR ', $codes).')', null, false); + $results = $bs->getResults(0, count($codes)); + return $results['books']; + } + public function FindBooks($codes) { $this->CheckSession(); $codes = json_decode($codes); - $bs = new BookSearch(); - $bs->addQuery('code:('.implode(' OR ', $codes).')', null, false); - $results = $bs->getResults(0, count($codes)); - return $results['books'] ? $this->AddBookData($results['books']) : array(); + // 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; + if(count($codes) > $limit) { + $parts = array_chunk($codes, $limit); + $books = array(); + foreach($parts as $p) { + $books = array_merge($books, $this->GetBooks($p)); + } + } else { + $books = $this->GetBooks($codes); + } + + return $this->AddBookData($books); } private function GetFiles(array $ids) @@ -367,11 +384,7 @@ class NetBiblio extends WebService public function FindBook($code) { $this->CheckSession(); - - $bs = new BookSearch(); - $bs->addQuery($code, 'code'); - $results = $bs->getResults(0, 1); - return $results['books'] ? reset($this->AddBookData($results['books'])) : null; + return reset($this->AddBookData($this->GetBooks(array($code)))); } public function GetRandomBooks($number = 100, $seed = null) { @@ -381,8 +394,7 @@ class NetBiblio extends WebService $bs = new BookSearch(); $bs->addSortField('random_'.$seed); - $results = $bs->getResults(0, $number); - return $results['books'] ? $this->AddBookData($results['books']) : array(); + return $this->AddBookData($bs->getResults(0, $number)); } public function Search($query, $start, $limit) @@ -470,11 +482,7 @@ class NetBiblio extends WebService 'facets' => $results['facets'], ); - if($results['books']) { - $data = array_merge($data, $this->AddBookData($results['books'])); - } - - return $data; + return array_merge($data, $this->AddBookData($results['books'])); } public function ListOfReaders()