From c1cc16efa59154cb8a6516a37a86e5ad79510465 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 25 Jul 2017 15:00:25 +0200 Subject: [PATCH] BUGFIX: Fix nun working options due to miss match of ts and es As TypoScript does not provide a way to configure key less options, we use a comma separated list and explode them to stay compatible with elasticsearch. --- .../Connection/Elasticsearch/IndexFactory.php | 30 ++++++++++++++++++- Documentation/source/configuration.rst | 11 +++---- .../Elasticsearch/IndexFactoryTest.php | 14 ++++----- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Classes/Connection/Elasticsearch/IndexFactory.php b/Classes/Connection/Elasticsearch/IndexFactory.php index f9fac51..66b448b 100644 --- a/Classes/Connection/Elasticsearch/IndexFactory.php +++ b/Classes/Connection/Elasticsearch/IndexFactory.php @@ -24,6 +24,7 @@ use Codappix\SearchCore\Configuration\ConfigurationContainerInterface; use Codappix\SearchCore\Configuration\InvalidArgumentException; use Elastica\Exception\ResponseException; use TYPO3\CMS\Core\SingletonInterface as Singleton; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; /** @@ -67,14 +68,41 @@ class IndexFactory implements Singleton /** * @param string $documentType + * * @return array */ protected function getConfigurationFor($documentType) { try { - return $this->configuration->get('indexing.' . $documentType . '.index'); + $configuration = $this->configuration->get('indexing.' . $documentType . '.index'); + + if (isset($configuration['analysis']['analyzer'])) { + foreach ($configuration['analysis']['analyzer'] as $key => $analyzer) { + $configuration['analysis']['analyzer'][$key] = $this->prepareAnalyzerConfiguration($analyzer); + } + } + + return $configuration; } catch (InvalidArgumentException $e) { return []; } } + + /** + * @param array $analyzer + * + * @return array + */ + protected function prepareAnalyzerConfiguration(array $analyzer) + { + $fieldsToExplode = ['char_filter', 'filter']; + + foreach ($fieldsToExplode as $fieldToExplode) { + if (isset($analyzer[$fieldToExplode])) { + $analyzer[$fieldToExplode] = GeneralUtility::trimExplode(',', $analyzer[$fieldToExplode], true); + } + } + + return $analyzer; + } } diff --git a/Documentation/source/configuration.rst b/Documentation/source/configuration.rst index e7b8a7f..68994d7 100644 --- a/Documentation/source/configuration.rst +++ b/Documentation/source/configuration.rst @@ -201,13 +201,8 @@ options are available: ngram4 { type = custom tokenizer = ngram4 - char_filter { - html_strip - } - - filter { - lowercase - } + char_filter = html_strip + filter = lowercase, asciifolding } } @@ -221,6 +216,8 @@ options are available: } } + ``char_filter`` and ``filter`` are a comma separated list of options. + .. _configuration_options_search: Searching diff --git a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php index ee55156..c73fe36 100644 --- a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php +++ b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php @@ -81,12 +81,8 @@ class IndexFactoryTest extends AbstractUnitTestCase 'ngram4' => [ 'type' => 'custom', 'tokenizer' => 'ngram4', - 'char_filter' => [ - 'html_strip', - ], - 'filter' => [ - 'lowercase', - ], + 'char_filter' => 'html_strip', + 'filter' => 'lowercase, , asciifolding', ], ], 'tokenizer' => [ @@ -99,6 +95,10 @@ class IndexFactoryTest extends AbstractUnitTestCase ], ]; + $expectedConfiguration = $configuration; + $expectedConfiguration['analysis']['analyzer']['ngram4']['char_filter'] = ['html_strip']; + $expectedConfiguration['analysis']['analyzer']['ngram4']['filter'] = ['lowercase', 'asciifolding']; + $indexMock = $this->getMockBuilder(\Elastica\Index::class) ->disableOriginalConstructor() ->getMock(); @@ -107,7 +107,7 @@ class IndexFactoryTest extends AbstractUnitTestCase ->willReturn(false); $indexMock->expects($this->once()) ->method('create') - ->with($configuration); + ->with($expectedConfiguration); $clientMock = $this->getMockBuilder(\Elastica\Client::class) ->disableOriginalConstructor() ->getMock();