Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Elasticsearch Class Reference

Public Member Functions

 __construct (\Magento\Elasticsearch\SearchAdapter\ConnectionManager $connectionManager, DataMapperInterface $documentDataMapper, FieldMapperInterface $fieldMapper, \Magento\Elasticsearch\Model\Config $clientConfig, \Magento\Elasticsearch\Model\Adapter\Index\BuilderInterface $indexBuilder, \Psr\Log\LoggerInterface $logger, \Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver $indexNameResolver, $options=[], BatchDataMapperInterface $batchDocumentDataMapper=null)
 
 ping ()
 
 prepareDocsPerStore (array $documentData, $storeId)
 
 addDocs (array $documents, $storeId, $mappedIndexerId)
 
 cleanIndex ($storeId, $mappedIndexerId)
 
 deleteDocs (array $documentIds, $storeId, $mappedIndexerId)
 
 checkIndex ( $storeId, $mappedIndexerId, $checkAlias=true)
 
 updateAlias ($storeId, $mappedIndexerId)
 

Data Fields

const BULK_ACTION_INDEX = 'index'
 
const BULK_ACTION_CREATE = 'create'
 
const BULK_ACTION_DELETE = 'delete'
 
const BULK_ACTION_UPDATE = 'update'
 

Protected Member Functions

 getDocsArrayInBulkIndexFormat ( $documents, $indexName, $action=self::BULK_ACTION_INDEX)
 
 prepareIndex ($storeId, $indexName, $mappedIndexerId)
 

Protected Attributes

 $connectionManager
 
 $documentDataMapper
 
 $indexNameResolver
 
 $fieldMapper
 
 $clientConfig
 
 $client
 
 $indexBuilder
 
 $logger
 
 $preparedIndex = []
 

Detailed Description

Elasticsearch adapter @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 15 of file Elasticsearch.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Elasticsearch\SearchAdapter\ConnectionManager  $connectionManager,
DataMapperInterface  $documentDataMapper,
FieldMapperInterface  $fieldMapper,
\Magento\Elasticsearch\Model\Config  $clientConfig,
\Magento\Elasticsearch\Model\Adapter\Index\BuilderInterface  $indexBuilder,
\Psr\Log\LoggerInterface  $logger,
\Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver  $indexNameResolver,
  $options = [],
BatchDataMapperInterface  $batchDocumentDataMapper = null 
)

Constructor for Elasticsearch adapter.

Parameters
\Magento\Elasticsearch\SearchAdapter\ConnectionManager$connectionManager
DataMapperInterface$documentDataMapper
FieldMapperInterface$fieldMapper
\Magento\Elasticsearch\Model\Config$clientConfig
\Magento\Elasticsearch\Model\Adapter\Index\BuilderInterface$indexBuilder
\Psr\Log\LoggerInterface$logger
\Magento\Elasticsearch\Model\Adapter\Index\IndexNameResolver$indexNameResolver
array$options
BatchDataMapperInterface$batchDocumentDataMapper
Exceptions

Definition at line 94 of file Elasticsearch.php.

104  {
105  $this->connectionManager = $connectionManager;
106  $this->documentDataMapper = $documentDataMapper;
107  $this->fieldMapper = $fieldMapper;
108  $this->clientConfig = $clientConfig;
109  $this->indexBuilder = $indexBuilder;
110  $this->logger = $logger;
111  $this->indexNameResolver = $indexNameResolver;
112  $this->batchDocumentDataMapper = $batchDocumentDataMapper ?:
113  ObjectManager::getInstance()->get(BatchDataMapperInterface::class);
114 
115  try {
116  $this->client = $this->connectionManager->getConnection($options);
117  } catch (\Exception $e) {
118  $this->logger->critical($e);
119  throw new \Magento\Framework\Exception\LocalizedException(
120  __('The search failed because of a search engine misconfiguration.')
121  );
122  }
123  }
__()
Definition: __.php:13

Member Function Documentation

◆ addDocs()

addDocs ( array  $documents,
  $storeId,
  $mappedIndexerId 
)

Add prepared Elasticsearch documents to Elasticsearch index

Parameters
array$documents
int$storeId
string$mappedIndexerId
Returns
$this
Exceptions

Definition at line 171 of file Elasticsearch.php.

172  {
173  if (count($documents)) {
174  try {
175  $indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
176  $bulkIndexDocuments = $this->getDocsArrayInBulkIndexFormat($documents, $indexName);
177  $this->client->bulkQuery($bulkIndexDocuments);
178  } catch (\Exception $e) {
179  $this->logger->critical($e);
180  throw $e;
181  }
182  }
183 
184  return $this;
185  }
getDocsArrayInBulkIndexFormat( $documents, $indexName, $action=self::BULK_ACTION_INDEX)

◆ checkIndex()

checkIndex (   $storeId,
  $mappedIndexerId,
  $checkAlias = true 
)

Checks whether Elasticsearch index and alias exists.

Parameters
int$storeId
string$mappedIndexerId
bool$checkAlias
Returns
$this

Definition at line 291 of file Elasticsearch.php.

295  {
296  // create new index for store
297  $indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
298  if (!$this->client->indexExists($indexName)) {
299  $this->prepareIndex($storeId, $indexName, $mappedIndexerId);
300  }
301 
302  // add index to alias
303  if ($checkAlias) {
304  $namespace = $this->indexNameResolver->getIndexNameForAlias($storeId, $mappedIndexerId);
305  if (!$this->client->existsAlias($namespace, $indexName)) {
306  $this->client->updateAlias($namespace, $indexName);
307  }
308  }
309  return $this;
310  }
prepareIndex($storeId, $indexName, $mappedIndexerId)

◆ cleanIndex()

cleanIndex (   $storeId,
  $mappedIndexerId 
)

Removes all documents from Elasticsearch index

Parameters
int$storeId
string$mappedIndexerId
Returns
$this

Definition at line 194 of file Elasticsearch.php.

195  {
196  $this->checkIndex($storeId, $mappedIndexerId, true);
197  $indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
198  if ($this->client->isEmptyIndex($indexName)) {
199  // use existing index if empty
200  return $this;
201  }
202 
203  // prepare new index name and increase version
204  $indexPattern = $this->indexNameResolver->getIndexPattern($storeId, $mappedIndexerId);
205  $version = intval(str_replace($indexPattern, '', $indexName));
206  $newIndexName = $indexPattern . ++$version;
207 
208  // remove index if already exists
209  if ($this->client->indexExists($newIndexName)) {
210  $this->client->deleteIndex($newIndexName);
211  }
212 
213  // prepare new index
214  $this->prepareIndex($storeId, $newIndexName, $mappedIndexerId);
215 
216  return $this;
217  }
checkIndex( $storeId, $mappedIndexerId, $checkAlias=true)
prepareIndex($storeId, $indexName, $mappedIndexerId)

◆ deleteDocs()

deleteDocs ( array  $documentIds,
  $storeId,
  $mappedIndexerId 
)

Delete documents from Elasticsearch index by Ids

Parameters
array$documentIds
int$storeId
string$mappedIndexerId
Returns
$this
Exceptions

Definition at line 228 of file Elasticsearch.php.

229  {
230  try {
231  $this->checkIndex($storeId, $mappedIndexerId, false);
232  $indexName = $this->indexNameResolver->getIndexName($storeId, $mappedIndexerId, $this->preparedIndex);
233  $bulkDeleteDocuments = $this->getDocsArrayInBulkIndexFormat(
234  $documentIds,
235  $indexName,
236  self::BULK_ACTION_DELETE
237  );
238  $this->client->bulkQuery($bulkDeleteDocuments);
239  } catch (\Exception $e) {
240  $this->logger->critical($e);
241  throw $e;
242  }
243 
244  return $this;
245  }
checkIndex( $storeId, $mappedIndexerId, $checkAlias=true)
getDocsArrayInBulkIndexFormat( $documents, $indexName, $action=self::BULK_ACTION_INDEX)

◆ getDocsArrayInBulkIndexFormat()

getDocsArrayInBulkIndexFormat (   $documents,
  $indexName,
  $action = self::BULK_ACTION_INDEX 
)
protected

Reformat documents array to bulk format

Parameters
array$documents
string$indexName
string$action
Returns
array

Definition at line 255 of file Elasticsearch.php.

259  {
260  $bulkArray = [
261  'index' => $indexName,
262  'type' => $this->clientConfig->getEntityType(),
263  'body' => [],
264  'refresh' => true,
265  ];
266 
267  foreach ($documents as $id => $document) {
268  $bulkArray['body'][] = [
269  $action => [
270  '_id' => $id,
271  '_type' => $this->clientConfig->getEntityType(),
272  '_index' => $indexName
273  ]
274  ];
275  if ($action == self::BULK_ACTION_INDEX) {
276  $bulkArray['body'][] = $document;
277  }
278  }
279 
280  return $bulkArray;
281  }
$id
Definition: fieldset.phtml:14

◆ ping()

ping ( )

Retrieve Elasticsearch server status

Returns
bool
Exceptions

Definition at line 131 of file Elasticsearch.php.

132  {
133  try {
134  $response = $this->client->ping();
135  } catch (\Exception $e) {
136  throw new \Magento\Framework\Exception\LocalizedException(
137  __('Could not ping search engine: %1', $e->getMessage())
138  );
139  }
140  return $response;
141  }
$response
Definition: 404.php:11
__()
Definition: __.php:13

◆ prepareDocsPerStore()

prepareDocsPerStore ( array  $documentData,
  $storeId 
)

Create Elasticsearch documents by specified data

Parameters
array$documentData
int$storeId
Returns
array

Definition at line 150 of file Elasticsearch.php.

151  {
152  $documents = [];
153  if (count($documentData)) {
154  $documents = $this->batchDocumentDataMapper->map(
155  $documentData,
156  $storeId
157  );
158  }
159  return $documents;
160  }

◆ prepareIndex()

prepareIndex (   $storeId,
  $indexName,
  $mappedIndexerId 
)
protected

Create new index with mapping.

Parameters
int$storeId
string$indexName
string$mappedIndexerId
Returns
$this

Definition at line 352 of file Elasticsearch.php.

353  {
354  $this->indexBuilder->setStoreId($storeId);
355  $settings = $this->indexBuilder->build();
356  $allAttributeTypes = $this->fieldMapper->getAllAttributesTypes([
357  'entityType' => $mappedIndexerId,
358  // Use store id instead of website id from context for save existing fields mapping.
359  // In future websiteId will be eliminated due to index stored per store
360  'websiteId' => $storeId
361  ]);
362  $settings['index']['mapping']['total_fields']['limit'] = $this->getMappingTotalFieldsLimit($allAttributeTypes);
363  $this->client->createIndex($indexName, ['settings' => $settings]);
364  $this->client->addFieldsMapping(
365  $allAttributeTypes,
366  $indexName,
367  $this->clientConfig->getEntityType()
368  );
369  $this->preparedIndex[$storeId] = $indexName;
370  return $this;
371  }
$settings
Definition: bootstrap.php:29

◆ updateAlias()

updateAlias (   $storeId,
  $mappedIndexerId 
)

Update Elasticsearch alias for new index.

Parameters
int$storeId
string$mappedIndexerId
Returns
$this

Definition at line 319 of file Elasticsearch.php.

320  {
321  if (!isset($this->preparedIndex[$storeId])) {
322  return $this;
323  }
324 
325  $oldIndex = $this->indexNameResolver->getIndexFromAlias($storeId, $mappedIndexerId);
326  if ($oldIndex == $this->preparedIndex[$storeId]) {
327  $oldIndex = '';
328  }
329 
330  $this->client->updateAlias(
331  $this->indexNameResolver->getIndexNameForAlias($storeId, $mappedIndexerId),
332  $this->preparedIndex[$storeId],
333  $oldIndex
334  );
335 
336  // remove obsolete index
337  if ($oldIndex) {
338  $this->client->deleteIndex($oldIndex);
339  }
340 
341  return $this;
342  }

Field Documentation

◆ $client

$client
protected

Definition at line 58 of file Elasticsearch.php.

◆ $clientConfig

$clientConfig
protected

Definition at line 53 of file Elasticsearch.php.

◆ $connectionManager

$connectionManager
protected

#-

Definition at line 32 of file Elasticsearch.php.

◆ $documentDataMapper

$documentDataMapper
protected

Definition at line 38 of file Elasticsearch.php.

◆ $fieldMapper

$fieldMapper
protected

Definition at line 48 of file Elasticsearch.php.

◆ $indexBuilder

$indexBuilder
protected

Definition at line 63 of file Elasticsearch.php.

◆ $indexNameResolver

$indexNameResolver
protected

Definition at line 43 of file Elasticsearch.php.

◆ $logger

$logger
protected

Definition at line 68 of file Elasticsearch.php.

◆ $preparedIndex

$preparedIndex = []
protected

Definition at line 73 of file Elasticsearch.php.

◆ BULK_ACTION_CREATE

const BULK_ACTION_CREATE = 'create'

Definition at line 21 of file Elasticsearch.php.

◆ BULK_ACTION_DELETE

const BULK_ACTION_DELETE = 'delete'

Definition at line 22 of file Elasticsearch.php.

◆ BULK_ACTION_INDEX

const BULK_ACTION_INDEX = 'index'

#+ Text flags for Elasticsearch bulk actions

Definition at line 20 of file Elasticsearch.php.

◆ BULK_ACTION_UPDATE

const BULK_ACTION_UPDATE = 'update'

Definition at line 23 of file Elasticsearch.php.


The documentation for this class was generated from the following file: