Skip to content

Instantly share code, notes, and snippets.

@christophlehmann
Last active December 26, 2022 20:22
Show Gist options
  • Save christophlehmann/d889fd9fdc86d738a648c858c89d5643 to your computer and use it in GitHub Desktop.
Save christophlehmann/d889fd9fdc86d738a648c858c89d5643 to your computer and use it in GitHub Desktop.
TYPO3 Solr: Search across multiple languages (language per core = Do not loose features like stemming)

TYPO3: A multi-language search with EXT:solr

Solr has a feature called sharding for doing search requests across multiple cores (core per language).

Pro:

  • Use language individual stemming

Contra:

  • Variants do not work across shards

TypoScript configuration

# de language = 0
# en language = 1
plugin.tx_solr.search.query.shard.languages = 0,1

This adds the shard parameter to the solr request:

http://localhost:8983/solr/core_de/select?shards=localhost:8983/solr/core_de,localhost:8983/solr/core_en


It was developed in times of TYPO3 10.4

\ApacheSolrForTypo3\Solr\Search\SearchComponentManager::registerSearchComponent(
'shard',
\My\Site\Search\ShardComponent::class
);
<?php declare(strict_types=1);
namespace My\Site\Query\Modifier;
use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
use ApacheSolrForTypo3\Solr\Query\Modifier\Modifier;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Adds shard query parameter
*
* http://localhost:8983/solr/core_de/select?shards=localhost:8983/solr/core_de,localhost:8983/solr/core_en
*/
class Shard implements Modifier
{
/**
* @var QueryBuilder
*/
protected $queryBuilder;
/**
* @var ConnectionManager
*/
protected $connectionManager;
/**
* @var TypoScriptConfiguration
*/
protected $typoScriptConfiguration = null;
public function __construct(QueryBuilder $builder = null)
{
$this->queryBuilder = $builder ?? GeneralUtility::makeInstance(QueryBuilder::class);
$this->typoScriptConfiguration = Util::getSolrConfiguration();
$this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
}
public function modifyQuery(Query $query): Query
{
$languages = GeneralUtility::trimExplode(',',
$this->typoScriptConfiguration->getValueByPath('plugin.tx_solr.search.query.shard.languages') ?? '');
if (count($languages) > 0) {
$shards = [];
foreach ($languages as $language) {
$connection = $this->connectionManager->getConnectionByPageId($GLOBALS['TSFE']->id, (integer)$language);
$node = $connection->getNode('read');
$shards[] = rtrim($node->__toString(), '/');
}
$query->addParam('shards', implode(',', $shards));
}
return $query;
}
}
<?php declare(strict_types=1);
namespace My\Site\Search;
use My\Site\Query\Modifier\Shard;
use ApacheSolrForTypo3\Solr\Search\AbstractComponent;
class ShardComponent extends AbstractComponent
{
public function initializeSearchComponent()
{
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['modifySearchQuery']['tx_mysite_shard'] = Shard::class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment