bundles/Devkitchen/DocumentIndexingBundle/src/ElasticSearchService/ElasticSearchService.php line 119

Open in your IDE?
  1. <?php
  2.     /**
  3.      * project: Pimcore - SNR
  4.      * User: erikb
  5.      * Year: 2022
  6.      */
  7.     namespace Devkitchen\DocumentIndexingBundle\ElasticSearchService;
  8.     use Devkitchen\DocumentIndexingBundle\DataExtractorService\DataExtractorService;
  9.     use Devkitchen\DocumentIndexingBundle\DataExtractorService\DocumentTypeHandler\DocumentTypeHandlerInterface;
  10.     use Devkitchen\DocumentIndexingBundle\Logger\LoggerInterface;
  11.     use Elastic\Elasticsearch\Client;
  12.     use Elastic\Elasticsearch\ClientBuilder;
  13.     use Elastic\Elasticsearch\Exception\ClientResponseException;
  14.     use Elastic\Elasticsearch\Exception\ElasticsearchException;
  15.     use Elastic\Elasticsearch\Exception\MissingParameterException;
  16.     use Elastic\Elasticsearch\Exception\ServerResponseException;
  17.     use Elastic\Elasticsearch\Response\Elasticsearch;
  18.     use Elastic\Transport\Exception\NotFoundException;
  19.     use Http\Promise\Promise;
  20.     use Symfony\Component\HttpFoundation\RequestStack;
  21.     class ElasticSearchService {
  22.         /** @var array  */
  23.         protected array $hosts;
  24.         /** @var string  */
  25.         protected string $index;
  26.         /** @var string  */
  27.         protected string $user;
  28.         /** @var string  */
  29.         protected string $password;
  30.         /** @var Client $esClient */
  31.         protected Client $esClient;
  32.         /** @var LoggerInterface $logger */
  33.         protected LoggerInterface $logger;
  34.         /** @var RequestStack  */
  35.         protected RequestStack $requestStack;
  36.         /** @var string $language */
  37.         protected string $language;
  38.         /** @var string $documentType */
  39.         protected string $documentType 'default';
  40.         public function __construct(array $hostsstring $indexstring $userstring $passwordLoggerInterface $loggerRequestStack $requestStack) {
  41.             $this->hosts $hosts;
  42.             $this->index $index;
  43.             $this->user $user;
  44.             $this->password $password;
  45.             $this->logger $logger;
  46.             $this->requestStack $requestStack;
  47.             $this->esClient ClientBuilder::create()
  48.                             ->setHttpClient( new \GuzzleHttp\Client(['verify' => false]))
  49.                             ->setSSLVerification(false)
  50.                             ->setHosts$this->hosts )
  51.                                             ->setBasicAuthentication$this->user$this->password )
  52.                                             ->build();
  53.         }
  54.         /**
  55.          * @param DataExtractorService $dataService
  56.          *
  57.          * @return mixed
  58.          */
  59.         public function delete(DataExtractorService $dataService) : mixed {
  60.             $params = [
  61.                 'index' => $this->index'.' $dataService->getElasticIndex() .'.' $dataService->getLanguage(),
  62.                 'id' => $dataService->getId()
  63.             ];
  64.             try {
  65.                 return $this->esClient->delete($params);
  66.             }
  67.             catch (ServerResponseException $serverResponseException) {
  68.                 return $this->_handleErrors$serverResponseException );
  69.             }
  70.             catch (ClientResponseException $clientResponseException) {
  71.                 return $this->_handleErrors$clientResponseException );
  72.             }
  73.         }
  74.         /**
  75.          * @param DataExtractorService $dataService
  76.          *
  77.          * @return mixed
  78.          */
  79.         public function add(DataExtractorService $dataService) : mixed {
  80.             $params = [
  81.                 'index' => $this->index'.' $dataService->getElasticIndex() .'.' $dataService->getLanguage(),
  82.                 'id' => $dataService->getId(),
  83.                 'body' => $dataService->getElasticSearchData()
  84.             ];
  85.             try {
  86.                 return $this->esClient->index$params );
  87.             }
  88.             catch (ServerResponseException $serverResponseException) {
  89.                 return $this->_handleErrors$serverResponseException );
  90.             }
  91.             catch (ClientResponseException $clientResponseException) {
  92.                 return $this->_handleErrors$clientResponseException);
  93.             }
  94.         }
  95.         /**
  96.          * @param array  $query
  97.          * @param string $documentIndexName
  98.          * @param int    $from
  99.          * @param int    $size
  100.          *
  101.          * @return mixed
  102.          */
  103.         public function search(array $queryint $from 0int $size 1000) : mixed {
  104.             $params = [
  105.                 'index' => $this->_buildIndexName(),
  106.                 'body' => $query,
  107.                 'from' => $from,
  108.                 'size' => $size
  109.             ];
  110.             try {
  111.                 return $this->esClient->search($params);
  112.             }
  113.             catch (ServerResponseException $serverResponseException) {
  114.                 return $this->_handleErrors$serverResponseException );
  115.             }
  116.             catch (ClientResponseException $clientResponseException) {
  117.                 return $this->_handleErrors$clientResponseException);
  118.             }
  119.         }
  120.         /**
  121.          * @return string
  122.          */
  123.         private function _buildIndexName() : string {
  124.             return $this->index'.' $this->documentType'.' .$this->language;
  125.         }
  126.         /**
  127.          * @param $exception
  128.          *
  129.          * @return mixed
  130.          */
  131.         private function _handleErrors($exception) : mixed {
  132.             $message $exception->getFile(). ' : '.$exception->getLine().' : '.$exception->getMessage();
  133.             $this->logger->log'error'$message);
  134.             return $exception->getResponse();
  135.         }
  136.         /**
  137.          * @param mixed $language
  138.          *
  139.          * @return ElasticSearchService
  140.          */
  141.         public function setLanguagemixed $language null): ElasticSearchService {
  142.             if ($language === null && $this->requestStack) {
  143.                 $language $this->requestStack->getCurrentRequest()->getLocale();
  144.             }
  145.             if ($language == '') {
  146.                 $language 'de';
  147.             }
  148.             
  149.             $this->language $language;
  150.             return $this;
  151.         }
  152.         /**
  153.          * @param string $documentType
  154.          *
  155.          * @return ElasticSearchService
  156.          */
  157.         public function setDocumentTypestring $documentType ): ElasticSearchService {
  158.             $this->documentType $documentType;
  159.             return $this;
  160.         }
  161.     }