12
12
use Magento \CatalogSearch \Model \ResourceModel \Fulltext as FulltextResource ;
13
13
use Magento \Framework \App \ObjectManager ;
14
14
use Magento \Framework \Indexer \DimensionProviderInterface ;
15
+ use Magento \Framework \Indexer \SaveHandler \IndexerInterface ;
15
16
use Magento \Store \Model \StoreDimensionProvider ;
16
17
use Magento \Indexer \Model \ProcessManager ;
17
18
@@ -33,6 +34,11 @@ class Fulltext implements
33
34
*/
34
35
const INDEXER_ID = 'catalogsearch_fulltext ' ;
35
36
37
+ /**
38
+ * Default batch size
39
+ */
40
+ private const BATCH_SIZE = 100 ;
41
+
36
42
/**
37
43
* @var array index structure
38
44
*/
@@ -77,6 +83,11 @@ class Fulltext implements
77
83
*/
78
84
private $ processManager ;
79
85
86
+ /**
87
+ * @var int
88
+ */
89
+ private $ batchSize ;
90
+
80
91
/**
81
92
* @param FullFactory $fullActionFactory
82
93
* @param IndexerHandlerFactory $indexerHandlerFactory
@@ -86,6 +97,7 @@ class Fulltext implements
86
97
* @param DimensionProviderInterface $dimensionProvider
87
98
* @param array $data
88
99
* @param ProcessManager $processManager
100
+ * @param int|null $batchSize
89
101
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
90
102
*/
91
103
public function __construct (
@@ -96,7 +108,8 @@ public function __construct(
96
108
StateFactory $ indexScopeStateFactory ,
97
109
DimensionProviderInterface $ dimensionProvider ,
98
110
array $ data ,
99
- ProcessManager $ processManager = null
111
+ ProcessManager $ processManager = null ,
112
+ ?int $ batchSize = null
100
113
) {
101
114
$ this ->fullAction = $ fullActionFactory ->create (['data ' => $ data ]);
102
115
$ this ->indexerHandlerFactory = $ indexerHandlerFactory ;
@@ -106,6 +119,7 @@ public function __construct(
106
119
$ this ->indexScopeState = ObjectManager::getInstance ()->get (State::class);
107
120
$ this ->dimensionProvider = $ dimensionProvider ;
108
121
$ this ->processManager = $ processManager ?: ObjectManager::getInstance ()->get (ProcessManager::class);
122
+ $ this ->batchSize = $ batchSize ?? self ::BATCH_SIZE ;
109
123
}
110
124
111
125
/**
@@ -148,13 +162,42 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds =
148
162
} else {
149
163
// internal implementation works only with array
150
164
$ entityIds = iterator_to_array ($ entityIds );
151
- $ productIds = array_unique (
152
- array_merge ($ entityIds , $ this ->fulltextResource ->getRelationsByChild ($ entityIds ))
153
- );
154
- if ($ saveHandler ->isAvailable ($ dimensions )) {
155
- $ saveHandler ->deleteIndex ($ dimensions , new \ArrayIterator ($ productIds ));
156
- $ saveHandler ->saveIndex ($ dimensions , $ this ->fullAction ->rebuildStoreIndex ($ storeId , $ productIds ));
165
+ $ currentBatch = [];
166
+ $ i = 0 ;
167
+
168
+ foreach ($ entityIds as $ entityId ) {
169
+ $ currentBatch [] = $ entityId ;
170
+ if (++$ i === $ this ->batchSize ) {
171
+ $ this ->processBatch ($ saveHandler , $ dimensions , $ currentBatch );
172
+ $ i = 0 ;
173
+ $ currentBatch = [];
174
+ }
157
175
}
176
+ if (!empty ($ currentBatch )) {
177
+ $ this ->processBatch ($ saveHandler , $ dimensions , $ currentBatch );
178
+ }
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Process batch
184
+ *
185
+ * @param IndexerInterface $saveHandler
186
+ * @param array $dimensions
187
+ * @param array $entityIds
188
+ */
189
+ private function processBatch (
190
+ IndexerInterface $ saveHandler ,
191
+ array $ dimensions ,
192
+ array $ entityIds
193
+ ) : void {
194
+ $ storeId = $ dimensions [StoreDimensionProvider::DIMENSION_NAME ]->getValue ();
195
+ $ productIds = array_unique (
196
+ array_merge ($ entityIds , $ this ->fulltextResource ->getRelationsByChild ($ entityIds ))
197
+ );
198
+ if ($ saveHandler ->isAvailable ($ dimensions )) {
199
+ $ saveHandler ->deleteIndex ($ dimensions , new \ArrayIterator ($ productIds ));
200
+ $ saveHandler ->saveIndex ($ dimensions , $ this ->fullAction ->rebuildStoreIndex ($ storeId , $ productIds ));
158
201
}
159
202
}
160
203
0 commit comments