Last active
September 11, 2017 09:56
-
-
Save wachterjohannes/320019bb1ffe4326ecf7ae96338be5d7 to your computer and use it in GitHub Desktop.
Index pages in ONGR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Command; | |
use Sulu\Bundle\ContentBundle\Document\BasePageDocument; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
/** | |
* Command to index all pages. | |
*/ | |
class PageIndexCommand extends ContainerAwareCommand | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function configure() | |
{ | |
$this->setName('app:index:pages'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$documentManager = $this->getContainer()->get('sulu_document_manager.document_manager'); | |
/** @var BasePageDocument[] $documents */ | |
$documents = $documentManager->createQuery( | |
'SELECT * FROM [nt:unstructured] WHERE [jcr:mixinTypes] = "sulu:page" OR [jcr:mixinTypes] = "sulu:home"' | |
)->execute(); | |
$pageIndexer = $this->getContainer()->get('app.page_indexer'); | |
$progressBar = new ProgressBar($output, count($documents)); | |
$progressBar->setFormat( | |
'%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s% %message%' | |
); | |
$progressBar->start(); | |
foreach ($documents as $document) { | |
$document = $documentManager->find($document->getUuid(), 'de'); | |
$pageIndexer->index($document); | |
$progressBar->advance(); | |
} | |
$pageIndexer->commit(); | |
$progressBar->finish(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Document\Indexer; | |
use AppBundle\Document\PageViewDocument; | |
use ONGR\ElasticsearchBundle\Service\Manager; | |
use Sulu\Bundle\ContentBundle\Document\BasePageDocument; | |
use Sulu\Bundle\ContentBundle\Document\PageDocument; | |
/** | |
* Index page-documents. | |
*/ | |
class PageIndexer | |
{ | |
/** | |
* @var Manager | |
*/ | |
private $searchManager; | |
/** | |
* @param Manager $searchManager | |
*/ | |
public function __construct(Manager $searchManager) | |
{ | |
$this->searchManager = $searchManager; | |
} | |
/** | |
* Index page-document. | |
* | |
* @param BasePageDocument $document | |
*/ | |
public function index(BasePageDocument $document) | |
{ | |
if ($document->getRedirectType() === RedirectType::INTERNAL) { | |
return $this->deindex($document->getUuid()); | |
} | |
$this->searchManager->persist($this->createViewDocument($document)); | |
} | |
/** | |
* Deindex page-document. | |
* | |
* @param string $uuid | |
*/ | |
public function deindex($uuid) | |
{ | |
$viewDocument = $this->searchManager->find('AppBundle:PageViewDocument', $uuid); | |
if (null !== $viewDocument) { | |
$this->searchManager->remove($viewDocument); | |
} | |
} | |
/** | |
* Creates a view-document for given page-document. | |
* | |
* @param BasePageDocument $document | |
* | |
* @return PageViewDocument | |
*/ | |
private function createViewDocument(BasePageDocument $document) | |
{ | |
$viewDocument = new PageViewDocument(); | |
$viewDocument->title = $document->getTitle(); | |
$viewDocument->uuid = $document->getUuid(); | |
$viewDocument->url = $document->getResourceSegment(); | |
$viewDocument->structureType = $document->getStructureType(); | |
$viewDocument->type = $document instanceof PageDocument ? 'page' : 'home'; | |
return $viewDocument; | |
} | |
/** | |
* Commit index. | |
*/ | |
public function commit() | |
{ | |
$this->searchManager->commit(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Document\Subscriber; | |
use AppBundle\Document\Indexer\BasePageDocumentIndexer; | |
use Sulu\Bundle\ContentBundle\Document\BasePageDocument; | |
use Sulu\Component\DocumentManager\Event\PublishEvent; | |
use Sulu\Component\DocumentManager\Event\RemoveEvent; | |
use Sulu\Component\DocumentManager\Event\UnpublishEvent; | |
use Sulu\Component\DocumentManager\Events; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
/** | |
* Indexes and deindexes pages. | |
*/ | |
class PageSubscriber implements EventSubscriberInterface | |
{ | |
/** | |
* @var BasePageDocumentIndexer | |
*/ | |
private $indexer; | |
/** | |
* @param BasePageDocumentIndexer $indexer | |
*/ | |
public function __construct(BasePageDocumentIndexer $indexer) | |
{ | |
$this->indexer = $indexer; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
Events::PUBLISH => ['indexPublishedDocument', -256], | |
Events::REMOVE => ['deindexRemovedDocument', 600], | |
Events::UNPUBLISH => ['deindexUnpublishedDocument', -1024], | |
]; | |
} | |
/** | |
* Indexes a published document. | |
* | |
* @param PublishEvent $event | |
*/ | |
public function indexPublishedDocument(PublishEvent $event) | |
{ | |
$document = $event->getDocument(); | |
if (!$document instanceof BasePageDocument) { | |
return; | |
} | |
$this->indexer->index($document); | |
$this->indexer->commit(); | |
} | |
/** | |
* Schedules a document to be deindexed. | |
* | |
* @param RemoveEvent $event | |
*/ | |
public function deindexRemovedDocument(RemoveEvent $event) | |
{ | |
$document = $event->getDocument(); | |
if (!$document instanceof BasePageDocument) { | |
return; | |
} | |
$this->indexer->deindex($document->getUuid()); | |
$this->indexer->commit(); | |
} | |
/** | |
* Deindexes the document from the search index for the website. | |
* | |
* @param UnpublishEvent $event | |
*/ | |
public function deindexUnpublishedDocument(UnpublishEvent $event) | |
{ | |
$document = $event->getDocument(); | |
if (!$document instanceof BasePageDocument) { | |
return; | |
} | |
$this->indexer->deindex($document->getUuid()); | |
$this->indexer->commit(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Document; | |
use ONGR\ElasticsearchBundle\Annotation\Document; | |
use ONGR\ElasticsearchBundle\Annotation\Embedded; | |
use ONGR\ElasticsearchBundle\Annotation\Id; | |
use ONGR\ElasticsearchBundle\Annotation\Property; | |
use ONGR\ElasticsearchBundle\Collection\Collection; | |
/** | |
* @Document(type="app_page") | |
*/ | |
class PageViewDocument | |
{ | |
/** | |
* @var string | |
* | |
* @Id | |
*/ | |
public $uuid; | |
/** | |
* @var string | |
* | |
* @Property( | |
* type="string", | |
* options={ | |
* "fields":{ | |
* "raw":{"type":"string", "index":"not_analyzed"}, | |
* "value":{"type":"string"} | |
* } | |
* } | |
* ) | |
*/ | |
public $url; | |
/** | |
* @var string | |
* | |
* @Property( | |
* type="string", | |
* options={ | |
* "fields":{ | |
* "raw":{"type":"string", "index":"not_analyzed"}, | |
* "folded":{"type":"string", "analyzer":"folding"}, | |
* "value":{"type":"string"} | |
* } | |
* } | |
* ) | |
*/ | |
public $title; | |
/** | |
* @var string | |
* | |
* @Property( | |
* type="string", | |
* options={ | |
* "analyzer":"keyword" | |
* } | |
* ) | |
*/ | |
public $type; | |
/** | |
* @var string | |
* | |
* @Property( | |
* type="string", | |
* options={ | |
* "analyzer":"keyword" | |
* } | |
* ) | |
*/ | |
public $structureType; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<services> | |
<service id="app.page_subscriber" class="AppBundle\Document\Subscriber\PageSubscriber"> | |
<argument type="service" id="app.page_indexer"/> | |
<tag name="sulu_document_manager.event_subscriber"/> | |
</service> | |
<service id="app.page_indexer" class="AppBundle\Document\Indexer\PageIndexer"> | |
<argument type="service" id="es.manager.live"/> | |
</service> | |
</services> | |
</container> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment