diff --git a/Classes/Connection/Elasticsearch.php b/Classes/Connection/Elasticsearch.php index 555d4e7..971aa4c 100644 --- a/Classes/Connection/Elasticsearch.php +++ b/Classes/Connection/Elasticsearch.php @@ -161,10 +161,13 @@ class Elasticsearch implements Singleton, ConnectionInterface public function deleteIndex(string $documentType) { - $index = $this->connection->getClient()->getIndex('typo3content'); + $index = $this->connection->getClient()->getIndex($this->indexFactory->getIndexName()); if (! $index->exists()) { - $this->logger->notice('Index did not exist, therefore was not deleted.', [$documentType, 'typo3content']); + $this->logger->notice( + 'Index did not exist, therefore was not deleted.', + [$documentType, $this->indexFactory->getIndexName()] + ); return; } @@ -193,7 +196,7 @@ class Elasticsearch implements Singleton, ConnectionInterface $this->logger->debug('Search for', [$searchRequest->getSearchTerm()]); $search = new \Elastica\Search($this->connection->getClient()); - $search->addIndex('typo3content'); + $search->addIndex($this->indexFactory->getIndexName()); $search->setQuery($this->queryFactory->create($searchRequest)); return $this->objectManager->get(SearchResult::class, $searchRequest, $search->search()); diff --git a/Classes/Connection/Elasticsearch/IndexFactory.php b/Classes/Connection/Elasticsearch/IndexFactory.php index 2ef905f..d76edc1 100644 --- a/Classes/Connection/Elasticsearch/IndexFactory.php +++ b/Classes/Connection/Elasticsearch/IndexFactory.php @@ -60,12 +60,22 @@ class IndexFactory implements Singleton $this->configuration = $configuration; } + /** + * Get the index name from the typoscript settings. + * + * @return string + */ + public function getIndexName() + { + return $this->configuration->get('connections.elasticsearch.index'); + } + /** * Get an index bases on TYPO3 table name. */ public function getIndex(Connection $connection, string $documentType) : \Elastica\Index { - $index = $connection->getClient()->getIndex('typo3content'); + $index = $connection->getClient()->getIndex($this->getIndexName()); if ($index->exists() === false) { $config = $this->getConfigurationFor($documentType); diff --git a/Configuration/TypoScript/constants.txt b/Configuration/TypoScript/constants.txt index b818471..ea5b86b 100644 --- a/Configuration/TypoScript/constants.txt +++ b/Configuration/TypoScript/constants.txt @@ -5,6 +5,7 @@ plugin { elasticsearch { host = localhost port = 9200 + index = typo3content } } diff --git a/Configuration/TypoScript/setup.txt b/Configuration/TypoScript/setup.txt index 751e47c..d3051c1 100644 --- a/Configuration/TypoScript/setup.txt +++ b/Configuration/TypoScript/setup.txt @@ -5,6 +5,7 @@ plugin { elasticsearch { host = {$plugin.tx_searchcore.settings.connections.elasticsearch.host} port = {$plugin.tx_searchcore.settings.connections.elasticsearch.port} + index = {$plugin.tx_searchcore.settings.connections.elasticsearch.index} } } diff --git a/Documentation/source/changelog.rst b/Documentation/source/changelog.rst index 27e6fab..d6a2b09 100644 --- a/Documentation/source/changelog.rst +++ b/Documentation/source/changelog.rst @@ -5,6 +5,7 @@ Changelog :maxdepth: 1 :glob: + changelog/20180518-75-make-index-name-configurable changelog/20180424-149-extract-relation-resolver-to-data-processing changelog/20180410-148-keep-sys_language_uid changelog/20180315-134-make-conent-fields-configurable diff --git a/Documentation/source/changelog/20180518-75-make-index-name-configurable.rst b/Documentation/source/changelog/20180518-75-make-index-name-configurable.rst new file mode 100644 index 0000000..28caa1b --- /dev/null +++ b/Documentation/source/changelog/20180518-75-make-index-name-configurable.rst @@ -0,0 +1,11 @@ +Feature 75 "Make index name configurable" +========================================== + +Prior to the change the index name was not changeable and hardcoded to `typo3content`. +Now you can change the index name for different use cases. + +You can now use one Elasticsearch server for multiple TYPO3 installations or multiple environments. + +The default is set to `typo3content`. No changes are necessary. + +See :issue:`75`. diff --git a/Documentation/source/configuration/connections.rst b/Documentation/source/configuration/connections.rst index 6d0c524..6c3f600 100644 --- a/Documentation/source/configuration/connections.rst +++ b/Documentation/source/configuration/connections.rst @@ -46,3 +46,18 @@ The port where search service is reachable. E.g. default ``9200`` for Elasticsea Example:: plugin.tx_searchcore.settings.connections.elasticsearch.port = 9200 + +.. _index: + +``index`` +--------- + +Used by: :ref:`connection_elasticsearch`. + +The index where the documents are being indexed to. E.g. default ``typo3content`` for Elasticsearch. + +This can be used to have one Elasticsearch server for multiple environments. E.g. production and staging. + +Example:: + + plugin.tx_searchcore.settings.connections.elasticsearch.index = typo3content diff --git a/Tests/Functional/Fixtures/BasicSetup.ts b/Tests/Functional/Fixtures/BasicSetup.ts index a4a26fb..e2cc456 100644 --- a/Tests/Functional/Fixtures/BasicSetup.ts +++ b/Tests/Functional/Fixtures/BasicSetup.ts @@ -5,6 +5,7 @@ plugin { elasticsearch { host = localhost port = 9200 + index = typo3content } } diff --git a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php index 4ede3cb..b48d08c 100644 --- a/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php +++ b/Tests/Unit/Connection/Elasticsearch/IndexFactoryTest.php @@ -24,6 +24,7 @@ use Codappix\SearchCore\Configuration\ConfigurationContainerInterface; use Codappix\SearchCore\Connection\Elasticsearch\Connection; use Codappix\SearchCore\Connection\Elasticsearch\IndexFactory; use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase; +use PHPUnit_Framework_MockObject_MockObject; class IndexFactoryTest extends AbstractUnitTestCase { @@ -32,6 +33,11 @@ class IndexFactoryTest extends AbstractUnitTestCase */ protected $subject; + /** + * @var PHPUnit_Framework_MockObject_MockObject + */ + protected $configuration; + public function setUp() { parent::setUp(); @@ -68,6 +74,11 @@ class IndexFactoryTest extends AbstractUnitTestCase ->method('getClient') ->willReturn($clientMock); + $this->configuration->expects($this->once()) + ->method('get') + ->with('connections.elasticsearch.index') + ->willReturn('typo3content'); + $this->subject->getIndex($connection, 'someIndex'); } @@ -123,10 +134,20 @@ class IndexFactoryTest extends AbstractUnitTestCase ->method('getClient') ->willReturn($clientMock); - $this->configuration->expects($this->once()) + $this->configuration->expects($this->exactly(2)) ->method('get') - ->with('indexing.someIndex.index') - ->willReturn($configuration); + ->will( + $this->returnValueMap([ + [ + 'indexing.someIndex.index', + $configuration + ], + [ + 'connections.elasticsearch.index', + 'typo3content' + ] + ]) + ); $this->subject->getIndex($connection, 'someIndex'); }