2016-12-09 13:19:35 +01:00
|
|
|
<?php
|
2017-07-06 23:48:47 +02:00
|
|
|
namespace Codappix\SearchCore\Connection;
|
2016-12-09 13:19:35 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2017-07-06 23:48:47 +02:00
|
|
|
use Codappix\SearchCore\Connection\Elasticsearch\SearchResult;
|
|
|
|
use Codappix\SearchCore\Domain\Search\QueryFactory;
|
2017-07-06 12:03:52 +02:00
|
|
|
use TYPO3\CMS\Core\SingletonInterface as Singleton;
|
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
|
2016-12-09 13:19:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Outer wrapper to elasticsearch.
|
|
|
|
*/
|
|
|
|
class Elasticsearch implements Singleton, ConnectionInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Elasticsearch\Connection
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @var Elasticsearch\IndexFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected $indexFactory;
|
|
|
|
|
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @var Elasticsearch\TypeFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected $typeFactory;
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @var Elasticsearch\MappingFactory
|
|
|
|
*/
|
|
|
|
protected $mappingFactory;
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @var Elasticsearch\DocumentFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected $documentFactory;
|
|
|
|
|
2017-06-06 13:53:08 +02:00
|
|
|
/**
|
|
|
|
* @var QueryFactory
|
|
|
|
*/
|
|
|
|
protected $queryFactory;
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @var ObjectManagerInterface
|
|
|
|
*/
|
|
|
|
protected $objectManager;
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
|
|
|
* Inject log manager to get concrete logger from it.
|
|
|
|
*
|
|
|
|
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
|
|
|
|
*/
|
|
|
|
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager)
|
|
|
|
{
|
|
|
|
$this->logger = $logManager->getLogger(__CLASS__);
|
|
|
|
}
|
|
|
|
|
2017-07-06 12:03:52 +02:00
|
|
|
/**
|
|
|
|
* @param ObjectManagerInterface $objectManager
|
|
|
|
*/
|
|
|
|
public function injectObjectManager(ObjectManagerInterface $objectManager)
|
|
|
|
{
|
|
|
|
$this->objectManager = $objectManager;
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
/**
|
|
|
|
* @param Elasticsearch\Connection $connection
|
|
|
|
* @param Elasticsearch\IndexFactory $indexFactory
|
|
|
|
* @param Elasticsearch\TypeFactory $typeFactory
|
2017-07-06 12:03:52 +02:00
|
|
|
* @param Elasticsearch\MappingFactory $mappingFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
* @param Elasticsearch\DocumentFactory $documentFactory
|
2017-06-06 13:53:08 +02:00
|
|
|
* @param QueryFactory $queryFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Elasticsearch\Connection $connection,
|
|
|
|
Elasticsearch\IndexFactory $indexFactory,
|
|
|
|
Elasticsearch\TypeFactory $typeFactory,
|
2017-07-06 12:03:52 +02:00
|
|
|
Elasticsearch\MappingFactory $mappingFactory,
|
2017-06-06 13:53:08 +02:00
|
|
|
Elasticsearch\DocumentFactory $documentFactory,
|
|
|
|
QueryFactory $queryFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->indexFactory = $indexFactory;
|
|
|
|
$this->typeFactory = $typeFactory;
|
2017-07-06 12:03:52 +02:00
|
|
|
$this->mappingFactory = $mappingFactory;
|
2016-12-09 13:19:35 +01:00
|
|
|
$this->documentFactory = $documentFactory;
|
2017-06-06 13:53:08 +02:00
|
|
|
$this->queryFactory = $queryFactory;
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:17:58 +01:00
|
|
|
public function addDocument($documentType, array $document)
|
2016-12-09 13:19:35 +01:00
|
|
|
{
|
2016-12-12 13:33:07 +01:00
|
|
|
$this->withType(
|
|
|
|
$documentType,
|
2016-12-12 18:02:52 +01:00
|
|
|
function ($type) use ($document) {
|
|
|
|
$type->addDocument($this->documentFactory->getDocument($type->getName(), $document));
|
2016-12-12 13:33:07 +01:00
|
|
|
}
|
|
|
|
);
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:17:58 +01:00
|
|
|
public function deleteDocument($documentType, $identifier)
|
2016-12-09 13:19:35 +01:00
|
|
|
{
|
2017-01-17 09:22:41 +01:00
|
|
|
try {
|
|
|
|
$this->withType(
|
|
|
|
$documentType,
|
|
|
|
function ($type) use ($identifier) {
|
|
|
|
$type->deleteById($identifier);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (\Elastica\Exception\NotFoundException $exception) {
|
2017-11-11 16:46:03 +01:00
|
|
|
$this->logger->debug(
|
|
|
|
'Tried to delete document in index, which does not exist.',
|
|
|
|
[$documentType, $identifier]
|
|
|
|
);
|
2017-01-17 09:22:41 +01:00
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:17:58 +01:00
|
|
|
public function updateDocument($documentType, array $document)
|
2016-12-09 13:19:35 +01:00
|
|
|
{
|
2016-12-12 13:33:07 +01:00
|
|
|
$this->withType(
|
|
|
|
$documentType,
|
2016-12-12 18:02:52 +01:00
|
|
|
function ($type) use ($document) {
|
|
|
|
$type->updateDocument($this->documentFactory->getDocument($type->getName(), $document));
|
2016-12-12 13:33:07 +01:00
|
|
|
}
|
|
|
|
);
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
|
|
|
|
2016-12-09 14:07:38 +01:00
|
|
|
public function addDocuments($documentType, array $documents)
|
2016-12-09 13:19:35 +01:00
|
|
|
{
|
2016-12-12 13:33:07 +01:00
|
|
|
$this->withType(
|
|
|
|
$documentType,
|
2016-12-12 18:02:52 +01:00
|
|
|
function ($type) use ($documents) {
|
|
|
|
$type->addDocuments($this->documentFactory->getDocuments($type->getName(), $documents));
|
2016-12-12 13:33:07 +01:00
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
);
|
2016-12-12 13:33:07 +01:00
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
|
2017-11-10 13:22:15 +01:00
|
|
|
public function deleteIndex($documentType)
|
|
|
|
{
|
|
|
|
$index = $this->connection->getClient()->getIndex('typo3content');
|
|
|
|
|
|
|
|
if (! $index->exists()) {
|
|
|
|
$this->logger->notice('Index did not exist, therefore was not deleted.', [$documentType, 'typo3content']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$index->delete();
|
|
|
|
}
|
|
|
|
|
2016-12-12 13:33:07 +01:00
|
|
|
/**
|
|
|
|
* Execute given callback with Elastica Type based on provided documentType
|
|
|
|
*
|
|
|
|
* @param string $documentType
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
|
|
|
protected function withType($documentType, callable $callback)
|
|
|
|
{
|
|
|
|
$type = $this->getType($documentType);
|
2017-07-06 12:03:52 +02:00
|
|
|
// TODO: Check whether it's to heavy to send it so often e.g. for every single document.
|
|
|
|
// Perhaps add command controller to submit mapping?!
|
|
|
|
// Also it's not possible to change mapping without deleting index first.
|
|
|
|
// Mattes told about a solution.
|
|
|
|
// So command looks like the best way so far, except we manage mattes solution.
|
|
|
|
// Still then this should be done once. So perhaps singleton which tracks state and does only once?
|
|
|
|
$this->mappingFactory->getMapping($type)->send();
|
2016-12-12 13:33:07 +01:00
|
|
|
$callback($type);
|
2016-12-09 13:19:35 +01:00
|
|
|
$type->getIndex()->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
2016-12-15 09:17:58 +01:00
|
|
|
*
|
2017-07-06 12:03:52 +02:00
|
|
|
* @return SearchResultInterface
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
public function search(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
|
|
|
$this->logger->debug('Search for', [$searchRequest->getSearchTerm()]);
|
|
|
|
|
|
|
|
$search = new \Elastica\Search($this->connection->getClient());
|
|
|
|
$search->addIndex('typo3content');
|
2017-07-06 12:03:52 +02:00
|
|
|
$search->setQuery($this->queryFactory->create($searchRequest));
|
2017-06-06 13:53:08 +02:00
|
|
|
|
2017-08-08 17:07:23 +02:00
|
|
|
return $this->objectManager->get(SearchResult::class, $searchRequest, $search->search());
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|
2016-12-12 13:33:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $documentType
|
|
|
|
*
|
|
|
|
* @return \Elastica\Type
|
|
|
|
*/
|
|
|
|
protected function getType($documentType)
|
|
|
|
{
|
|
|
|
return $this->typeFactory->getType(
|
|
|
|
$this->indexFactory->getIndex(
|
|
|
|
$this->connection,
|
|
|
|
$documentType
|
|
|
|
),
|
|
|
|
$documentType
|
|
|
|
);
|
|
|
|
}
|
2016-12-09 13:19:35 +01:00
|
|
|
}
|