Skip to content

Commit

Permalink
Integrate massive search (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath committed Aug 26, 2020
1 parent ffd7bb8 commit 07da23b
Show file tree
Hide file tree
Showing 26 changed files with 1,869 additions and 38 deletions.
142 changes: 142 additions & 0 deletions Content/Application/ContentIndexer/ContentIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentIndexer;

use Massive\Bundle\SearchBundle\Search\QueryHit;
use Massive\Bundle\SearchBundle\Search\SearchManager;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;

class ContentIndexer implements ContentIndexerInterface
{
/**
* @var SearchManager
*/
private $searchManager;

/**
* @var ContentResolverInterface
*/
private $contentResolver;

/**
* @param SearchManager $searchManager
*/
public function __construct(SearchManagerInterface $searchManager, ContentResolverInterface $contentResolver)
{
$this->searchManager = $searchManager;
$this->contentResolver = $contentResolver;
}

public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface
{
$dimensionContent = $this->loadDimensionContent($contentRichEntity, $dimensionAttributes);

$this->indexDimensionContent($dimensionContent);

return $dimensionContent;
}

public function indexDimensionContent(DimensionContentInterface $dimensionContent): void
{
if (!$dimensionContent->isMerged()) {
return;
}

$this->searchManager->index($dimensionContent);
}

public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void
{
$locale = $dimensionAttributes['locale'] ?? null;
$stage = $dimensionAttributes['stage'] ?? null;

$search = $this->searchManager->createSearch(sprintf('__id:"%s"', $id))
->indexes($this->getIndexes($resourceKey, $stage));

if ($locale) {
$search->locale($locale);
}

$searchResult = $search->execute();

/** @var QueryHit $hit */
foreach ($searchResult as $hit) {
$document = $hit->getDocument();
$this->searchManager->deindex($document, $document->getLocale());
}
}

public function deindexDimensionContent(DimensionContentInterface $dimensionContent): void
{
if (!$dimensionContent->isMerged()) {
return;
}

$this->searchManager->deindex($dimensionContent, $dimensionContent->getDimension()->getLocale());
}

/**
* @param mixed[] $dimensionAttributes
*/
private function loadDimensionContent(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentInterface {
$locale = $dimensionAttributes['locale'] ?? null;
$stage = $dimensionAttributes['stage'] ?? null;

if (null === $locale || null === $stage) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
}

$dimensionContent = $this->contentResolver->resolve($contentRichEntity, $dimensionAttributes);

if ($locale !== $dimensionContent->getDimension()->getLocale()
|| $stage !== $dimensionContent->getDimension()->getStage()) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
}

return $dimensionContent;
}

/**
* @return string[]
*/
private function getIndexes(string $resourceKey, ?string $stage): array
{
return array_filter(
$this->searchManager->getIndexNames(),
function ($indexName) use ($resourceKey, $stage) {
if (null === $stage) {
return $resourceKey === $indexName || $resourceKey . '_published' === $indexName;
}

if (DimensionInterface::STAGE_DRAFT === $stage) {
return $resourceKey === $indexName;
}

if (DimensionInterface::STAGE_LIVE === $stage) {
return $resourceKey . '_published' === $indexName;
}

return false;
}
);
}
}
35 changes: 35 additions & 0 deletions Content/Application/ContentIndexer/ContentIndexerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentIndexer;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

interface ContentIndexerInterface
{
/**
* @param mixed[] $dimensionAttributes
*/
public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface;

public function indexDimensionContent(DimensionContentInterface $dimensionContent): void;

/**
* @param mixed $id
* @param mixed[] $dimensionAttributes
*/
public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void;

public function deindexDimensionContent(DimensionContentInterface $dimensionContent): void;
}
20 changes: 19 additions & 1 deletion Content/Application/ContentManager/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentManager;

use Sulu\Bundle\ContentBundle\Content\Application\ContentCopier\ContentCopierInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentIndexer\ContentIndexerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentNormalizer\ContentNormalizerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentPersister\ContentPersisterInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
Expand Down Expand Up @@ -48,18 +49,25 @@ class ContentManager implements ContentManagerInterface
*/
private $contentWorkflow;

/**
* @var ContentIndexerInterface
*/
private $contentIndexer;

public function __construct(
ContentResolverInterface $contentResolver,
ContentPersisterInterface $contentPersister,
ContentNormalizerInterface $contentNormalizer,
ContentCopierInterface $contentCopier,
ContentWorkflowInterface $contentWorkflow
ContentWorkflowInterface $contentWorkflow,
ContentIndexerInterface $contentIndexer
) {
$this->contentResolver = $contentResolver;
$this->contentPersister = $contentPersister;
$this->contentNormalizer = $contentNormalizer;
$this->contentCopier = $contentCopier;
$this->contentWorkflow = $contentWorkflow;
$this->contentIndexer = $contentIndexer;
}

public function resolve(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface
Expand Down Expand Up @@ -98,4 +106,14 @@ public function applyTransition(
): DimensionContentInterface {
return $this->contentWorkflow->apply($contentRichEntity, $dimensionAttributes, $transitionName);
}

public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface
{
return $this->contentIndexer->index($contentRichEntity, $dimensionAttributes);
}

public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void
{
$this->contentIndexer->deindex($resourceKey, $id, $dimensionAttributes);
}
}
11 changes: 11 additions & 0 deletions Content/Application/ContentManager/ContentManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ public function applyTransition(
array $dimensionAttributes,
string $transitionName
): DimensionContentInterface;

/**
* @param mixed[] $dimensionAttributes
*/
public function index(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface;

/**
* @param mixed $id
* @param mixed[] $dimensionAttributes
*/
public function deindex(string $resourceKey, $id, array $dimensionAttributes = []): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ public function onPublish(TransitionEvent $transitionEvent): void

public static function getSubscribedEvents(): array
{
$eventName = 'workflow.content_workflow.transition.' . WorkflowInterface::WORKFLOW_TRANSITION_PUBLISH;

return [
'workflow.content_workflow.transition.publish' => 'onPublish',
$eventName => 'onPublish',
];
}
}
Loading

0 comments on commit 07da23b

Please sign in to comment.