Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NodeSearchBundle][SearchBundle] Multi domain/language search population fix #1635

Merged
merged 4 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Kunstmaan\NodeSearchBundle\Event\IndexNodeEvent;
use Kunstmaan\NodeSearchBundle\Helper\IndexablePagePartsService;
use Kunstmaan\NodeSearchBundle\Helper\SearchBoostInterface;
use Kunstmaan\NodeSearchBundle\Helper\SearchViewTemplateInterface;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
use Kunstmaan\SearchBundle\Configuration\SearchConfigurationInterface;
Expand Down Expand Up @@ -126,6 +125,26 @@ public function setLogger(LoggerInterface $logger)
$this->logger = $logger;
}

/**
* @param $locale
* @return array
*/
public function checkAnalyzerLanguages()
{
$errors = [];
foreach ($this->locales as $locale) {
if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) {
$locale = strtolower($locale);
}

if ( false === array_key_exists($locale, $this->analyzerLanguages) ) {
$errors[] = $locale;
}
}

return $errors;
}

/**
* Create node index
*/
Expand All @@ -137,14 +156,23 @@ public function createIndex()
);

foreach ($this->locales as $locale) {
$localeAnalysis = clone($analysis);
$language = $this->analyzerLanguages[$locale]['analyzer'];
// Multilanguage check
if (preg_match('/[a-z]{2}_?+[a-zA-Z]{2}/', $locale)) {
$locale = strtolower($locale);
}

//build new index
// Build new index
$index = $this->searchProvider->createIndex($this->indexName . '_' . $locale);

//create index with analysis
$this->setAnalysis($index, $localeAnalysis->setupLanguage($language));
if (array_key_exists($locale, $this->analyzerLanguages)) {
$localeAnalysis = clone $analysis;
$language = $this->analyzerLanguages[$locale]['analyzer'];

// Create index with analysis
$this->setAnalysis($index, $localeAnalysis->setupLanguage($language));
} else {
$index->create();
}

$this->setMapping($index, $locale);
}
Expand Down Expand Up @@ -334,7 +362,7 @@ protected function createDefaultSearchFieldsMapping(Index $index, $lang = 'en')
* @param Index $index
* @param string $lang
*/
protected function setMapping(Index $index, $lang = 'en')
protected function setMapping(Index $index, $lang='en')
{
$mapping = $this->createDefaultSearchFieldsMapping($index, $lang);
$mapping->send();
Expand Down
16 changes: 16 additions & 0 deletions src/Kunstmaan/SearchBundle/Command/SetupIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
* Command to create the indexes
Expand All @@ -29,12 +30,27 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$searchConfigurationChain = $this->getContainer()->get('kunstmaan_search.search_configuration_chain');
/**
* @var string $alias
* @var SearchConfigurationInterface $searchConfiguration
*/
foreach ($searchConfigurationChain->getConfigurations() as $alias => $searchConfiguration) {

if (count($searchConfiguration->checkAnalyzerLanguages()) > 0) {
$question = new ChoiceQuestion(
sprintf('Languages analyzer is not available for: %s. Do you want continue?',
implode(', ', $searchConfiguration->checkAnalyzerLanguages())
),
['No', 'Yes']
);
$question->setErrorMessage('Answer %s is invalid.');
if ( $helper->ask($input, $output, $question) === 'No' ) {
return;
}
}

$searchConfiguration->createIndex();
$output->writeln('Index created : ' . $alias);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Kunstmaan/SearchBundle/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function getClient()
*/
public function createDocument($uid, $document, $indexName = '', $indexType = '')
{
$indexName = strtolower($indexName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing strtolower here?


return $this->getActiveProvider()->createDocument(
$uid,
$document,
Expand All @@ -90,6 +92,8 @@ public function createDocument($uid, $document, $indexName = '', $indexType = ''
*/
public function addDocument($uid, $document, $indexType, $indexName)
{
$indexName = strtolower($indexName);

return $this->getActiveProvider()->addDocument(
$this->indexNamePrefix . $indexName,
$indexType,
Expand All @@ -103,6 +107,8 @@ public function addDocument($uid, $document, $indexType, $indexName)
*/
public function addDocuments($documents, $indexName = '', $indexType = '')
{
$indexName = strtolower($indexName);

return $this->getActiveProvider()->addDocuments($documents, $indexName, $indexType);
}

Expand All @@ -111,6 +117,8 @@ public function addDocuments($documents, $indexName = '', $indexType = '')
*/
public function deleteDocument($indexName, $indexType, $uid)
{
$indexName = strtolower($indexName);

return $this->getActiveProvider()->deleteDocument($this->indexNamePrefix . $indexName, $indexType, $uid);
}

Expand All @@ -119,6 +127,8 @@ public function deleteDocument($indexName, $indexType, $uid)
*/
public function deleteDocuments($indexName, $indexType, array $ids)
{
$indexName = strtolower($indexName);

return $this->getActiveProvider()->deleteDocuments($this->indexNamePrefix . $indexName, $indexType, $ids);
}

Expand All @@ -127,6 +137,8 @@ public function deleteDocuments($indexName, $indexType, array $ids)
*/
public function deleteIndex($indexName)
{
$indexName = strtolower($indexName);

return $this->getActiveProvider()->deleteIndex($this->indexNamePrefix . $indexName);
}

Expand Down