Make Solr instantiation more robust

This commit is contained in:
Sebastian Meyer 2020-12-21 09:13:19 +01:00
parent 6786da5683
commit 5c9d79ae2d
9 changed files with 37 additions and 26 deletions

View File

@ -565,7 +565,10 @@ class DocumentList implements \ArrayAccess, \Countable, \Iterator, \TYPO3\CMS\Co
// Get Solr instance.
if (!$this->solr) {
// Connect to Solr server.
if ($this->solr = Solr::getInstance($this->metadata['options']['core'])) {
$solr = Solr::getInstance($this->metadata['options']['core']);
if ($solr->ready) {
$this->solr = $solr;
// Get indexed fields.
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_dlf_metadata');

View File

@ -560,7 +560,9 @@ class Indexer
// Get Solr instance.
if (!self::$solr) {
// Connect to Solr server.
if (self::$solr = Solr::getInstance($core)) {
$solr = Solr::getInstance($core);
if ($solr->ready) {
self::$solr = $solr;
// Load indexing configuration if needed.
if ($pid) {
self::loadIndexConf($pid);

View File

@ -274,9 +274,11 @@ class Solr
$core = Helper::getIndexNameFromUid($core, 'tx_dlf_solrcores');
}
// Check if core is set or null.
if (empty($core) && $core !== null) {
if (
empty($core)
&& $core !== null
) {
Helper::devLog('Invalid core UID or name given for Apache Solr', DEVLOG_SEVERITY_ERROR);
return;
}
if (!empty($core)) {
// Check if there is an instance in the registry already.
@ -291,13 +293,10 @@ class Solr
// Create new instance...
$instance = new self($core);
// ...and save it to registry.
if (
$instance->ready
&& !empty($instance->core)
) {
self::$registry[$instance->core] = $instance;
} else {
if (!$instance->ready) {
Helper::devLog('Could not connect to Apache Solr service', DEVLOG_SEVERITY_ERROR);
} elseif (!empty($instance->core)) {
self::$registry[$instance->core] = $instance;
}
return $instance;
}
@ -617,7 +616,7 @@ class Solr
*
* @return void
*/
protected function __construct($core = null)
protected function __construct($core)
{
// Get Solr connection parameters from configuration.
$this->loadSolrConnectionInfo();
@ -649,17 +648,15 @@ class Solr
// Check if connection is established.
$query = $this->service->createCoreAdmin();
$action = $query->createStatus();
if (!empty($core)) {
if ($core !== null) {
$action->setCore($core);
}
$query->setAction($action);
try {
$response = $this->service->coreAdmin($query);
if ($response->getWasSuccessful()) {
// Instantiation successful!
$this->ready = true;
// Solr is reachable, but is the core as well?
if (!empty($core)) {
if ($core !== null) {
$result = $response->getStatusResultByCoreName($core);
if (
$result instanceof \Solarium\QueryType\Server\CoreAdmin\Result\StatusResult
@ -667,8 +664,13 @@ class Solr
) {
// Set core name.
$this->core = $core;
} else {
// Core not available.
return;
}
}
// Instantiation successful!
$this->ready = true;
}
} catch (\Exception $e) {
// Nothing to do here.

View File

@ -246,7 +246,8 @@ class DataHandler
$resArray = $allResults[0];
if ($resArray['hidden']) {
// Establish Solr connection.
if ($solr = Solr::getInstance($resArray['core'])) {
$solr = Solr::getInstance($resArray['core']);
if ($solr->ready) {
// Delete Solr document.
$updateQuery = $solr->service->createUpdate();
$updateQuery->addDeleteQuery('uid:' . $id);
@ -325,7 +326,8 @@ class DataHandler
case 'move':
case 'delete':
// Establish Solr connection.
if ($solr = Solr::getInstance($resArray['core'])) {
$solr = Solr::getInstance($resArray['core']);
if ($solr->ready) {
// Delete Solr document.
$updateQuery = $solr->service->createUpdate();
$updateQuery->addDeleteQuery('uid:' . $id);

View File

@ -142,6 +142,10 @@ class Collection extends \Kitodo\Dlf\Common\AbstractPlugin
$this->showSingleCollection(intval($resArray['uid']));
}
$solr = Solr::getInstance($this->conf['solrcore']);
if (!$solr->ready) {
Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
return $content;
}
// We only care about the UID and partOf in the results and want them sorted
$params['fields'] = 'uid,partof';
$params['sort'] = ['uid' => 'asc'];

View File

@ -53,10 +53,7 @@ class SearchInDocument
$core = Helper::decrypt($encrypted, $hashed);
// Perform Solr query.
$solr = Solr::getInstance($core);
if (
$solr->ready
&& $solr->core === $core
) {
if ($solr->ready) {
$query = $solr->service->createSelect();
$query->setFields(['id', 'uid', 'page']);
$query->setQuery('fulltext:(' . Solr::escapeQuery((string) $parameters['q']) . ') AND uid:' . intval($parameters['uid']));

View File

@ -49,10 +49,7 @@ class SearchSuggest
$core = Helper::decrypt($encrypted, $hashed);
// Perform Solr query.
$solr = Solr::getInstance($core);
if (
$solr->ready
&& $solr->core === $core
) {
if ($solr->ready) {
$query = $solr->service->createSuggester();
$query->setCount(10);
$query->setDictionary('suggest');

View File

@ -923,6 +923,10 @@ class OaiPmh extends \Kitodo\Dlf\Common\AbstractPlugin
$solr_query .= ' AND timestamp:[' . $from . ' TO ' . $until . ']';
$documentSet = [];
$solr = Solr::getInstance($this->conf['solrcore']);
if (!$solr->ready) {
Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR);
return $documentSet;
}
if (intval($this->conf['solr_limit']) > 0) {
$solr->limit = intval($this->conf['solr_limit']);
}

View File

@ -141,7 +141,7 @@ class ext_update
while ($resArray = $result->fetch()) {
// Instantiate search object.
$solr = Solr::getInstance($resArray['index_name']);
if ($solr->core !== $resArray['index_name']) {
if (!$solr->ready) {
return true;
}
}