Skip to content

Commit dc02ae5

Browse files
alongoszandrerom
authored andcommitted
Update Solr search handler to implement upcoming changes in eZ\Publish\SPI\Search\Handler interface (#49)
* EZP-25088: Modified Solr search engine handler to implement upcoming changes in the eZ\Publish\SPI\Search\Handler interface * EZP-25088: Modified ezplatform:solr_create_index command to use new version of search engine Handler
1 parent 342d224 commit dc02ae5

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

bundle/Command/SolrCreateIndexCommand.php

+37-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace EzSystems\EzPlatformSolrSearchEngineBundle\Command;
1212

1313
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
14+
use Symfony\Component\Console\Helper\ProgressHelper;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use Symfony\Component\Console\Input\InputArgument;
@@ -37,6 +38,8 @@ protected function configure()
3738

3839
protected function execute(InputInterface $input, OutputInterface $output)
3940
{
41+
$this->logger = $this->getContainer()->get('logger');
42+
4043
$bulkCount = $input->getArgument('bulk_count');
4144

4245
/** @var \eZ\Publish\SPI\Search\Handler $searchHandler */
@@ -79,8 +82,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
7982
$progress = $this->getHelperSet()->get('progress');
8083
$progress->start($output, $totalCount);
8184
$i = 0;
82-
$logger = $this->getContainer()->get('logger');
83-
/* @var \Psr\Log\LoggerInterface $logger */
8485
do {
8586
$contentObjects = array();
8687

@@ -94,15 +95,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
9495
$row['current_version']
9596
);
9697
} catch (NotFoundException $e) {
97-
$progress->clear();
98-
$output->write("\r"); // get rid of padding (side effect of displaying progress bar)
99-
$logger->warning("Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage());
100-
$progress->display();
98+
$this->logWarning($output, $progress, "Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage());
10199
}
102100
}
103101

104-
if (!empty($contentObjects)) {
105-
$searchHandler->bulkIndexContent($contentObjects);
102+
$documents = [];
103+
104+
foreach ($contentObjects as $content) {
105+
try {
106+
$documents[] = $searchHandler->generateDocument($content);
107+
} catch (NotFoundException $e) {
108+
// Ignore content objects that have some sort of missing data on it
109+
$this->logWarning($output, $progress, 'Content with id ' . $content->versionInfo->id . ' has missing data, so skipped for indexing. Full exception: ' . $e->getMessage());
110+
}
111+
}
112+
113+
if (!empty($documents)) {
114+
$searchHandler->bulkIndexDocuments($documents);
106115
}
107116

108117
$progress->advance($k);
@@ -113,4 +122,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
113122

114123
$progress->finish();
115124
}
125+
126+
/**
127+
* Log warning while progress helper is running.
128+
*
129+
* @param \Symfony\Component\Console\Output\OutputInterface $output
130+
* @param \Symfony\Component\Console\Helper\ProgressHelper $progress
131+
* @param $message
132+
*/
133+
private function logWarning(OutputInterface $output, ProgressHelper $progress, $message)
134+
{
135+
$progress->clear();
136+
$output->write("\r"); // get rid of padding (side effect of displaying progress bar)
137+
$this->logger->warning($message);
138+
$progress->display();
139+
}
140+
141+
/**
142+
* @var \Psr\Log\LoggerInterface
143+
*/
144+
private $logger;
116145
}

lib/Handler.php

+26
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,30 @@ protected function allItemsWithinLocationWithAdditionalLocation($locationId)
412412
"/@&~(.*\\/{$locationId}\\/.*)/"
413413
);
414414
}
415+
416+
/**
417+
* Generate search document for Content object to be indexed by a search engine.
418+
*
419+
* @param \eZ\Publish\SPI\Persistence\Content $content
420+
* @return \eZ\Publish\SPI\Search\Document
421+
*/
422+
public function generateDocument(Content $content)
423+
{
424+
return $this->mapper->mapContentBlock($content);
425+
}
426+
427+
/**
428+
* Index search documents generated by generateDocument method.
429+
*
430+
* Notes:
431+
* - Does not force a commit on solr, depends on solr config, use {@see commit()} if you need that.
432+
* - On large amounts of data make sure to iterate with several calls to this function with a limited
433+
* set of content objects, amount you have memory for depends on server, size of objects, & PHP version.
434+
*
435+
* @param \eZ\Publish\SPI\Search\Document[] $documents
436+
*/
437+
public function bulkIndexDocuments(array $documents)
438+
{
439+
$this->gateway->bulkIndexDocuments($documents);
440+
}
415441
}

0 commit comments

Comments
 (0)