29 private const MAPPING_TOTAL_FIELDS_BUFFER_LIMIT = 1000;
78 private $batchDocumentDataMapper;
100 \Psr\Log\LoggerInterface
$logger,
112 $this->batchDocumentDataMapper = $batchDocumentDataMapper ?:
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.')
135 }
catch (\Exception $e) {
136 throw new \Magento\Framework\Exception\LocalizedException(
137 __(
'Could not ping search engine: %1', $e->getMessage())
153 if (count($documentData)) {
154 $documents = $this->batchDocumentDataMapper->map(
173 if (count($documents)) {
175 $indexName = $this->indexNameResolver->getIndexName(
$storeId, $mappedIndexerId, $this->preparedIndex);
177 $this->client->bulkQuery($bulkIndexDocuments);
178 }
catch (\Exception $e) {
179 $this->logger->critical($e);
197 $indexName = $this->indexNameResolver->getIndexName(
$storeId, $mappedIndexerId, $this->preparedIndex);
198 if ($this->client->isEmptyIndex($indexName)) {
204 $indexPattern = $this->indexNameResolver->getIndexPattern(
$storeId, $mappedIndexerId);
205 $version = intval(str_replace($indexPattern,
'', $indexName));
206 $newIndexName = $indexPattern . ++
$version;
209 if ($this->client->indexExists($newIndexName)) {
210 $this->client->deleteIndex($newIndexName);
232 $indexName = $this->indexNameResolver->getIndexName(
$storeId, $mappedIndexerId, $this->preparedIndex);
236 self::BULK_ACTION_DELETE
238 $this->client->bulkQuery($bulkDeleteDocuments);
239 }
catch (\Exception $e) {
240 $this->logger->critical($e);
258 $action = self::BULK_ACTION_INDEX
261 'index' => $indexName,
262 'type' => $this->clientConfig->getEntityType(),
267 foreach ($documents as
$id => $document) {
268 $bulkArray[
'body'][] = [
271 '_type' => $this->clientConfig->getEntityType(),
272 '_index' => $indexName
275 if ($action == self::BULK_ACTION_INDEX) {
276 $bulkArray[
'body'][] = $document;
297 $indexName = $this->indexNameResolver->getIndexName(
$storeId, $mappedIndexerId, $this->preparedIndex);
298 if (!$this->client->indexExists($indexName)) {
304 $namespace = $this->indexNameResolver->getIndexNameForAlias(
$storeId, $mappedIndexerId);
305 if (!$this->client->existsAlias($namespace, $indexName)) {
306 $this->client->updateAlias($namespace, $indexName);
321 if (!isset($this->preparedIndex[
$storeId])) {
325 $oldIndex = $this->indexNameResolver->getIndexFromAlias(
$storeId, $mappedIndexerId);
326 if ($oldIndex == $this->preparedIndex[
$storeId]) {
330 $this->client->updateAlias(
331 $this->indexNameResolver->getIndexNameForAlias(
$storeId, $mappedIndexerId),
338 $this->client->deleteIndex($oldIndex);
354 $this->indexBuilder->setStoreId(
$storeId);
355 $settings = $this->indexBuilder->build();
356 $allAttributeTypes = $this->fieldMapper->getAllAttributesTypes([
357 'entityType' => $mappedIndexerId,
362 $settings[
'index'][
'mapping'][
'total_fields'][
'limit'] = $this->getMappingTotalFieldsLimit($allAttributeTypes);
363 $this->client->createIndex($indexName, [
'settings' =>
$settings]);
364 $this->client->addFieldsMapping(
367 $this->clientConfig->getEntityType()
369 $this->preparedIndex[
$storeId] = $indexName;
379 private function getMappingTotalFieldsLimit(array $allAttributeTypes): int
381 return count($allAttributeTypes) + self::MAPPING_TOTAL_FIELDS_BUFFER_LIMIT;
deleteDocs(array $documentIds, $storeId, $mappedIndexerId)
prepareDocsPerStore(array $documentData, $storeId)
updateAlias($storeId, $mappedIndexerId)
addDocs(array $documents, $storeId, $mappedIndexerId)
checkIndex( $storeId, $mappedIndexerId, $checkAlias=true)
cleanIndex($storeId, $mappedIndexerId)
prepareIndex($storeId, $indexName, $mappedIndexerId)
getDocsArrayInBulkIndexFormat( $documents, $indexName, $action=self::BULK_ACTION_INDEX)
__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)