Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-28176: Support for parallel and selective indexing command #113

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": "~5.6|~7.0",
"ezsystems/ezpublish-kernel": "~6.7.6@dev|^6.11.4@dev|^7.0@dev",
"ezsystems/ezpublish-kernel": "~6.7.7@dev|^6.12.1@dev|^7.0@dev",
"netgen/query-translator": "^1.0"
},
"require-dev": {
Expand Down
103 changes: 41 additions & 62 deletions lib/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,83 +9,62 @@
namespace EzSystems\EzPlatformSolrSearchEngine;

use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use eZ\Publish\Core\Search\Common\Indexer as SearchIndexer;
use EzSystems\EzPlatformSolrSearchEngine\Handler as SearchHandler;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use PDO;
use RuntimeException;
use eZ\Publish\Core\Persistence\Database\DatabaseHandler;
use eZ\Publish\Core\Search\Common\IncrementalIndexer;
use EzSystems\EzPlatformSolrSearchEngine\Handler as SolrSearchHandler;
use eZ\Publish\SPI\Persistence\Handler as PersistenceHandler;
use Psr\Log\LoggerInterface;

class Indexer extends SearchIndexer
class Indexer extends IncrementalIndexer
{
/**
* @var \EzSystems\EzPlatformSolrSearchEngine\Handler
*/
protected $searchHandler;

/**
* Create search engine index.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param int $iterationCount
* @param bool $commit
*/
public function createSearchIndex(OutputInterface $output, $iterationCount, $commit)
{
$output->writeln('Creating Solr Search Engine Index...');

if (!$this->searchHandler instanceof SearchHandler) {
throw new RuntimeException(sprintf('Expected to find an instance of %s, but found %s', SearchHandler::class, get_class($this->searchHandler)));
}
public function __construct(
LoggerInterface $logger,
PersistenceHandler $persistenceHandler,
DatabaseHandler $databaseHandler,
SolrSearchHandler $searchHandler
) {
parent::__construct($logger, $persistenceHandler, $databaseHandler, $searchHandler);
}

$stmt = $this->getContentDbFieldsStmt(['count(id)']);
$totalCount = (int) $stmt->fetchColumn();
$stmt = $this->getContentDbFieldsStmt(['id', 'current_version']);
public function getName()
{
return 'eZ Platform Solr Search Engine';
}

public function purge()
{
$this->searchHandler->purgeIndex();
}

$progress = new ProgressBar($output);
$progress->start($totalCount);

$i = 0;
do {
$contentObjects = [];
for ($k = 0; $k <= $iterationCount; ++$k) {
if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
break;
}
try {
$contentObjects[] = $this->persistenceHandler->contentHandler()->load(
$row['id'],
$row['current_version']
);
} catch (NotFoundException $e) {
$this->logWarning($progress, "Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage());
}
}

$documents = [];
foreach ($contentObjects as $content) {
try {
public function updateSearchIndex(array $contentIds, $commit)
{
$documents = [];
$contentHandler = $this->persistenceHandler->contentHandler();
foreach ($contentIds as $contentId) {
try {
$info = $contentHandler->loadContentInfo($contentId);
if ($info->isPublished) {
$content = $contentHandler->load($contentId, $info->currentVersionNo);
$documents[] = $this->searchHandler->generateDocument($content);
} catch (NotFoundException $e) {
// Ignore content objects that have some sort of missing data on it
$this->logWarning($progress, 'Content with id ' . $content->versionInfo->id . ' has missing data, so skipped for indexing. Full exception: ' . $e->getMessage());
} else {
$this->searchHandler->deleteContent($contentId);
}
} catch (NotFoundException $e) {
$this->searchHandler->deleteContent($contentId);
}
if (!empty($documents)) {
$this->searchHandler->bulkIndexDocuments($documents);
}

if ($commit) {
$this->searchHandler->commit();
}
}

$progress->advance($k);
} while (($i += $iterationCount) < $totalCount);
$progress->finish();
$output->writeln('');
if (!empty($documents)) {
$this->searchHandler->bulkIndexDocuments($documents);
}

$output->writeln('Finished creating Solr Search Engine Index');
if ($commit) {
$this->searchHandler->commit(true);
}
}
}