<?php
/**
* project: Pimcore - SNR
* User: erikb
* Year: 2022
*/
namespace Devkitchen\DocumentIndexingBundle\ElasticSearchService;
use Devkitchen\DocumentIndexingBundle\DataExtractorService\DataExtractorService;
use Devkitchen\DocumentIndexingBundle\DataExtractorService\DocumentTypeHandler\DocumentTypeHandlerInterface;
use Devkitchen\DocumentIndexingBundle\Logger\LoggerInterface;
use Elastic\Elasticsearch\Client;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\ElasticsearchException;
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Elastic\Elasticsearch\Response\Elasticsearch;
use Elastic\Transport\Exception\NotFoundException;
use Http\Promise\Promise;
use Symfony\Component\HttpFoundation\RequestStack;
class ElasticSearchService {
/** @var array */
protected array $hosts;
/** @var string */
protected string $index;
/** @var string */
protected string $user;
/** @var string */
protected string $password;
/** @var Client $esClient */
protected Client $esClient;
/** @var LoggerInterface $logger */
protected LoggerInterface $logger;
/** @var RequestStack */
protected RequestStack $requestStack;
/** @var string $language */
protected string $language;
/** @var string $documentType */
protected string $documentType = 'default';
public function __construct(array $hosts, string $index, string $user, string $password, LoggerInterface $logger, RequestStack $requestStack) {
$this->hosts = $hosts;
$this->index = $index;
$this->user = $user;
$this->password = $password;
$this->logger = $logger;
$this->requestStack = $requestStack;
$this->esClient = ClientBuilder::create()
->setHttpClient( new \GuzzleHttp\Client(['verify' => false]))
->setSSLVerification(false)
->setHosts( $this->hosts )
->setBasicAuthentication( $this->user, $this->password )
->build();
}
/**
* @param DataExtractorService $dataService
*
* @return mixed
*/
public function delete(DataExtractorService $dataService) : mixed {
$params = [
'index' => $this->index. '.' . $dataService->getElasticIndex() .'.' . $dataService->getLanguage(),
'id' => $dataService->getId()
];
try {
return $this->esClient->delete($params);
}
catch (ServerResponseException $serverResponseException) {
return $this->_handleErrors( $serverResponseException );
}
catch (ClientResponseException $clientResponseException) {
return $this->_handleErrors( $clientResponseException );
}
}
/**
* @param DataExtractorService $dataService
*
* @return mixed
*/
public function add(DataExtractorService $dataService) : mixed {
$params = [
'index' => $this->index. '.' . $dataService->getElasticIndex() .'.' . $dataService->getLanguage(),
'id' => $dataService->getId(),
'body' => $dataService->getElasticSearchData()
];
try {
return $this->esClient->index( $params );
}
catch (ServerResponseException $serverResponseException) {
return $this->_handleErrors( $serverResponseException );
}
catch (ClientResponseException $clientResponseException) {
return $this->_handleErrors( $clientResponseException);
}
}
/**
* @param array $query
* @param string $documentIndexName
* @param int $from
* @param int $size
*
* @return mixed
*/
public function search(array $query, int $from = 0, int $size = 1000) : mixed {
$params = [
'index' => $this->_buildIndexName(),
'body' => $query,
'from' => $from,
'size' => $size
];
try {
return $this->esClient->search($params);
}
catch (ServerResponseException $serverResponseException) {
return $this->_handleErrors( $serverResponseException );
}
catch (ClientResponseException $clientResponseException) {
return $this->_handleErrors( $clientResponseException);
}
}
/**
* @return string
*/
private function _buildIndexName() : string {
return $this->index. '.' . $this->documentType. '.' .$this->language;
}
/**
* @param $exception
*
* @return mixed
*/
private function _handleErrors($exception) : mixed {
$message = $exception->getFile(). ' : '.$exception->getLine().' : '.$exception->getMessage();
$this->logger->log( 'error', $message);
return $exception->getResponse();
}
/**
* @param mixed $language
*
* @return ElasticSearchService
*/
public function setLanguage( mixed $language = null): ElasticSearchService {
if ($language === null && $this->requestStack) {
$language = $this->requestStack->getCurrentRequest()->getLocale();
}
if ($language == '') {
$language = 'de';
}
$this->language = $language;
return $this;
}
/**
* @param string $documentType
*
* @return ElasticSearchService
*/
public function setDocumentType( string $documentType ): ElasticSearchService {
$this->documentType = $documentType;
return $this;
}
}