From a15b78ff45a2afad0610cfd150dc35409eb54c44 Mon Sep 17 00:00:00 2001 From: Justus Moroni Date: Thu, 15 Feb 2018 16:44:02 +0100 Subject: [PATCH 1/3] FEATURE: Provide new feature to configure the index This enables you to configure the index which elastic will use. Related: #75 --- Classes/Connection/Elasticsearch.php | 9 ++++--- .../Connection/Elasticsearch/IndexFactory.php | 12 ++++++++- Configuration/TypoScript/constants.txt | 1 + Configuration/TypoScript/setup.txt | 1 + .../source/configuration/connections.rst | 13 +++++++++ Documentation/source/connections.rst | 2 ++ Tests/Functional/Fixtures/BasicSetup.ts | 1 + .../Elasticsearch/IndexFactoryTest.php | 27 ++++++++++++++++--- 8 files changed, 59 insertions(+), 7 deletions(-) diff --git a/Classes/Connection/Elasticsearch.php b/Classes/Connection/Elasticsearch.php index 8a3cb2b..a99641a 100644 --- a/Classes/Connection/Elasticsearch.php +++ b/Classes/Connection/Elasticsearch.php @@ -158,10 +158,13 @@ class Elasticsearch implements Singleton, ConnectionInterface public function deleteIndex($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; } @@ -198,7 +201,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 66b448b..6537ddd 100644 --- a/Classes/Connection/Elasticsearch/IndexFactory.php +++ b/Classes/Connection/Elasticsearch/IndexFactory.php @@ -47,6 +47,16 @@ 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. * @@ -57,7 +67,7 @@ class IndexFactory implements Singleton */ public function getIndex(Connection $connection, $documentType) { - $index = $connection->getClient()->getIndex('typo3content'); + $index = $connection->getClient()->getIndex($this->getIndexName()); if ($index->exists() === false) { $index->create($this->getConfigurationFor($documentType)); diff --git a/Configuration/TypoScript/constants.txt b/Configuration/TypoScript/constants.txt index 1a39851..7c8cab8 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 1a1577f..d1ab3d3 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/configuration/connections.rst b/Documentation/source/configuration/connections.rst index 5819730..8e92da1 100644 --- a/Documentation/source/configuration/connections.rst +++ b/Documentation/source/configuration/connections.rst @@ -51,5 +51,18 @@ Example:: plugin.tx_searchcore.settings.connections.elasticsearch.port = 9200 +.. _index: + +``index`` +-------- + +Used by: :ref:`Elasticsearch`. + +The index where the documents are being indexed to. E.g. default ``typo3content`` for Elasticsearch. + +Example:: + + plugin.tx_searchcore.settings.connections.elasticsearch.index = typo3content + diff --git a/Documentation/source/connections.rst b/Documentation/source/connections.rst index b51f280..3347f47 100644 --- a/Documentation/source/connections.rst +++ b/Documentation/source/connections.rst @@ -22,6 +22,8 @@ The connection is configurable through the following options: * :ref:`port` +* :ref:`index` + * :ref:`mapping` * :ref:`facets` diff --git a/Tests/Functional/Fixtures/BasicSetup.ts b/Tests/Functional/Fixtures/BasicSetup.ts index b7b0c6a..d4a5574 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 c73fe36..10ee006 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(); @@ -67,6 +73,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'); } @@ -122,10 +133,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'); } From 253ce5b591bf28fe12243b01a587a97080ff8b36 Mon Sep 17 00:00:00 2001 From: Justus Moroni Date: Fri, 18 May 2018 00:14:21 +0200 Subject: [PATCH 2/3] TASK: Provide more documentation for index name Add more documentation for the use cases. Add changelog entry. Related: #75 --- Documentation/source/changelog.rst | 1 + .../20180518-75-make-index-name-configurable.rst | 11 +++++++++++ Documentation/source/configuration/connections.rst | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 Documentation/source/changelog/20180518-75-make-index-name-configurable.rst 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 b108600..94d0ecf 100644 --- a/Documentation/source/configuration/connections.rst +++ b/Documentation/source/configuration/connections.rst @@ -56,6 +56,8 @@ Used by: :ref:`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 From 763d881f7cf73712ddd2976fc7be74ac8737fd75 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Fri, 18 May 2018 10:12:19 +0200 Subject: [PATCH 3/3] TASK: Fix two rst issues --- Documentation/source/configuration/connections.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/source/configuration/connections.rst b/Documentation/source/configuration/connections.rst index 94d0ecf..6c3f600 100644 --- a/Documentation/source/configuration/connections.rst +++ b/Documentation/source/configuration/connections.rst @@ -50,9 +50,9 @@ Example:: .. _index: ``index`` --------- +--------- -Used by: :ref:`Elasticsearch`. +Used by: :ref:`connection_elasticsearch`. The index where the documents are being indexed to. E.g. default ``typo3content`` for Elasticsearch.