123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Elasticsearch\Model\Client;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\AdvancedSearch\Model\Client\ClientInterface;
- /**
- * Elasticsearch client
- */
- class Elasticsearch implements ClientInterface
- {
- /**
- * Elasticsearch Client instances
- *
- * @var \Elasticsearch\Client[]
- */
- private $client;
- /**
- * @var array
- */
- private $clientOptions;
- /**
- * @var bool
- */
- private $pingResult;
- /**
- * Initialize Elasticsearch Client
- *
- * @param array $options
- * @param \Elasticsearch\Client|null $elasticsearchClient
- * @throws LocalizedException
- */
- public function __construct(
- $options = [],
- $elasticsearchClient = null
- ) {
- if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
- ($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
- throw new LocalizedException(
- __('The search failed because of a search engine misconfiguration.')
- );
- }
- if (!($elasticsearchClient instanceof \Elasticsearch\Client)) {
- $config = $this->buildConfig($options);
- $elasticsearchClient = \Elasticsearch\ClientBuilder::fromConfig($config, true);
- }
- $this->client[getmypid()] = $elasticsearchClient;
- $this->clientOptions = $options;
- }
- /**
- * Get Elasticsearch Client
- *
- * @return \Elasticsearch\Client
- */
- private function getClient()
- {
- $pid = getmypid();
- if (!isset($this->client[$pid])) {
- $config = $this->buildConfig($this->clientOptions);
- $this->client[$pid] = \Elasticsearch\ClientBuilder::fromConfig($config, true);
- }
- return $this->client[$pid];
- }
- /**
- * Ping the Elasticsearch client
- *
- * @return bool
- */
- public function ping()
- {
- if ($this->pingResult === null) {
- $this->pingResult = $this->getClient()->ping(['client' => ['timeout' => $this->clientOptions['timeout']]]);
- }
- return $this->pingResult;
- }
- /**
- * Validate connection params
- *
- * @return bool
- */
- public function testConnection()
- {
- return $this->ping();
- }
- /**
- * @param array $options
- * @return array
- */
- private function buildConfig($options = [])
- {
- $host = preg_replace('/http[s]?:\/\//i', '', $options['hostname']);
- $protocol = parse_url($options['hostname'], PHP_URL_SCHEME);
- if (!$protocol) {
- $protocol = 'http';
- }
- if (!empty($options['port'])) {
- $host .= ':' . $options['port'];
- }
- if (!empty($options['enableAuth']) && ($options['enableAuth'] == 1)) {
- $host = sprintf('%s://%s:%s@%s', $protocol, $options['username'], $options['password'], $host);
- }
- $options['hosts'] = [$host];
- return $options;
- }
- /**
- * Performs bulk query over Elasticsearch index
- *
- * @param array $query
- * @return void
- */
- public function bulkQuery($query)
- {
- $this->getClient()->bulk($query);
- }
- /**
- * Creates an Elasticsearch index.
- *
- * @param string $index
- * @param array $settings
- * @return void
- */
- public function createIndex($index, $settings)
- {
- $this->getClient()->indices()->create([
- 'index' => $index,
- 'body' => $settings,
- ]);
- }
- /**
- * Delete an Elasticsearch index.
- *
- * @param string $index
- * @return void
- */
- public function deleteIndex($index)
- {
- $this->getClient()->indices()->delete(['index' => $index]);
- }
- /**
- * Check if index is empty.
- *
- * @param string $index
- * @return bool
- */
- public function isEmptyIndex($index)
- {
- $stats = $this->getClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
- if ($stats['indices'][$index]['primaries']['docs']['count'] == 0) {
- return true;
- }
- return false;
- }
- /**
- * Updates alias.
- *
- * @param string $alias
- * @param string $newIndex
- * @param string $oldIndex
- * @return void
- */
- public function updateAlias($alias, $newIndex, $oldIndex = '')
- {
- $params['body'] = ['actions' => []];
- if ($oldIndex) {
- $params['body']['actions'][] = ['remove' => ['alias' => $alias, 'index' => $oldIndex]];
- }
- if ($newIndex) {
- $params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
- }
- $this->getClient()->indices()->updateAliases($params);
- }
- /**
- * Checks whether Elasticsearch index exists
- *
- * @param string $index
- * @return bool
- */
- public function indexExists($index)
- {
- return $this->getClient()->indices()->exists(['index' => $index]);
- }
- /**
- * @param string $alias
- * @param string $index
- *
- * @return bool
- */
- public function existsAlias($alias, $index = '')
- {
- $params = ['name' => $alias];
- if ($index) {
- $params['index'] = $index;
- }
- return $this->getClient()->indices()->existsAlias($params);
- }
- /**
- * @param string $alias
- *
- * @return array
- */
- public function getAlias($alias)
- {
- return $this->getClient()->indices()->getAlias(['name' => $alias]);
- }
- /**
- * Add mapping to Elasticsearch index
- *
- * @param array $fields
- * @param string $index
- * @param string $entityType
- * @return void
- */
- public function addFieldsMapping(array $fields, $index, $entityType)
- {
- $params = [
- 'index' => $index,
- 'type' => $entityType,
- 'body' => [
- $entityType => [
- '_all' => [
- 'enabled' => true,
- 'type' => 'string'
- ],
- 'properties' => [],
- 'dynamic_templates' => [
- [
- 'price_mapping' => [
- 'match' => 'price_*',
- 'match_mapping' => 'string',
- 'mapping' => [
- 'type' => 'float'
- ],
- ],
- ],
- [
- 'string_mapping' => [
- 'match' => '*',
- 'match_mapping' => 'string',
- 'mapping' => [
- 'type' => 'string',
- 'index' => 'no'
- ],
- ],
- ],
- [
- 'position_mapping' => [
- 'match' => 'position_*',
- 'match_mapping' => 'string',
- 'mapping' => [
- 'type' => 'int'
- ],
- ],
- ],
- ],
- ],
- ],
- ];
- foreach ($fields as $field => $fieldInfo) {
- $params['body'][$entityType]['properties'][$field] = $fieldInfo;
- }
- $this->getClient()->indices()->putMapping($params);
- }
- /**
- * Delete mapping in Elasticsearch index
- *
- * @param string $index
- * @param string $entityType
- * @return void
- */
- public function deleteMapping($index, $entityType)
- {
- $this->getClient()->indices()->deleteMapping([
- 'index' => $index,
- 'type' => $entityType,
- ]);
- }
- /**
- * Execute search by $query
- *
- * @param array $query
- * @return array
- */
- public function query($query)
- {
- return $this->getClient()->search($query);
- }
- /**
- * Execute suggest query
- *
- * @param array $query
- * @return array
- */
- public function suggest($query)
- {
- return $this->getClient()->suggest($query);
- }
- }
|