Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexNameResolver.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Elasticsearch\Model\Client\Elasticsearch as ElasticsearchClient;
12 use Psr\Log\LoggerInterface;
15 
22 {
27  protected $connectionManager;
28 
33  protected $clientConfig;
34 
39  protected $client;
40 
45  protected $logger;
46 
56  public function __construct(
59  LoggerInterface $logger,
60  $options = []
61  ) {
62  $this->connectionManager = $connectionManager;
63  $this->clientConfig = $clientConfig;
64  $this->logger = $logger;
65 
66  try {
67  $this->client = $this->connectionManager->getConnection($options);
68  } catch (\Exception $e) {
69  $this->logger->critical($e);
70  throw new LocalizedException(
71  __('The search failed because of a search engine misconfiguration.')
72  );
73  }
74  }
75 
82  protected function getIndexNamespace()
83  {
84  return $this->clientConfig->getIndexPrefix();
85  }
86 
96  public function getIndexNameForAlias($storeId, $mappedIndexerId)
97  {
98  return $this->clientConfig->getIndexPrefix() . '_' . $mappedIndexerId . '_' . $storeId;
99  }
100 
110  public function getIndexName($storeId, $mappedIndexerId, array $preparedIndex)
111  {
112  if (isset($preparedIndex[$storeId])) {
113  return $preparedIndex[$storeId];
114  } else {
115  $indexName = $this->getIndexFromAlias($storeId, $mappedIndexerId);
116  if (empty($indexName)) {
117  $indexName = $this->getIndexPattern($storeId, $mappedIndexerId) . 1;
118  }
119  }
120  return $indexName;
121  }
122 
131  public function getIndexPattern($storeId, $mappedIndexerId)
132  {
133  return $this->getIndexNamespace() . '_' . $mappedIndexerId . '_' . $storeId . '_v';
134  }
135 
144  public function getIndexFromAlias($storeId, $mappedIndexerId)
145  {
146  $storeIndex = '';
147  $indexPattern = $this->getIndexPattern($storeId, $mappedIndexerId);
148  $namespace = $this->getIndexNamespace() . '_' . $mappedIndexerId . '_' . $storeId;
149  if ($this->client->existsAlias($namespace)) {
150  $alias = $this->client->getAlias($namespace);
151  $indices = array_keys($alias);
152  foreach ($indices as $index) {
153  if (strpos($index, $indexPattern) === 0) {
154  $storeIndex = $index;
155  break;
156  }
157  }
158  }
159  return $storeIndex;
160  }
161 
169  public function getIndexMapping($indexerId)
170  {
171  if ($indexerId == Fulltext::INDEXER_ID) {
172  $mappedIndexerId = 'product';
173  } else {
174  $mappedIndexerId = $indexerId;
175  }
176  return $mappedIndexerId;
177  }
178 }
__construct(ConnectionManager $connectionManager, Config $clientConfig, LoggerInterface $logger, $options=[])
__()
Definition: __.php:13
getIndexName($storeId, $mappedIndexerId, array $preparedIndex)
if(!trim($html)) $alias
Definition: details.phtml:20
$index
Definition: list.phtml:44