resultJsonFactory = $resultJsonFactory; $this->tagFilter = $tagFilter; $this->config = $config; $this->getAmastySearchEngines = $getAmastySearchEngines; $this->clientFactory = $clientFactory; } /** * @return $this|ResponseInterface|ResultInterface */ public function execute() { $result = [ 'success' => false, 'errorMessage' => '', ]; $options = $this->getRequest()->getParams(); try { if (empty($options['engine']) || !in_array($options['engine'], $this->getAmastySearchEngines->execute(), true) ) { throw new LocalizedException( __('Test connection can be applied only for Amasty Elastic Search engine.') ); } $connectionData = $this->config->prepareConnectionData($options); $client = $this->clientFactory->create($options['engine'], $connectionData); $pingResult = $client->ping(); if ($pingResult) { $indexPrefix = $connectionData['index'] ?? $this->config->getIndexPrefix(); $this->checkIsReadOnly($client, $indexPrefix); if (isset($options['customAnalyzer']) && $options['customAnalyzer'] != CustomAnalyzer::DISABLED ) { $customAnalyzerIndexName = $indexPrefix . '_' . self::CUSTOM_ANALYZER_TEST_INDEX_NAME; $client->createIndex( $customAnalyzerIndexName, [ 'analysis' => [ 'analyzer' => [ 'default' => ['type' => $options['customAnalyzer']] ] ] ] ); try { $client->deleteIndex($customAnalyzerIndexName); } catch (Missing404Exception $e) { ;// do nothing } } $result['success'] = $pingResult; } else { $result['errorMessage'] = __('It is impossible to perform the search. Please make sure Elasticsearch Engine is installed on the server and configured properly.'); } // @codingStandardsIgnoreLine } catch (LocalizedException $e) { $result['errorMessage'] = $e->getMessage(); } catch (\Exception $e) { if (preg_match(self::EXCEPTION_PATTERN, $e->getMessage())) { $result['errorMessage'] = __('To use custom analyzer you have to install matching plugin'); } else { $result['errorMessage'] = $this->tagFilter->filter(__($e->getMessage())); } } /** @var \Magento\Framework\Controller\Result\Json $resultJson */ $resultJson = $this->resultJsonFactory->create(); return $resultJson->setData($result); } private function checkIsReadOnly(ClientInterface $client, string $indexPrefix) { $testIndexName = $indexPrefix . '_' . self::IS_READ_ONLY_INDEX; try { $client->createIndex($testIndexName, []); $client->deleteIndex($testIndexName); } catch (\Exception $e) { $this->messageManager->addErrorMessage( __('Please check the state of read_only_allow_delete setting in Elasticsearch server configuration.') ); } } }