From b335bd7e9cc79e19725eb4575a42a75dfac242b3 Mon Sep 17 00:00:00 2001 From: Arno Kaimbacher Date: Wed, 26 May 2021 14:38:09 +0200 Subject: [PATCH] - Solr Library Update: small changes in SolrIndexBuilder.php, SolariumAdapter.php and SolariumDocument.php --- app/Console/Commands/SolrIndexBuilder.php | 29 +++++++++++++------- app/Http/Middleware/EncryptCookies.php | 2 +- app/Library/Search/SolariumAdapter.php | 33 +++++++++++++++-------- app/Library/Search/SolariumDocument.php | 6 ++--- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/app/Console/Commands/SolrIndexBuilder.php b/app/Console/Commands/SolrIndexBuilder.php index 2f3deda..595b47b 100644 --- a/app/Console/Commands/SolrIndexBuilder.php +++ b/app/Console/Commands/SolrIndexBuilder.php @@ -5,6 +5,11 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use App\Models\Dataset; +use Illuminate\Support\Facades\Log; +use App\Library\Search\SolariumAdapter; +use \Exception; + + class SolrIndexBuilder extends Command { /** @@ -43,17 +48,21 @@ class SolrIndexBuilder extends Command // update statistics table foreach ($datasets as $dataset) { $datasetId = $dataset->id; - $time = new \Illuminate\Support\Carbon(); - $dataset->server_date_modified = $time; - $dataset->save(); - // try { - // // Opus_Search_Service::selectIndexingService('onDocumentChange') - // $service = new SolariumAdapter("solr", config('solarium')); - // $service->addDatasetsToIndex($dataset); - // } catch (Exception $e) { - // Log::debug(__METHOD__ . ': ' . 'Indexing document ' . $datasetId . ' failed: ' . $e->getMessage()); - // } + // $time = new \Illuminate\Support\Carbon(); + // $dataset->server_date_modified = $time; + // $dataset->save(); + // Log::debug(__METHOD__ . ': ' . 'Adding index job for dataset ' . $datasetId . '.'); + + try { + // Opus_Search_Service::selectIndexingService('onDocumentChange') + $service = new SolariumAdapter("solr", config('solarium')); + $service->addDatasetsToIndex($dataset); + } catch (Exception $e) { + $this->error(__METHOD__ . ': ' . 'Indexing document ' . $dataset->id . ' failed: ' . $e->getMessage()); + } } return 0; } + + } diff --git a/app/Http/Middleware/EncryptCookies.php b/app/Http/Middleware/EncryptCookies.php index b540c39..daf435e 100644 --- a/app/Http/Middleware/EncryptCookies.php +++ b/app/Http/Middleware/EncryptCookies.php @@ -11,6 +11,6 @@ class EncryptCookies extends BaseEncrypter * @var array */ protected $except = [ - // + 'XDEBUG_SESSION' ]; } diff --git a/app/Library/Search/SolariumAdapter.php b/app/Library/Search/SolariumAdapter.php index 5287cbe..da8a3f4 100644 --- a/app/Library/Search/SolariumAdapter.php +++ b/app/Library/Search/SolariumAdapter.php @@ -4,10 +4,12 @@ namespace App\Library\Search; //use App\Library\Util\SolrSearchQuery; use App\Library\Search\SearchResult; -use App\Library\Util\SearchParameter; -use Illuminate\Support\Facades\Log; use App\Library\Search\SolariumDocument; +use App\Library\Util\SearchParameter; use App\Models\Dataset; +use Illuminate\Support\Facades\Log; +use Solarium\Core\Client\Adapter\Curl; +use Symfony\Component\EventDispatcher\EventDispatcher; use \Solarium\QueryType\Select\Query\Query; class SolariumAdapter @@ -22,7 +24,11 @@ class SolariumAdapter public function __construct($serviceName, $options) { $this->options = $options; - $this->client = new \Solarium\Client($options); + $adapter = new Curl(); + $dispatcher = new EventDispatcher(); + + $this->client = new \Solarium\Client($adapter, $dispatcher, $options); + // $this->client = new \Solarium\Client($options); // ensure service is basically available $ping = $this->client->createPing(); @@ -61,25 +67,30 @@ class SolariumAdapter $slices = array_chunk($datasets, 16); // update documents of every chunk in a separate request foreach ($slices as $slice) { + // get an update query instance $update = $this->client->createUpdate(); $updateDocs = array_map(function ($rdrDoc) use ($builder, $update) { - $solarium_document = $update->createDocument(); + $solarium_document = $update->createDocument(); return $builder->toSolrUpdateDocument($rdrDoc, $solarium_document); }, $slice); - // adding the document to the update query + // add the documents and a commit command to the update query $update->addDocuments($updateDocs); - // Then commit the update: - $update->addCommit(); - $result = $this->client->update($update); + $update->addCommit(); + + // this executes the query and returns the result + $result = $this->client->update($update); + echo 'Update query executed
'; + echo 'Query status: ' . $result->getStatus() . '
'; + echo 'Query time: ' . $result->getQueryTime(); //$this->execute($update, 'failed updating slice of documents'); } // finally commit all updates // $update = $this->client->createUpdate(); - + // $update->addCommit(); // $this->execute($update, 'failed committing update of documents'); @@ -196,7 +207,7 @@ class SolariumAdapter $result = null; try { $result = $this->client->execute($query); - } catch (\Solarium\Exception\HttpException $e) { + } catch (\Solarium\Exception\HttpException$e) { sprintf('%s: %d %s', $actionText, $e->getCode(), $e->getStatusMessage()); } finally { return $result; @@ -207,7 +218,7 @@ class SolariumAdapter // } } - protected function processQuery(\Solarium\QueryType\Select\Query\Query $query): SearchResult + protected function processQuery(\Solarium\QueryType\Select\Query\Query$query): SearchResult { // send search query to service $request = $this->execute($query, 'failed querying search engine'); diff --git a/app/Library/Search/SolariumDocument.php b/app/Library/Search/SolariumDocument.php index cf605e0..cfaddc4 100644 --- a/app/Library/Search/SolariumDocument.php +++ b/app/Library/Search/SolariumDocument.php @@ -3,7 +3,7 @@ namespace App\Library\Search; use App\Models\Dataset; -use Solarium\QueryType\Update\Query\Document\Document; +use Solarium\QueryType\Update\Query\Document; use Solarium\QueryType\Update\Query\Document\DocumentInterface; class SolariumDocument extends SolrDocumentXslt @@ -13,10 +13,10 @@ class SolariumDocument extends SolrDocumentXslt parent::__construct($options); } - public function toSolrUpdateDocument(Dataset $rdrDataset, DocumentInterface $solrDoc) + public function toSolrUpdateDocument(Dataset $rdrDataset, $solrDoc) { if (!($solrDoc instanceof Document)) { - throw new \Exception('provided Solr document must be instance of Solarium Update Document'); + throw new \Exception(get_class($solrDoc) . 'provided Solr document must be instance of Solarium Update Document'); } // convert Opus document to Solr XML document for supporting custom transformations