66
77namespace Magento \Elasticsearch \Model \Adapter ;
88
9- use Magento \Catalog \Api \Data \ProductAttributeInterface ;
9+ use Elasticsearch \Common \Exceptions \Missing404Exception ;
10+ use Magento \AdvancedSearch \Model \Client \ClientInterface ;
1011use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
1112use Magento \Elasticsearch \Model \Adapter \FieldMapper \Product \FieldProvider \StaticField ;
13+ use Magento \Elasticsearch \Model \Adapter \Index \BuilderInterface ;
14+ use Magento \Elasticsearch \Model \Adapter \Index \IndexNameResolver ;
15+ use Magento \Elasticsearch \Model \Config ;
16+ use Magento \Elasticsearch \SearchAdapter \ConnectionManager ;
1217use Magento \Framework \App \ObjectManager ;
18+ use Magento \Framework \Exception \LocalizedException ;
1319use Magento \Framework \Stdlib \ArrayManager ;
20+ use Psr \Log \LoggerInterface ;
1421
1522/**
1623 * Elasticsearch adapter
@@ -36,7 +43,7 @@ class Elasticsearch
3643 protected $ connectionManager ;
3744
3845 /**
39- * @var \Magento\Elasticsearch\Model\Adapter\Index\ IndexNameResolver
46+ * @var IndexNameResolver
4047 */
4148 protected $ indexNameResolver ;
4249
@@ -46,22 +53,22 @@ class Elasticsearch
4653 protected $ fieldMapper ;
4754
4855 /**
49- * @var \Magento\Elasticsearch\Model\ Config
56+ * @var Config
5057 */
5158 protected $ clientConfig ;
5259
5360 /**
54- * @var \Magento\AdvancedSearch\Model\Client\ ClientInterface
61+ * @var ClientInterface
5562 */
5663 protected $ client ;
5764
5865 /**
59- * @var \Magento\Elasticsearch\Model\Adapter\Index\ BuilderInterface
66+ * @var BuilderInterface
6067 */
6168 protected $ indexBuilder ;
6269
6370 /**
64- * @var \Psr\Log\ LoggerInterface
71+ * @var LoggerInterface
6572 */
6673 protected $ logger ;
6774
@@ -101,27 +108,27 @@ class Elasticsearch
101108 private $ arrayManager ;
102109
103110 /**
104- * @param \Magento\Elasticsearch\SearchAdapter\ ConnectionManager $connectionManager
111+ * @param ConnectionManager $connectionManager
105112 * @param FieldMapperInterface $fieldMapper
106- * @param \Magento\Elasticsearch\Model\ Config $clientConfig
113+ * @param Config $clientConfig
107114 * @param Index\BuilderInterface $indexBuilder
108- * @param \Psr\Log\ LoggerInterface $logger
115+ * @param LoggerInterface $logger
109116 * @param Index\IndexNameResolver $indexNameResolver
110117 * @param BatchDataMapperInterface $batchDocumentDataMapper
111118 * @param array $options
112119 * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
113120 * @param StaticField|null $staticFieldProvider
114121 * @param ArrayManager|null $arrayManager
115- * @throws \Magento\Framework\Exception\ LocalizedException
122+ * @throws LocalizedException
116123 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
117124 */
118125 public function __construct (
119- \ Magento \ Elasticsearch \ SearchAdapter \ ConnectionManager $ connectionManager ,
126+ ConnectionManager $ connectionManager ,
120127 FieldMapperInterface $ fieldMapper ,
121- \ Magento \ Elasticsearch \ Model \ Config $ clientConfig ,
122- \ Magento \ Elasticsearch \ Model \ Adapter \ Index \ BuilderInterface $ indexBuilder ,
123- \ Psr \ Log \ LoggerInterface $ logger ,
124- \ Magento \ Elasticsearch \ Model \ Adapter \ Index \ IndexNameResolver $ indexNameResolver ,
128+ Config $ clientConfig ,
129+ BuilderInterface $ indexBuilder ,
130+ LoggerInterface $ logger ,
131+ IndexNameResolver $ indexNameResolver ,
125132 BatchDataMapperInterface $ batchDocumentDataMapper ,
126133 $ options = [],
127134 ProductAttributeRepositoryInterface $ productAttributeRepository = null ,
@@ -146,7 +153,7 @@ public function __construct(
146153 $ this ->client = $ this ->connectionManager ->getConnection ($ options );
147154 } catch (\Exception $ e ) {
148155 $ this ->logger ->critical ($ e );
149- throw new \ Magento \ Framework \ Exception \ LocalizedException (
156+ throw new LocalizedException (
150157 __ ('The search failed because of a search engine misconfiguration. ' )
151158 );
152159 }
@@ -156,14 +163,14 @@ public function __construct(
156163 * Retrieve Elasticsearch server status
157164 *
158165 * @return bool
159- * @throws \Magento\Framework\Exception\ LocalizedException
166+ * @throws LocalizedException
160167 */
161168 public function ping ()
162169 {
163170 try {
164171 $ response = $ this ->client ->ping ();
165172 } catch (\Exception $ e ) {
166- throw new \ Magento \ Framework \ Exception \ LocalizedException (
173+ throw new LocalizedException (
167174 __ ('Could not ping search engine: %1 ' , $ e ->getMessage ())
168175 );
169176 }
@@ -387,22 +394,12 @@ public function updateIndexMapping(int $storeId, string $mappedIndexerId, string
387394 return $ this ;
388395 }
389396
390- $ attribute = $ this ->productAttributeRepository ->get ($ attributeCode );
391- $ newAttributeMapping = $ this ->staticFieldProvider ->getField ($ attribute );
392- $ mappedAttributes = $ this ->getMappedAttributes ($ indexName );
393-
394- $ attrToUpdate = array_diff_key ($ newAttributeMapping , $ mappedAttributes );
395- if (!empty ($ attrToUpdate )) {
396- $ settings ['index ' ]['mapping ' ]['total_fields ' ]['limit ' ] = $ this
397- ->getMappingTotalFieldsLimit (array_merge ($ mappedAttributes , $ attrToUpdate ));
398- $ this ->client ->putIndexSettings ($ indexName , ['settings ' => $ settings ]);
399-
400- $ this ->client ->addFieldsMapping (
401- $ attrToUpdate ,
402- $ indexName ,
403- $ this ->clientConfig ->getEntityType ()
404- );
405- $ this ->setMappedAttributes ($ indexName , $ attrToUpdate );
397+ try {
398+ $ this ->updateMapping ($ attributeCode , $ indexName );
399+ } catch (Missing404Exception $ e ) {
400+ unset($ this ->indexByCode [$ mappedIndexerId . '_ ' . $ storeId ]);
401+ $ indexName = $ this ->getIndexFromAlias ($ storeId , $ mappedIndexerId );
402+ $ this ->updateMapping ($ attributeCode , $ indexName );
406403 }
407404
408405 return $ this ;
@@ -505,4 +502,31 @@ private function getMappingTotalFieldsLimit(array $allAttributeTypes): int
505502 }
506503 return $ count + self ::MAPPING_TOTAL_FIELDS_BUFFER_LIMIT ;
507504 }
505+
506+ /**
507+ * Perform index mapping update
508+ *
509+ * @param string $attributeCode
510+ * @param string $indexName
511+ * @return void
512+ */
513+ private function updateMapping (string $ attributeCode , string $ indexName ): void
514+ {
515+ $ attribute = $ this ->productAttributeRepository ->get ($ attributeCode );
516+ $ newAttributeMapping = $ this ->staticFieldProvider ->getField ($ attribute );
517+ $ mappedAttributes = $ this ->getMappedAttributes ($ indexName );
518+ $ attrToUpdate = array_diff_key ($ newAttributeMapping , $ mappedAttributes );
519+ if (!empty ($ attrToUpdate )) {
520+ $ settings ['index ' ]['mapping ' ]['total_fields ' ]['limit ' ] = $ this
521+ ->getMappingTotalFieldsLimit (array_merge ($ mappedAttributes , $ attrToUpdate ));
522+ $ this ->client ->putIndexSettings ($ indexName , ['settings ' => $ settings ]);
523+
524+ $ this ->client ->addFieldsMapping (
525+ $ attrToUpdate ,
526+ $ indexName ,
527+ $ this ->clientConfig ->getEntityType ()
528+ );
529+ $ this ->setMappedAttributes ($ indexName , $ attrToUpdate );
530+ }
531+ }
508532}
0 commit comments