2016-12-09 13:19:35 +01:00
|
|
|
<?php
|
|
|
|
namespace Leonmrni\SearchCore\Connection;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use TYPO3\CMS\Core\SingletonInterface as Singleton;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
2016-12-09 14:07:38 +01:00
|
|
|
* @var Elasticsearch\DocumentFactory
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
protected $documentFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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__);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Elasticsearch\Connection $connection
|
|
|
|
* @param Elasticsearch\IndexFactory $indexFactory
|
|
|
|
* @param Elasticsearch\TypeFactory $typeFactory
|
|
|
|
* @param Elasticsearch\DocumentFactory $documentFactory
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
Elasticsearch\Connection $connection,
|
|
|
|
Elasticsearch\IndexFactory $indexFactory,
|
|
|
|
Elasticsearch\TypeFactory $typeFactory,
|
|
|
|
Elasticsearch\DocumentFactory $documentFactory
|
|
|
|
) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->indexFactory = $indexFactory;
|
|
|
|
$this->typeFactory = $typeFactory;
|
|
|
|
$this->documentFactory = $documentFactory;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$this->logger->debug('Tried to delete document in index, which does not exist.', [$documentType, $identifier]);
|
|
|
|
}
|
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
|
|
|
|
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);
|
|
|
|
$callback($type);
|
2016-12-09 13:19:35 +01:00
|
|
|
$type->getIndex()->refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SearchRequestInterface $searchRequest
|
2016-12-15 09:17:58 +01:00
|
|
|
*
|
2016-12-12 18:02:52 +01:00
|
|
|
* @return \Elastica\ResultSet
|
2016-12-09 13:19:35 +01:00
|
|
|
*/
|
|
|
|
public function search(SearchRequestInterface $searchRequest)
|
|
|
|
{
|
|
|
|
$this->logger->debug('Search for', [$searchRequest->getSearchTerm()]);
|
|
|
|
|
2017-06-06 12:32:27 +02:00
|
|
|
$query = [
|
|
|
|
'bool' => [
|
|
|
|
'must' => [
|
|
|
|
[
|
|
|
|
'match' => [
|
|
|
|
'_all' => $searchRequest->getSearchTerm()
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($searchRequest->hasFilter()) {
|
|
|
|
$queryFilter = [];
|
|
|
|
foreach ($searchRequest->getFilter() as $field => $value) {
|
|
|
|
$queryFilter[$field] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query['bool']['filter'] = [
|
|
|
|
'term' => $queryFilter,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-12-09 13:19:35 +01:00
|
|
|
$search = new \Elastica\Search($this->connection->getClient());
|
|
|
|
$search->addIndex('typo3content');
|
2017-06-06 12:32:27 +02:00
|
|
|
$search->setQuery(new \Elastica\Query(['query' => $query]));
|
2016-12-09 13:19:35 +01:00
|
|
|
// TODO: Return wrapped result to implement our interface.
|
2016-12-12 18:02:52 +01:00
|
|
|
// Also update php doc to reflect the change.
|
2017-06-06 12:32:27 +02:00
|
|
|
return $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
|
|
|
}
|