|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Chrysanthos\ScoutElasticAppSearch; |
| 4 | + |
| 5 | +use Elastic\EnterpriseSearch\AppSearch\Request\CreateEngine; |
| 6 | +use Elastic\EnterpriseSearch\AppSearch\Request\DeleteDocuments; |
| 7 | +use Elastic\EnterpriseSearch\AppSearch\Request\GetEngine; |
| 8 | +use Elastic\EnterpriseSearch\AppSearch\Request\IndexDocuments; |
| 9 | +use Elastic\EnterpriseSearch\AppSearch\Request\Search; |
| 10 | +use Elastic\EnterpriseSearch\AppSearch\Schema\Engine; |
| 11 | +use Elastic\EnterpriseSearch\AppSearch\Schema\SearchRequestParams; |
| 12 | +use Elastic\EnterpriseSearch\Client; |
| 13 | + |
| 14 | +class ElasticAppProxy |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var Client |
| 18 | + */ |
| 19 | + protected $client; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $engine; |
| 25 | + |
| 26 | + public function __construct(Client $client) |
| 27 | + { |
| 28 | + $this->client = $client->appSearch(); |
| 29 | + } |
| 30 | + |
| 31 | + public function setEngine($name): self |
| 32 | + { |
| 33 | + $this->engine = str_replace('_', '-', $name); |
| 34 | + |
| 35 | + return $this; |
| 36 | + } |
| 37 | + |
| 38 | + public function getClient() |
| 39 | + { |
| 40 | + return $this->client; |
| 41 | + } |
| 42 | + |
| 43 | + public function getEngine() |
| 44 | + { |
| 45 | + $this->client->getEngine(new GetEngine($this->engine)); |
| 46 | + } |
| 47 | + |
| 48 | + public function indexDocuments($objects) |
| 49 | + { |
| 50 | + $this->client->indexDocuments(new IndexDocuments($this->engine, $objects)); |
| 51 | + } |
| 52 | + |
| 53 | + public function deleteDocuments($objects) |
| 54 | + { |
| 55 | + $this->client->deleteDocuments(new DeleteDocuments($this->engine, $objects)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Ensure the Engine exists by checking for it and if not there creating it. |
| 60 | + * |
| 61 | + * @param $name |
| 62 | + */ |
| 63 | + public function ensureEngine($name) |
| 64 | + { |
| 65 | + $this->setEngine($name); |
| 66 | + |
| 67 | + try { |
| 68 | + $this->getEngine(); |
| 69 | + } catch (\Elastic\EnterpriseSearch\Exception\ClientErrorResponseException $e) { |
| 70 | + $this->client->createEngine(new CreateEngine(new Engine($name))); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Ensure the Engine exists by checking for it and if not there creating it. |
| 76 | + * |
| 77 | + * @param $name |
| 78 | + */ |
| 79 | + public function search($name) |
| 80 | + { |
| 81 | + $this->setEngine($name); |
| 82 | + |
| 83 | + try { |
| 84 | + $this->getEngine(); |
| 85 | + } catch (\Elastic\EnterpriseSearch\Exception\ClientErrorResponseException $e) { |
| 86 | + $this->client->search(new Search($name, new SearchRequestParams())); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /* |
| 91 | + * Flush the engine |
| 92 | + */ |
| 93 | + public function flushEngine($name = null) |
| 94 | + { |
| 95 | + if ($name) { |
| 96 | + $this->setEngine($name); |
| 97 | + } |
| 98 | + |
| 99 | + $this->deleteEngine(); |
| 100 | + $this->createEngine(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Dynamically call the Elastic client instance. Add the engine name to methods that require it. |
| 105 | + * |
| 106 | + * @param string $method |
| 107 | + * @param array $parameters |
| 108 | + * |
| 109 | + * @return mixed |
| 110 | + * @throws EngineNotInitialisedException |
| 111 | + */ |
| 112 | + public function __call($method, $parameters) |
| 113 | + { |
| 114 | + if (!method_exists($this->client, $method)) { |
| 115 | + throw new \BadMethodCallException($method . ' method not found on ' . get_class($this->client)); |
| 116 | + } |
| 117 | + |
| 118 | + if ($method !== 'listEngines' && !$this->engine) { |
| 119 | + throw new EngineNotInitialisedException('Unable to proxy call to Elastic App Client, no Engine initialised'); |
| 120 | + } |
| 121 | + |
| 122 | + if ($method !== 'listEngines' && $this->engine) { |
| 123 | + array_unshift($parameters, $this->engine); |
| 124 | + } |
| 125 | + |
| 126 | + return $this->client->$method(...$parameters); |
| 127 | + } |
| 128 | +} |
0 commit comments