- Solr Library Update: small changes in SolrIndexBuilder.php, SolariumAdapter.php and SolariumDocument.php

This commit is contained in:
Arno Kaimbacher 2021-05-26 14:38:09 +02:00
parent bcbd05d7d8
commit b335bd7e9c
4 changed files with 45 additions and 25 deletions

View File

@ -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;
}
}

View File

@ -11,6 +11,6 @@ class EncryptCookies extends BaseEncrypter
* @var array
*/
protected $except = [
//
'XDEBUG_SESSION'
];
}

View File

@ -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,6 +67,7 @@ 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) {
@ -68,11 +75,15 @@ class SolariumAdapter
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();
// this executes the query and returns the result
$result = $this->client->update($update);
echo '<b>Update query executed</b><br/>';
echo 'Query status: ' . $result->getStatus() . '<br/>';
echo 'Query time: ' . $result->getQueryTime();
//$this->execute($update, 'failed updating slice 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');

View File

@ -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