IndexNameResolver.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Adapter\Index;
  7. use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
  8. use Magento\Elasticsearch\SearchAdapter\ConnectionManager;
  9. use Magento\Elasticsearch\Model\Config;
  10. use Psr\Log\LoggerInterface;
  11. use Magento\Framework\Exception\LocalizedException;
  12. use Magento\CatalogSearch\Model\Indexer\Fulltext;
  13. /**
  14. * Index name resolver
  15. * @api
  16. * @since 100.1.0
  17. */
  18. class IndexNameResolver
  19. {
  20. /**
  21. * @var ConnectionManager
  22. * @since 100.1.0
  23. */
  24. protected $connectionManager;
  25. /**
  26. * @var Config
  27. * @since 100.1.0
  28. */
  29. protected $clientConfig;
  30. /**
  31. * @var ElasticsearchClient
  32. * @since 100.1.0
  33. */
  34. protected $client;
  35. /**
  36. * @var LoggerInterface
  37. * @since 100.1.0
  38. */
  39. protected $logger;
  40. /**
  41. * Constructor for Index Name Resolver
  42. *
  43. * @param ConnectionManager $connectionManager
  44. * @param Config $clientConfig
  45. * @param LoggerInterface $logger
  46. * @param array $options
  47. * @throws LocalizedException
  48. */
  49. public function __construct(
  50. ConnectionManager $connectionManager,
  51. Config $clientConfig,
  52. LoggerInterface $logger,
  53. $options = []
  54. ) {
  55. $this->connectionManager = $connectionManager;
  56. $this->clientConfig = $clientConfig;
  57. $this->logger = $logger;
  58. try {
  59. $this->client = $this->connectionManager->getConnection($options);
  60. } catch (\Exception $e) {
  61. $this->logger->critical($e);
  62. throw new LocalizedException(
  63. __('The search failed because of a search engine misconfiguration.')
  64. );
  65. }
  66. }
  67. /**
  68. * Get index namespace from config
  69. *
  70. * @return string
  71. * @since 100.1.0
  72. */
  73. protected function getIndexNamespace()
  74. {
  75. return $this->clientConfig->getIndexPrefix();
  76. }
  77. /**
  78. * Get index namespace from config
  79. *
  80. * @param int $storeId
  81. * @param string $mappedIndexerId
  82. *
  83. * @return string
  84. * @since 100.1.0
  85. */
  86. public function getIndexNameForAlias($storeId, $mappedIndexerId)
  87. {
  88. return $this->clientConfig->getIndexPrefix() . '_' . $mappedIndexerId . '_' . $storeId;
  89. }
  90. /**
  91. * Returns the index name
  92. *
  93. * @param int $storeId
  94. * @param string $mappedIndexerId
  95. * @param array $preparedIndex
  96. * @return string
  97. * @since 100.1.0
  98. */
  99. public function getIndexName($storeId, $mappedIndexerId, array $preparedIndex)
  100. {
  101. if (isset($preparedIndex[$storeId])) {
  102. return $preparedIndex[$storeId];
  103. } else {
  104. $indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
  105. if (empty($indexName)) {
  106. $indexName = $this->getIndexPattern($storeId, $mappedIndexerId) . 1;
  107. }
  108. }
  109. return $indexName;
  110. }
  111. /**
  112. * Returns index pattern.
  113. *
  114. * @param int $storeId
  115. * @param string $mappedIndexerId
  116. * @return string
  117. * @since 100.1.0
  118. */
  119. public function getIndexPattern($storeId, $mappedIndexerId)
  120. {
  121. return $this->getIndexNamespace() . '_' . $mappedIndexerId . '_' . $storeId . '_v';
  122. }
  123. /**
  124. * Returns index for store in alias definition.
  125. *
  126. * @param int $storeId
  127. * @param string $mappedIndexerId
  128. * @return string
  129. * @since 100.1.0
  130. */
  131. public function getIndexFromAlias($storeId, $mappedIndexerId)
  132. {
  133. $storeIndex = '';
  134. $indexPattern = $this->getIndexPattern($storeId, $mappedIndexerId);
  135. $namespace = $this->getIndexNamespace() . '_' . $mappedIndexerId . '_' . $storeId;
  136. if ($this->client->existsAlias($namespace)) {
  137. $alias = $this->client->getAlias($namespace);
  138. $indices = array_keys($alias);
  139. foreach ($indices as $index) {
  140. if (strpos($index, $indexPattern) === 0) {
  141. $storeIndex = $index;
  142. break;
  143. }
  144. }
  145. }
  146. return $storeIndex;
  147. }
  148. /**
  149. * Taking index name by indexer ID
  150. *
  151. * @param string $indexerId
  152. * @return string
  153. * @since 100.1.0
  154. */
  155. public function getIndexMapping($indexerId)
  156. {
  157. if ($indexerId == Fulltext::INDEXER_ID) {
  158. $mappedIndexerId = 'product';
  159. } else {
  160. $mappedIndexerId = $indexerId;
  161. }
  162. return $mappedIndexerId;
  163. }
  164. }