/** * Get Solr connection. * * @return \VuFind\Connection\SolrStats */ protected function getSolr() { if (null === $this->solr) { $this->solr = ConnectionManager::connectToIndex('SolrStats'); } return $this->solr; }
/** * Save an XML file to the Solr index using the specified configuration. * * @param string $xmlFile XML file to transform. * @param string $properties Properties file. * @param string $index Solr index to use. * @param bool $testMode Are we in test-only mode? * * @throws \Exception * @return void */ public function save($xmlFile, $properties, $index = 'Solr', $testMode = false) { // Process the file: $xml = $this->generateXML($xmlFile, $properties); // Save the results (or just display them, if in test mode): if (!$testMode) { $solr = ConnectionManager::connectToIndex($index); $result = $solr->saveRecord($xml); } else { Console::write($xml . "\n"); } }
/** * Gathers data for the view of the AlphaBrowser and does some initialization * * @return \Zend\View\Model\ViewModel */ public function homeAction() { $config = ConfigReader::getConfig(); // Load browse types from config file, or use defaults if unavailable: if (isset($config->AlphaBrowse_Types) && !empty($config->AlphaBrowse_Types)) { $types = array(); foreach ($config->AlphaBrowse_Types as $key => $value) { $types[$key] = $value; } } else { $types = array('topic' => 'By Topic', 'author' => 'By Author', 'title' => 'By Title', 'lcc' => 'By Call Number'); } // Connect to Solr: $db = ConnectionManager::connectToIndex(); // Process incoming parameters: $source = $this->params()->fromQuery('source', false); $from = $this->params()->fromQuery('from', false); $page = intval($this->params()->fromQuery('page', 0)); $limit = isset($config->AlphaBrowse->page_size) ? $config->AlphaBrowse->page_size : 20; // Create view model: $view = $this->createViewModel(); // If required parameters are present, load results: if ($source && $from !== false) { // Load Solr data or die trying: try { $result = $db->alphabeticBrowse($source, $from, $page, $limit); // No results? Try the previous page just in case we've gone past // the end of the list.... if ($result['Browse']['totalCount'] == 0) { $page--; $result = $db->alphabeticBrowse($source, $from, $page, $limit); } } catch (SolrException $e) { if ($e->isMissingBrowseIndex()) { throw new \Exception("Alphabetic Browse index missing. See " . "http://vufind.org/wiki/alphabetical_heading_browse for " . "details on generating the index."); } throw $e; } // Only display next/previous page links when applicable: if ($result['Browse']['totalCount'] > $limit) { $view->nextpage = $page + 1; } if ($result['Browse']['offset'] + $result['Browse']['startRow'] > 1) { $view->prevpage = $page - 1; } $view->result = $result; } $view->alphaBrowseTypes = $types; $view->from = $from; $view->source = $source; return $view; }
/** * Support method for performAndProcessSearch -- perform a search based on the * parameters passed to the object. * * @return void */ protected function performSearch() { $solr = ConnectionManager::connectToIndex(); // Collect the search parameters: $params = array('query' => $solr->buildQuery($this->getParams()->getSearchTerms()), 'handler' => $this->getParams()->getSearchHandler(), 'limit' => 0, 'facet' => $this->getParams()->getFacetSettings()); // Perform the search: $this->rawResponse = $solr->search($params); // Get the facets from which we will build our results: $facets = $this->getFacetList(array('authorStr' => null)); if (isset($facets['authorStr'])) { $params = $this->getParams(); $this->resultTotal = ($params->getPage() - 1) * $params->getLimit() + count($facets['authorStr']['list']); $this->results = array_slice($facets['authorStr']['list'], 0, $params->getLimit()); } }
/** * Get reserve info from the catalog or Solr reserves index. * * @param string $course Course ID to use as limit (optional) * @param string $inst Instructor ID to use as limit (optional) * @param string $dept Department ID to use as limit (optional) * * @return array */ public function findReserves($course = null, $inst = null, $dept = null) { // Special case -- process reserves info using index if ($this->useIndex()) { // connect to reserves index $reservesIndex = ConnectionManager::connectToIndex('SolrReserves'); // get the selected reserve record from reserves index // and extract the bib IDs from it $result = $reservesIndex->findReserves($course, $inst, $dept); $bibs = array(); $instructor = isset($result['instructor']) ? $result['instructor'] : ''; $course = isset($result['course']) ? $result['course'] : ''; foreach ($result['bib_id'] as $bib_id) { $bibs[] = array('BIB_ID' => $bib_id, 'bib_id' => $bib_id, 'course' => $course, 'instructor' => $instructor); } return $bibs; } // Default case -- find reserves info from the catalog $catalog = $this->getController()->getILS(); return $catalog->findReserves($course, $inst, $dept); }
/** * Get a connection to the Solr index. * * @param null|array $shards Selected shards to use (null for defaults) * @param string $index ID of index/search classes to use (this assumes * that \VuFind\Search\$index\Options and \VuFind\Connection\$index are both * valid classes) * * @return \VuFind\Connection\Solr */ public function getSolrConnection($shards = null, $index = 'Solr') { // Turn on all shards by default if none are specified (we need to be sure // that any given ID will yield results, even if not all shards are on by // default). $sm = $this->getSearchManager(); $options = $sm->setSearchClassId($index)->getOptionsInstance(); $allShards = $options->getShards(); if (is_null($shards)) { $shards = array_keys($allShards); } // If we have selected shards, we need to format them: if (!empty($shards)) { $selectedShards = array(); foreach ($shards as $current) { $selectedShards[$current] = $allShards[$current]; } $shards = $selectedShards; } // Connect to Solr and set up shards: $solr = ConnectionManager::connectToIndex($index); $solr->setShards($shards, $options->getSolrShardsFieldsToStrip()); return $solr; }
/** * Generate the sitemaps based on settings established by the constructor. * * @return void */ public function generate() { $solr = ConnectionManager::connectToIndex(); $currentPage = 1; $last_term = ''; while (true) { if ($currentPage == 1) { $fileWhole = $this->fileStart . ".xml"; } else { $fileWhole = $this->fileStart . "-" . $currentPage . ".xml"; } // Get $current_page_info_array = $solr->getTerms('id', $last_term, $this->countPerPage); if (!isset($current_page_info_array) || count($current_page_info_array) < 1) { break; } else { $smf = $this->openSitemapFile($fileWhole, 'urlset'); foreach ($current_page_info_array as $item => $count) { $loc = htmlspecialchars($this->resultUrl . urlencode($item)); if (strpos($loc, 'http') === false) { $loc = 'http://' . $loc; } fwrite($smf, '<url>' . "\n"); fwrite($smf, ' <loc>' . $loc . '</loc>' . "\n"); fwrite($smf, ' <changefreq>' . htmlspecialchars($this->frequency) . '</changefreq>' . "\n"); fwrite($smf, '</url>' . "\n"); $last_term = $item; } fwrite($smf, '</urlset>'); fclose($smf); } $currentPage++; } // Set-up Sitemap Index $this->buildIndex($currentPage - 1); }
/** * Set up the Solr index so we can retrieve some record data. * * @return void */ protected function prepSolr() { // Create or solr connection $this->db = ConnectionManager::connectToIndex(); // Get the total # of records in the system $result = $this->db->search(array('query' => '*:*')); $this->totalRecords = $result['response']['numFound']; }
/** * Display repair instructions for Solr problems. * * @return mixed */ public function fixsolrAction() { // In Windows, localhost may fail -- see if switching to 127.0.0.1 helps: $config = ConfigReader::getConfig(); $configFile = ConfigReader::getLocalConfigPath('config.ini', null, true); if (stristr($config->Index->url, 'localhost')) { $newUrl = str_replace('localhost', '127.0.0.1', $config->Index->url); try { $solr = ConnectionManager::connectToIndex(null, null, $newUrl); $results = $solr->search(); // If we got this far, the fix worked. Let's write it to disk! $writer = new ConfigWriter($configFile); $writer->set('Index', 'url', $newUrl); if (!$writer->save()) { return $this->forwardTo('Install', 'fixbasicconfig'); } return $this->redirect()->toRoute('install-home'); } catch (\Exception $e) { // Didn't work! } } // If we got this far, the automatic fix didn't work, so let's just assign // some variables to use in offering troubleshooting advice: $view = $this->createViewModel(); $view->rawUrl = $config->Index->url; $view->userUrl = str_replace(array('localhost', '127.0.0.1'), $this->getRequest()->getServer()->get('HTTP_HOST'), $config->Index->url); $view->core = isset($config->Index->default_core) ? $config->Index->default_core : "biblio"; $view->configFile = $configFile; return $view; }
/** * Command-line tool to delete suppressed records from the index. * * @return void */ public function suppressedAction() { // Setup Solr Connection $this->consoleOpts->addRules(array('authorities' => 'Delete authority records instead of bibliographic records')); $core = $this->consoleOpts->getOption('authorities') ? 'authority' : 'biblio'; $solr = ConnectionManager::connectToIndex('Solr', $core); // Make ILS Connection try { $catalog = $this->getILS(); if ($core == 'authority') { $result = $catalog->getSuppressedAuthorityRecords(); } else { $result = $catalog->getSuppressedRecords(); } } catch (\Exception $e) { Console::writeLine("ILS error -- " . $e->getMessage()); return $this->getFailureResponse(); } // Validate result: if (!is_array($result)) { Console::writeLine("Could not obtain suppressed record list from ILS."); return $this->getFailureResponse(); } else { if (empty($result)) { Console::writeLine("No suppressed records to delete."); return $this->getSuccessResponse(); } } // Get Suppressed Records and Delete from index $status = $solr->deleteRecords($result); if ($status) { // Commit and Optimize $solr->commit(); $solr->optimize(); } else { Console::writeLine("Delete failed."); return $this->getFailureResponse(); } return $this->getSuccessResponse(); }