.. _extending_solr: Extending Solr indexation ------------------------- How to index page blocks contents ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If all your text content is written in *block nodes* instead of reachable *pages*, you should index them into your page Solr documents to improve your search engine relevancy. You can use the ``NodesSourcesIndexingEvent::class`` event to enhance your node indexing data before it’s persisted into *Solr* engine (especially ``collection_txt`` field): .. code-block:: php walkerContext = $walkerContext; $this->solariumFactory = $solariumFactory; $this->maxLevel = $maxLevel; } /** * @inheritDoc */ public static function getSubscribedEvents(): array { return [ NodesSourcesIndexingEvent::class => ['onIndexing'], ]; } public function onIndexing(NodesSourcesIndexingEvent $event): void { $nodeSource = $event->getNodeSource(); if (null !== $nodeSource->getNode() && $nodeSource->isReachable() && !$event->isSubResource()) { $assoc = $event->getAssociations(); $blockWalker = AutoChildrenNodeSourceWalker::build( $nodeSource, $this->walkerContext, $this->maxLevel ); // Need a locale field $locale = $nodeSource->getTranslation()->getLocale(); $lang = \Locale::getPrimaryLanguage($locale) ?? 'fr'; foreach ($blockWalker->getChildren() as $subWalker) { $this->walkAndIndex($subWalker, $assoc, $lang); } $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($walker->getItem()); // Fetch all fields array association AS sub-resources (i.e. do not index their title) $childAssoc = $solarium->getFieldsAssoc(true); $assoc['collection_txt'] = array_filter(array_merge( $assoc['collection_txt'], $childAssoc['collection_txt'] )); if (!empty($childAssoc['collection_txt_' . $locale])) { $assoc['collection_txt_' . $locale] .= PHP_EOL . $childAssoc['collection_txt_' . $locale]; } } if ($walker->count() > 0) { foreach ($walker->getChildren() as $subWalker) { $this->walkAndIndex($subWalker, $assoc, $locale); } } } }