Skip to content

Commit

Permalink
refactor: extract SolrIndexService
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Mar 12, 2024
1 parent c1b2473 commit 299485a
Show file tree
Hide file tree
Showing 17 changed files with 364 additions and 205 deletions.
2 changes: 1 addition & 1 deletion src/Console/Command/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Indexer extends Command
*/
public function __construct(
private readonly iterable $documentEnricherList,
private readonly SolrIndexerBuilder $solrIndexerBuilder,
private readonly InternalResourceIndexerBuilder $solrIndexerBuilder,
private readonly IndexerProgressBarFactory $progressBarFactory
) {
parent::__construct();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
use Atoolo\Search\Service\Indexer\DocumentEnricher;
use Atoolo\Search\Service\Indexer\IndexDocument;
use Atoolo\Search\Service\Indexer\IndexingAborter;
use Atoolo\Search\Service\Indexer\InternalResourceIndexer;
use Atoolo\Search\Service\Indexer\LocationFinder;
use Atoolo\Search\Service\Indexer\SiteKit\ContentMatcher;
use Atoolo\Search\Service\Indexer\SiteKit\DefaultSchema2xDocumentEnricher;
use Atoolo\Search\Service\Indexer\SiteKit\HeadlineMatcher;
use Atoolo\Search\Service\Indexer\SiteKit\RichtTextMatcher;
use Atoolo\Search\Service\Indexer\SiteKit\SubDirTranslationSplitter;
use Atoolo\Search\Service\Indexer\SolrIndexer;
use Atoolo\Search\Service\Indexer\SolrIndexService;
use Atoolo\Search\Service\SolrParameterClientFactory;

class SolrIndexerBuilder
class InternalResourceIndexerBuilder
{
private string $resourceDir;
/**
Expand All @@ -35,8 +36,9 @@ public function __construct(
private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder
) {
}
public function resourceDir(string $resourceDir): SolrIndexerBuilder
{
public function resourceDir(
string $resourceDir
): InternalResourceIndexerBuilder {
$this->resourceDir = $resourceDir;
return $this;
}
Expand All @@ -47,26 +49,26 @@ public function resourceDir(string $resourceDir): SolrIndexerBuilder
*/
public function documentEnricherList(
iterable $documentEnricherList
): SolrIndexerBuilder {
): InternalResourceIndexerBuilder {
$this->documentEnricherList = $documentEnricherList;
return $this;
}

public function progressBar(
IndexerProgressBar $progressBar
): SolrIndexerBuilder {
): InternalResourceIndexerBuilder {
$this->progressBar = $progressBar;
return $this;
}

public function solrConnectionUrl(
string $solrConnectionUrl
): SolrIndexerBuilder {
): InternalResourceIndexerBuilder {
$this->solrConnectionUrl = $solrConnectionUrl;
return $this;
}

public function build(): SolrIndexer
public function build(): InternalResourceIndexer
{
$resourceBaseLocator = $this->resourceBaseLocatorBuilder->build(
$this->resourceDir
Expand Down Expand Up @@ -106,17 +108,19 @@ public function build(): SolrIndexer
0
);

$solrIndexService = new SolrIndexService($clientFactory);

$translationSplitter = new SubDirTranslationSplitter();

$aborter = new IndexingAborter('.');

return new SolrIndexer(
return new InternalResourceIndexer(
$documentEnricherList,
$this->progressBar,
$finder,
$resourceLoader,
$translationSplitter,
$clientFactory,
$solrIndexService,
$aborter,
'internal'
);
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Indexer/BackgroundIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class BackgroundIndexer implements Indexer
{
public function __construct(
private readonly SolrIndexerFactory $indexerFactory,
private readonly InternalResourceIndexerFactory $indexerFactory,
private readonly IndexerStatusStore $statusStore,
private readonly LoggerInterface $logger = new NullLogger(),
private readonly LockFactory $lockFactory = new LockFactory(
Expand Down Expand Up @@ -59,7 +59,7 @@ public function getStatus(string $index): IndexerStatus
return $this->statusStore->load($index);
}

private function getIndexer(string $index): SolrIndexer
private function getIndexer(string $index): InternalResourceIndexer
{
$progressHandler = new BackgroundIndexerProgressState(
$index,
Expand Down
4 changes: 3 additions & 1 deletion src/Service/Indexer/IndexDocumentDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function __construct(

/**
* @param string[] $paths
* @return array<int,array<string,mixed>>.
* @return array<int,array<string,mixed>>
* Returns the raw array data of the documents to be able to
* output them as JSON, for example.
*/
public function dump(array $paths): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Atoolo\Search\Dto\Indexer\IndexerParameter;
use Atoolo\Search\Dto\Indexer\IndexerStatus;
use Atoolo\Search\Indexer;
use Atoolo\Search\Service\SolrClientFactory;
use Exception;
use Solarium\QueryType\Update\Result as UpdateResult;
use Throwable;
Expand All @@ -35,7 +34,7 @@
* but always a list of documents. The entire list is divided into chunks
* and indexed chunk-wise.
*/
class SolrIndexer implements Indexer
class InternalResourceIndexer implements Indexer
{
/**
* @param iterable<DocumentEnricher<IndexDocument>> $documentEnricherList
Expand All @@ -46,7 +45,7 @@ public function __construct(
private readonly LocationFinder $finder,
private readonly ResourceLoader $resourceLoader,
private readonly TranslationSplitter $translationSplitter,
private readonly SolrClientFactory $clientFactory,
private readonly SolrIndexService $indexService,
private readonly IndexingAborter $aborter,
private readonly string $source
) {
Expand All @@ -61,8 +60,8 @@ public function remove(string $index, array $idList): void
return;
}

$this->deleteByIdList($index, $idList);
$this->commit($index);
$this->indexService->deleteByIdList($index, $this->source, $idList);
$this->indexService->commit($index);
}

public function abort(string $index): void
Expand Down Expand Up @@ -137,7 +136,7 @@ private function indexResources(
$this->indexerProgressHandler->startUpdate($total);
}

$availableIndexes = $this->getAvailableIndexes();
$availableIndexes = $this->indexService->getAvailableIndexes();
$splitterResult = $this->translationSplitter->split($pathList);

$this->indexTranslationSplittedResources(
Expand Down Expand Up @@ -232,9 +231,13 @@ private function indexResourcesPerLanguageIndex(
$parameter->cleanupThreshold > 0 &&
$successCount >= $parameter->cleanupThreshold
) {
$this->deleteByProcessId($index, $processId);
$this->indexService->deleteExcludingProcessId(
$index,
$this->source,
$processId
);
}
$this->commit($index);
$this->indexService->commit($index);
}

/**
Expand Down Expand Up @@ -322,12 +325,9 @@ private function add(
string $processId,
array $resources
): UpdateResult {
$client = $this->clientFactory->create($solrCore);

$update = $client->createUpdate();
$update->setDocumentClass(IndexSchema2xDocument::class);
$updater = $this->indexService->updater($solrCore);

$documents = [];
foreach ($resources as $resource) {
foreach ($this->documentEnricherList as $enricher) {
if (!$enricher->isIndexable($resource)) {
Expand All @@ -337,89 +337,29 @@ private function add(
}
try {
/** @var IndexSchema2xDocument $doc */
$doc = $update->createDocument();
$doc = $updater->createDocument();
foreach ($this->documentEnricherList as $enricher) {
$doc = $enricher->enrichDocument(
$resource,
$doc,
$processId
);
}
$documents[] = $doc;
$updater->addDocument($doc);

Check failure on line 348 in src/Service/Indexer/InternalResourceIndexer.php

View workflow job for this annotation

GitHub Actions / verify / verify

Parameter #1 $document of method Atoolo\Search\Service\Indexer\SolrIndexUpdater::addDocument() expects Solarium\QueryType\Update\Query\Document, Atoolo\Search\Service\Indexer\IndexDocument given.
} catch (Throwable $e) {
$this->indexerProgressHandler->error($e);
}
}
// add the documents and a commit command to the update query
$update->addDocuments($documents);

// this executes the query and returns the result
return $client->update($update);
}

private function deleteByProcessId(string $core, string $processId): void
{
$this->deleteByQuery(
$core,
'-crawl_process_id:' . $processId . ' AND ' .
' sp_source:' . $this->source
);
}

/**
* @param string[] $idList
*/
private function deleteByIdList(string $core, array $idList): void
{
$this->deleteByQuery(
$core,
'sp_id:(' . implode(' ', $idList) . ') AND ' .
'sp_source:' . $this->source
);
return $updater->update();
}

private function deleteErrorProtocol(string $core): void
{
$this->deleteByQuery(
$this->indexService->deleteByQuery(
$core,
'crawl_status:error OR crawl_status:warning'
);
}

private function deleteByQuery(string $core, string $query): void
{
$client = $this->clientFactory->create($core);
$update = $client->createUpdate();
$update->addDeleteQuery($query);
$client->update($update);
}

private function commit(string $core): void
{
$client = $this->clientFactory->create($core);
$update = $client->createUpdate();
$update->addCommit();
$update->addOptimize();
$client->update($update);
}

/**
* @return string[]
*/
private function getAvailableIndexes(): array
{
$client = $this->clientFactory->create('');
$coreAdminQuery = $client->createCoreAdmin();
$statusAction = $coreAdminQuery->createStatus();
$coreAdminQuery->setAction($statusAction);

$availableIndexes = [];
$response = $client->coreAdmin($coreAdminQuery);
$statusResults = $response->getStatusResults() ?? [];
foreach ($statusResults as $statusResult) {
$availableIndexes[] = $statusResult->getCoreName();
}

return $availableIndexes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Atoolo\Search\Service\Indexer;

use Atoolo\Resource\ResourceLoader;
use Atoolo\Search\Service\SolrClientFactory;

class SolrIndexerFactory
class InternalResourceIndexerFactory
{
/**
* @param iterable<DocumentEnricher<IndexDocument>> $documentEnricherList
Expand All @@ -17,22 +16,22 @@ public function __construct(
private readonly LocationFinder $finder,
private readonly ResourceLoader $resourceLoader,
private readonly TranslationSplitter $translationSplitter,
private readonly SolrClientFactory $clientFactory,
private readonly SolrIndexService $solrService,
private readonly IndexingAborter $aborter,
private readonly string $source
) {
}

public function create(
IndexerProgressHandler $progressHandler
): SolrIndexer {
return new SolrIndexer(
): InternalResourceIndexer {
return new InternalResourceIndexer(
$this->documentEnricherList,
$progressHandler,
$this->finder,
$this->resourceLoader,
$this->translationSplitter,
$this->clientFactory,
$this->solrService,
$this->aborter,
$this->source
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ private function idWithoutSignature(string $id): int
* - https://gitlab.sitepark.com/customer-projects/stadtundland/blob/develop/stadtundland-module/src/publish/php/SP/Stadtundland/Component/PurchaseExpose.php#L38
* - https://gitlab.sitepark.com/ies-modules/citycall/blob/develop/citycall-module/src/main/php/src/SP/CityCall/Component/Intro.php#L51
* - https://gitlab.sitepark.com/ies-modules/sitekit-real-estate/blob/develop/src/publish/php/SP/RealEstate/Component/Expose.php#L47
* - https://gitlab.sitepark.com/sitekit/xzufi-php/-/blob/develop/php/SP/Xzufi/Renderer/SolrData.php?ref_type=heads#L78
*/

private function getLocaleFromResource(Resource $resource): string
Expand Down
Loading

0 comments on commit 299485a

Please sign in to comment.