-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Solr): Added
TreeWalkerIndexingEventSubscriber
to index node c…
…hildren using a TreeWalker, improved `AttributeValueIndexingSubscriber`
- Loading branch information
1 parent
13041a2
commit fea4ea0
Showing
6 changed files
with
179 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
100 changes: 100 additions & 0 deletions
100
lib/RoadizCoreBundle/src/SearchEngine/Subscriber/TreeWalkerIndexingEventSubscriber.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\SearchEngine\Subscriber; | ||
|
||
use RZ\Roadiz\CoreBundle\Api\TreeWalker\AutoChildrenNodeSourceWalker; | ||
use RZ\Roadiz\CoreBundle\Entity\NodesSources; | ||
use RZ\Roadiz\CoreBundle\Event\NodesSources\NodesSourcesIndexingEvent; | ||
use RZ\Roadiz\CoreBundle\SearchEngine\SolariumFactoryInterface; | ||
use RZ\TreeWalker\WalkerContextInterface; | ||
use RZ\TreeWalker\WalkerInterface; | ||
|
||
/** | ||
* Index sub nodes content into any reachable node-source using AutoChildrenNodeSourceWalker. | ||
*/ | ||
final class TreeWalkerIndexingEventSubscriber extends AbstractIndexingSubscriber | ||
{ | ||
private WalkerContextInterface $walkerContext; | ||
private SolariumFactoryInterface $solariumFactory; | ||
private int $maxLevel; | ||
private string $defaultLocale; | ||
|
||
public function __construct( | ||
WalkerContextInterface $walkerContext, | ||
SolariumFactoryInterface $solariumFactory, | ||
int $maxLevel = 5, | ||
string $defaultLocale = 'en' | ||
) { | ||
$this->walkerContext = $walkerContext; | ||
$this->solariumFactory = $solariumFactory; | ||
$this->maxLevel = $maxLevel; | ||
$this->defaultLocale = $defaultLocale; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
NodesSourcesIndexingEvent::class => ['onIndexing', -99], | ||
]; | ||
} | ||
|
||
public function onIndexing(NodesSourcesIndexingEvent $event): void | ||
{ | ||
$nodeSource = $event->getNodeSource(); | ||
if (!$nodeSource->isReachable() || $event->isSubResource()) { | ||
return; | ||
} | ||
|
||
$assoc = $event->getAssociations(); | ||
|
||
$blockWalker = AutoChildrenNodeSourceWalker::build( | ||
$nodeSource, | ||
$this->walkerContext, | ||
$this->maxLevel | ||
); | ||
|
||
// Need a locale field | ||
$locale = $nodeSource->getTranslation()->getLocale(); | ||
$lang = \Locale::getPrimaryLanguage($locale) ?? $this->defaultLocale; | ||
|
||
try { | ||
foreach ($blockWalker->getChildren() as $subWalker) { | ||
$this->walkAndIndex($subWalker, $assoc, $lang); | ||
} | ||
} catch (\Exception $e) { | ||
} | ||
|
||
$event->setAssociations($assoc); | ||
} | ||
|
||
/** | ||
* @param WalkerInterface $walker | ||
* @param array $assoc | ||
* @param string $locale | ||
* @throws \Exception | ||
*/ | ||
protected function walkAndIndex(WalkerInterface $walker, array &$assoc, string $locale): void | ||
{ | ||
$item = $walker->getItem(); | ||
if ($item instanceof NodesSources) { | ||
$solarium = $this->solariumFactory->createWithNodesSources($item); | ||
// Fetch all fields array association AS sub-resources (i.e. do not index their title, and relationships) | ||
$childAssoc = $solarium->getFieldsAssoc(true); | ||
$assoc['collection_txt'] = array_filter(array_merge( | ||
$assoc['collection_txt'], | ||
$childAssoc['collection_txt'] | ||
)); | ||
$assoc['collection_txt_' . $locale] = $this->flattenTextCollection($assoc['collection_txt']); | ||
} | ||
if ($walker->count() > 0) { | ||
foreach ($walker->getChildren() as $subWalker) { | ||
$this->walkAndIndex($subWalker, $assoc, $locale); | ||
} | ||
} | ||
} | ||
} |