FEATURE: Provide new feature to configure the index

This enables you to configure the index which elastic
will use.

Related: #75
This commit is contained in:
Justus Moroni 2018-02-15 16:44:02 +01:00
parent 07c9d5a136
commit a15b78ff45
8 changed files with 59 additions and 7 deletions

View file

@ -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());

View file

@ -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));

View file

@ -5,6 +5,7 @@ plugin {
elasticsearch {
host = localhost
port = 9200
index = typo3content
}
}

View file

@ -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}
}
}

View file

@ -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

View file

@ -22,6 +22,8 @@ The connection is configurable through the following options:
* :ref:`port`
* :ref:`index`
* :ref:`mapping`
* :ref:`facets`

View file

@ -5,6 +5,7 @@ plugin {
elasticsearch {
host = localhost
port = 9200
index = typo3content
}
}

View file

@ -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');
}