6
6
7
7
namespace Magento \Elasticsearch \Model \Adapter ;
8
8
9
- use Magento \Catalog \Api \Data \ProductAttributeInterface ;
9
+ use Elasticsearch \Common \Exceptions \Missing404Exception ;
10
+ use Magento \AdvancedSearch \Model \Client \ClientInterface ;
10
11
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
11
12
use 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 ;
12
17
use Magento \Framework \App \ObjectManager ;
18
+ use Magento \Framework \Exception \LocalizedException ;
13
19
use Magento \Framework \Stdlib \ArrayManager ;
20
+ use Psr \Log \LoggerInterface ;
14
21
15
22
/**
16
23
* Elasticsearch adapter
@@ -36,7 +43,7 @@ class Elasticsearch
36
43
protected $ connectionManager ;
37
44
38
45
/**
39
- * @var \Magento\Elasticsearch\Model\Adapter\Index\ IndexNameResolver
46
+ * @var IndexNameResolver
40
47
*/
41
48
protected $ indexNameResolver ;
42
49
@@ -46,22 +53,22 @@ class Elasticsearch
46
53
protected $ fieldMapper ;
47
54
48
55
/**
49
- * @var \Magento\Elasticsearch\Model\ Config
56
+ * @var Config
50
57
*/
51
58
protected $ clientConfig ;
52
59
53
60
/**
54
- * @var \Magento\AdvancedSearch\Model\Client\ ClientInterface
61
+ * @var ClientInterface
55
62
*/
56
63
protected $ client ;
57
64
58
65
/**
59
- * @var \Magento\Elasticsearch\Model\Adapter\Index\ BuilderInterface
66
+ * @var BuilderInterface
60
67
*/
61
68
protected $ indexBuilder ;
62
69
63
70
/**
64
- * @var \Psr\Log\ LoggerInterface
71
+ * @var LoggerInterface
65
72
*/
66
73
protected $ logger ;
67
74
@@ -101,27 +108,27 @@ class Elasticsearch
101
108
private $ arrayManager ;
102
109
103
110
/**
104
- * @param \Magento\Elasticsearch\SearchAdapter\ ConnectionManager $connectionManager
111
+ * @param ConnectionManager $connectionManager
105
112
* @param FieldMapperInterface $fieldMapper
106
- * @param \Magento\Elasticsearch\Model\ Config $clientConfig
113
+ * @param Config $clientConfig
107
114
* @param Index\BuilderInterface $indexBuilder
108
- * @param \Psr\Log\ LoggerInterface $logger
115
+ * @param LoggerInterface $logger
109
116
* @param Index\IndexNameResolver $indexNameResolver
110
117
* @param BatchDataMapperInterface $batchDocumentDataMapper
111
118
* @param array $options
112
119
* @param ProductAttributeRepositoryInterface|null $productAttributeRepository
113
120
* @param StaticField|null $staticFieldProvider
114
121
* @param ArrayManager|null $arrayManager
115
- * @throws \Magento\Framework\Exception\ LocalizedException
122
+ * @throws LocalizedException
116
123
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117
124
*/
118
125
public function __construct (
119
- \ Magento \ Elasticsearch \ SearchAdapter \ ConnectionManager $ connectionManager ,
126
+ ConnectionManager $ connectionManager ,
120
127
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 ,
125
132
BatchDataMapperInterface $ batchDocumentDataMapper ,
126
133
$ options = [],
127
134
ProductAttributeRepositoryInterface $ productAttributeRepository = null ,
@@ -146,7 +153,7 @@ public function __construct(
146
153
$ this ->client = $ this ->connectionManager ->getConnection ($ options );
147
154
} catch (\Exception $ e ) {
148
155
$ this ->logger ->critical ($ e );
149
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
156
+ throw new LocalizedException (
150
157
__ ('The search failed because of a search engine misconfiguration. ' )
151
158
);
152
159
}
@@ -156,14 +163,14 @@ public function __construct(
156
163
* Retrieve Elasticsearch server status
157
164
*
158
165
* @return bool
159
- * @throws \Magento\Framework\Exception\ LocalizedException
166
+ * @throws LocalizedException
160
167
*/
161
168
public function ping ()
162
169
{
163
170
try {
164
171
$ response = $ this ->client ->ping ();
165
172
} catch (\Exception $ e ) {
166
- throw new \ Magento \ Framework \ Exception \ LocalizedException (
173
+ throw new LocalizedException (
167
174
__ ('Could not ping search engine: %1 ' , $ e ->getMessage ())
168
175
);
169
176
}
@@ -387,22 +394,12 @@ public function updateIndexMapping(int $storeId, string $mappedIndexerId, string
387
394
return $ this ;
388
395
}
389
396
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 );
406
403
}
407
404
408
405
return $ this ;
@@ -505,4 +502,31 @@ private function getMappingTotalFieldsLimit(array $allAttributeTypes): int
505
502
}
506
503
return $ count + self ::MAPPING_TOTAL_FIELDS_BUFFER_LIMIT ;
507
504
}
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
+ }
508
532
}
0 commit comments