|
13 | 13 | use SilverStripe\Core\Injector\Injector; |
14 | 14 | use SilverStripe\Forager\Exception\IndexConfigurationException; |
15 | 15 | use SilverStripe\Forager\Exception\IndexingServiceException; |
16 | | -use SilverStripe\Forager\Interfaces\BatchDocumentRemovalInterface; |
17 | 16 | use SilverStripe\Forager\Interfaces\DocumentInterface; |
18 | 17 | use SilverStripe\Forager\Interfaces\IndexingInterface; |
19 | 18 | use SilverStripe\Forager\Schema\Field; |
|
26 | 25 | use stdClass; |
27 | 26 | use Throwable; |
28 | 27 |
|
29 | | -class BifrostService implements IndexingInterface, BatchDocumentRemovalInterface |
| 28 | +class BifrostService implements IndexingInterface |
30 | 29 | { |
31 | 30 |
|
32 | 31 | use Configurable; |
@@ -168,56 +167,45 @@ public function removeDocuments(array $documents): array |
168 | 167 | } |
169 | 168 |
|
170 | 169 | /** |
171 | | - * Forcefully remove all documents from the provided index name. Batches the requests to Bifröst based upon the |
172 | | - * configured batch size, beginning at page 1 and continuing until the index is empty. |
173 | | - * |
174 | | - * @param string $indexSuffix The index name to remove all documents from |
175 | 170 | * @return int The total number of documents removed |
176 | 171 | */ |
177 | | - public function removeAllDocuments(string $indexSuffix): int |
| 172 | + public function clearIndexDocuments(string $indexSuffix, int $batchSize): int |
178 | 173 | { |
179 | 174 | $indexName = $this->getConfiguration()->environmentizeIndex($indexSuffix); |
180 | | - $cfg = $this->getConfiguration(); |
181 | 175 | $client = $this->getClient(); |
182 | 176 | $numDeleted = 0; |
183 | 177 |
|
184 | 178 | $pagination = new PaginationNoTotals(); |
185 | | - $pagination->setSize($cfg->getBatchSize()); |
| 179 | + $pagination->setSize($batchSize); |
186 | 180 | $pagination->setCurrent(1); |
187 | 181 |
|
188 | 182 | $request = new DocumentListRequest(); |
189 | 183 | $request->setPage($pagination); |
190 | 184 |
|
191 | 185 | $response = $client->documentsListPost($indexName, $request); |
192 | 186 |
|
193 | | - $results = $response->getResults() ?? []; |
| 187 | + $idsToRemove = []; |
194 | 188 |
|
195 | | - // Loop forever until we no longer get any results |
196 | | - while (count($results) > 0) { |
197 | | - $idsToRemove = []; |
| 189 | + // Create the list of indexed documents to remove |
| 190 | + foreach ($response->getResults() as $doc) { |
| 191 | + $idsToRemove[] = $doc['id']; |
| 192 | + } |
198 | 193 |
|
199 | | - // Create the list of indexed documents to remove |
200 | | - foreach ($response->getResults() as $doc) { |
201 | | - $idsToRemove[] = $doc['id']; |
202 | | - } |
| 194 | + if (!$idsToRemove) { |
| 195 | + return 0; |
| 196 | + } |
203 | 197 |
|
204 | | - // Actually delete the documents |
205 | | - $deletedDocs = $client->documentsDelete($indexName, $idsToRemove); |
| 198 | + // Actually delete the documents |
| 199 | + $deletedDocs = $client->documentsDelete($indexName, $idsToRemove); |
206 | 200 |
|
207 | | - // Keep an accurate running count of the number of documents deleted. |
208 | | - foreach ($deletedDocs as $doc) { |
209 | | - $deleted = $doc?->getDeleted() ?? false; |
| 201 | + // Keep an accurate running count of the number of documents deleted. |
| 202 | + foreach ($deletedDocs as $doc) { |
| 203 | + $deleted = $doc?->getDeleted() ?? false; |
210 | 204 |
|
211 | | - // phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
212 | | - if ($deleted) { |
213 | | - $numDeleted += 1; |
214 | | - } |
| 205 | + // phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
| 206 | + if ($deleted) { |
| 207 | + $numDeleted += 1; |
215 | 208 | } |
216 | | - |
217 | | - // Re-fetch $documents now that we've deleted this batch |
218 | | - $response = $client->documentsListPost($indexName, $request); |
219 | | - |
220 | | - $results = $response->getResults() ?? []; |
221 | 209 | } |
222 | 210 |
|
223 | 211 | return $numDeleted; |
|
0 commit comments