factorize solr results formatting in BookSearch

master
Gilles Crettenand 11 years ago
parent 6abf00985d
commit 7b2c4c18ee

@ -51,7 +51,8 @@ class BookSearch
/** /**
* @param int $start * @param int $start
* @param int $count * @param int $count
* @return SolrObject * @return array
* @throws WebException
*/ */
public function getResults($start = 0, $count = 15) public function getResults($start = 0, $count = 15)
{ {
@ -64,6 +65,20 @@ class BookSearch
$this->query->setStart($start); $this->query->setStart($start);
$this->query->setRows($count); $this->query->setRows($count);
return $this->client->query($this->query)->getResponse(); try {
$results = $this->client->query($this->query)->getResponse();
} catch(SolrClientException $e) {
throw new WebException ("SolrError", $e->getMessage(), -700);
}
$books = isset($results['response']['docs']) ?
array_map(function($o) { return (array) $o; }, $results['response']['docs']) :
false;
return array(
'count' => $results['response']['numFound'],
'facets' => $results['facet_counts']['facet_fields'],
'books' => $books,
);
} }
} }

@ -357,12 +357,7 @@ class NetBiblio extends WebService
$bs = new BookSearch(); $bs = new BookSearch();
$bs->addSortField('random_'.$seed); $bs->addSortField('random_'.$seed);
$results = $bs->getResults(0, $number); $results = $bs->getResults(0, $number);
return $results['books'] ? $this->AddBookData($results['books']) : array();
if($results['response']['docs']) {
$books = array_map(function($o) { return (array) $o; }, $results['response']['docs']);
return $this->AddBookData($books);
}
return array();
} }
public function Search($query, $start, $limit) public function Search($query, $start, $limit)
@ -444,21 +439,14 @@ class NetBiblio extends WebService
$count = isset($queryArray['count']) ? (int) $queryArray['count'] : Configuration::get('solr.result_count'); $count = isset($queryArray['count']) ? (int) $queryArray['count'] : Configuration::get('solr.result_count');
$start = isset($queryArray['page']) ? $queryArray['page'] * $count : 0; $start = isset($queryArray['page']) ? $queryArray['page'] * $count : 0;
try { $results = $bs->getResults($start, $count);
$results = $bs->getResults($start, $count);
} catch(SolrClientException $e) {
throw new WebException ("SolrError", $e->getMessage(), -700);
}
$data = array( $data = array(
'count' => $results['response']['numFound'], 'count' => $results['count'],
'facets' => $results['facet_counts']['facet_fields'], 'facets' => $results['facets'],
); );
if($results['response']['docs']) { if($results['books']) {
$books = array_map(function($o) { return (array) $o; }, $results['response']['docs']); $data = array_merge($data, $this->AddBookData($results['books']));
$books = $this->AddBookData($books);
$data = array_merge($data, $books);
} }
return $data; return $data;

Loading…
Cancel
Save