Configuration::get('solr.server'), 'port' => Configuration::get('solr.port'), 'login' => Configuration::get('solr.username'), 'password' => Configuration::get('solr.password'), 'path' => Configuration::get('solr.path'), ); $this->client = new \SolrClient($options); $this->query = new \SolrQuery(); $this->query->setQuery('*:*'); $this->query->addField('*'); $this->query->addParam('q.op', 'AND'); } public function addQuery($queryText, $queryField = null, $escape = true) { if($escape) { $queryText = \SolrUtils::escapeQueryChars($queryText); } if (strlen($queryField) > 0) { $queryText = "$queryField:\"$queryText\""; } $this->queryParts[] = $queryText; } public function addSortField($field, $order = \SolrQuery::ORDER_DESC) { $this->query->addSortField($field, $order); } /** * @param int $start * @param int $count * @return array * @throws WebException */ public function getResults($start = 0, $count = 15) { if (count($this->queryParts) == 0) $query = '*:*'; else { $query = implode(' AND ', $this->queryParts); } $this->query->setQuery($query); $this->query->setStart($start); $this->query->setRows($count); try { $results = $this->client->query($this->query)->getResponse(); } catch(\SolrClientException $e) { throw new WebException ("SolrError", $e->getMessage(), -700); } $books = isset($results['response']['docs']) && $results['response']['docs'] ? array_map(function($o) { return (array) $o; }, $results['response']['docs']) : array(); return array( 'count' => $results['response']['numFound'], 'facets' => $results['facet_counts']['facet_fields'], 'books' => $books, ); } /** * Retrieve 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']; } }