src/Extern/ExternElasticService.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\Extern;
  3. use Elastica\Client;
  4. use Elastica\Exception\NotFoundException;
  5. use Elastica\Index;
  6. use Elastica\Search;
  7. use Psr\Log\LoggerInterface;
  8. use RuntimeException;
  9. class ExternElasticService
  10. {
  11.     protected $client;
  12.     protected $search;
  13.     /** @var Index $index */
  14.     protected $index;
  15.     /** @var LoggerInterface $logger */
  16.     private $logger;
  17.     public function __construct($host$port$transportLoggerInterface $logger)
  18.     {
  19.         $this->logger $logger;
  20.         $this->client = new Client(
  21.             [
  22.                 'servers' => [
  23.                     [
  24.                         'host' => $host,
  25.                         'port' => $port,
  26.                         'transport' => $transport
  27.                     ]
  28.                 ],
  29.             ]
  30.         );
  31.         $this->search = new Search($this->client);
  32.     }
  33.     public function getCount()
  34.     {
  35.         return $this->search->count();
  36.     }
  37.     public function getClient()
  38.     {
  39.         return $this->client;
  40.     }
  41.     public function getSearch()
  42.     {
  43.         return $this->search;
  44.     }
  45.     public function getIndex()
  46.     {
  47.         return $this->index;
  48.     }
  49.     public function addAndSetIndex(string $index)
  50.     {
  51.         try {
  52.             $this->search->addIndex($index);
  53.             $this->index $this->client->getIndex($index);
  54.         } catch (RuntimeException $exception) {
  55.             $this->logger->alert(
  56.                 "impossible to add index " $index ": " $exception->getMessage(),
  57.                 ['exception' => $exception]
  58.             );
  59.         }
  60.     }
  61.     public function getDocument($id)
  62.     {
  63.         $doc null;
  64.         try {
  65.             $doc $this->index->getDocument($id)->getData();
  66.             $doc["_id"] = $id;
  67.         } catch (NotFoundException $exception) {
  68.             $doc null;
  69.         } catch (RuntimeException $exception) {
  70.             $this->logger->alert(
  71.                 "impossible to get document: " $exception->getMessage(),
  72.                 ['exception' => $exception]
  73.             );
  74.         }
  75.         return $doc;
  76.     }
  77.     public function updateDocument($id$data)
  78.     {
  79.         $upd null;
  80.         try {
  81.             $doc $this->index->getDocument($id);
  82.             $doc->setData(array_merge($doc->getData(), (array)json_decode($data)));
  83.             $upd $this->index->updateDocument($doc);
  84.         } catch (RuntimeException $exception) {
  85.             $this->logger->alert(
  86.                 "impossible to update document: " $exception->getMessage(),
  87.                 ['exception' => $exception]
  88.             );
  89.             $upd null;
  90.         }
  91.         return $upd;
  92.     }
  93.     public function addDocument($id$data)
  94.     {
  95.         try {
  96.             $doc $this->index->createDocument($id$data);
  97.             return $this->index->addDocument($doc);
  98.         } catch (RuntimeException $exception) {
  99.             $this->logger->alert(
  100.                 "impossible to delete document: " $exception->getMessage(),
  101.                 ['exception' => $exception]
  102.             );
  103.         }
  104.         return null;
  105.     }
  106.     public function deleteDocument($id)
  107.     {
  108.         $del null;
  109.         try {
  110.             $doc $this->index->getDocument($id);
  111.             $del $this->index->deleteDocument($doc);
  112.         } catch (NotFoundException $exception) {
  113.             $del null;
  114.         } catch (RuntimeException $exception) {
  115.             $this->logger->alert(
  116.                 "impossible to delete document: " $exception->getMessage(),
  117.                 ['exception' => $exception]
  118.             );
  119.         }
  120.         return $del;
  121.     }
  122. }