Skip to content

Commit b12b40e

Browse files
committed
Updated interface implementation
1 parent c32d49c commit b12b40e

2 files changed

Lines changed: 28 additions & 40 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This module **does not** provide any method for performing searches on your engi
99
* [Installation](#installation)
1010
* [Engine vs Index](#engine-vs-index)
1111
* [Specify environment variables](#specify-environment-variables)
12-
* [Understanding your engine prefix and suffix:](#understanding-your-engine-prefix-and-suffix)
12+
* [Understanding your engine prefix and engine suffix:](#understanding-your-engine-prefix-and-engine-suffix)
1313
* [Configuration](#configuration)
1414
* [File attachments for content extraction](#file-attachments-for-content-extraction)
1515
* [Additional documentation](#additional-documentation)
@@ -40,20 +40,20 @@ To integrate with Silverstripe Search, define environment variables containing y
4040

4141
```
4242
BIFROST_ENDPOINT="https://abc.provided.domain"
43-
BIFROST_ENGINE_PREFIX="<engine-prefix>" # See "Understanding your engine prefix and suffix" below
43+
BIFROST_ENGINE_PREFIX="<enginePrefix>" # See "Understanding your engine prefix and engine suffix" below
4444
BIFROST_MANAGEMENT_API_KEY="abc.123.xyz"
4545
```
4646

47-
### Understanding your engine prefix and suffix:
47+
### Understanding your engine prefix and engine suffix:
4848

4949
> [!IMPORTANT]
5050
> **TL;DR:**
51-
> - All Silverstripe Search engine names follow a 4 slug format like this: `search-<subscription>-<environment>-<suffix>`
52-
> - Your `<engine-prefix>` is everything except `-<suffix>`; so, it's just `search-<subscription>-<environment>`
51+
> - All Silverstripe Search engine names follow a 4 slug format like this: `search-<subscription>-<environment>-<engineSuffix>`
52+
> - Your `<enginePrefix>` is everything except `-<engineSuffix>`; so, it's just `search-<subscription>-<environment>`
5353
5454
For example:
5555

56-
| Engine | Engine prefix | Engine suffix |
56+
| Engine name | Engine prefix | Engine suffix |
5757
|---------------------------|----------------------|---------------|
5858
| search-acmecorp-prod-main | search-acmecorp-prod | main |
5959
| search-acmecorp-prod-inc | search-acmecorp-prod | inc |
@@ -64,7 +64,7 @@ For example:
6464

6565
Because you probably have more than one environment type that you're running search on (e.g. Production and UAT), and (generally speaking) you should have different engines for each of those environments. So, you can't just hardcode the entire engine name into your project, because that code doesn't change between environments.
6666

67-
Whenever you make a query, Forager will ask you for the "index" name; you will actually want to provide only the `<suffix>`. We will then take `BIFROST_ENGINE_PREFIX` and your `<suffix>`, put them together, and that's what will be queried. This allows you to set `BIFROST_ENGINE_PREFIX` differently for each environment, while having your `<suffix>` hardcoded in your project.
67+
Whenever you make a query, Forager will ask you for the "index" name; you will actually want to provide only the `<engineSuffix>`. We will then take `BIFROST_ENGINE_PREFIX` and your `<engineSuffix>`, put them together, and that's what will be queried. This allows you to set `BIFROST_ENGINE_PREFIX` differently for each environment, while having your `<engineSuffix>` hardcoded in your project.
6868

6969
## Configuration
7070

@@ -84,7 +84,7 @@ You can specify these data types in the `options` node of your fields.
8484
```yaml
8585
SilverStripe\Forager\Service\IndexConfiguration:
8686
indexes:
87-
<suffix>:
87+
<engineSuffix>:
8888
includeClasses:
8989
SilverStripe\CMS\Model\SiteTree:
9090
fields:
@@ -123,7 +123,7 @@ This field needs to contain a base 64 encoded string of binary for the file you
123123
```yaml
124124
SilverStripe\Forager\Service\IndexConfiguration:
125125
indexes:
126-
<suffix>:
126+
<engineSuffix>:
127127
includeClasses:
128128
SilverStripe\Assets\File:
129129
fields:

src/Service/BifrostService.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use SilverStripe\Core\Injector\Injector;
1414
use SilverStripe\Forager\Exception\IndexConfigurationException;
1515
use SilverStripe\Forager\Exception\IndexingServiceException;
16-
use SilverStripe\Forager\Interfaces\BatchDocumentRemovalInterface;
1716
use SilverStripe\Forager\Interfaces\DocumentInterface;
1817
use SilverStripe\Forager\Interfaces\IndexingInterface;
1918
use SilverStripe\Forager\Schema\Field;
@@ -26,7 +25,7 @@
2625
use stdClass;
2726
use Throwable;
2827

29-
class BifrostService implements IndexingInterface, BatchDocumentRemovalInterface
28+
class BifrostService implements IndexingInterface
3029
{
3130

3231
use Configurable;
@@ -168,56 +167,45 @@ public function removeDocuments(array $documents): array
168167
}
169168

170169
/**
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
175170
* @return int The total number of documents removed
176171
*/
177-
public function removeAllDocuments(string $indexSuffix): int
172+
public function clearIndexDocuments(string $indexSuffix, int $batchSize): int
178173
{
179174
$indexName = $this->getConfiguration()->environmentizeIndex($indexSuffix);
180-
$cfg = $this->getConfiguration();
181175
$client = $this->getClient();
182176
$numDeleted = 0;
183177

184178
$pagination = new PaginationNoTotals();
185-
$pagination->setSize($cfg->getBatchSize());
179+
$pagination->setSize($batchSize);
186180
$pagination->setCurrent(1);
187181

188182
$request = new DocumentListRequest();
189183
$request->setPage($pagination);
190184

191185
$response = $client->documentsListPost($indexName, $request);
192186

193-
$results = $response->getResults() ?? [];
187+
$idsToRemove = [];
194188

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+
}
198193

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+
}
203197

204-
// Actually delete the documents
205-
$deletedDocs = $client->documentsDelete($indexName, $idsToRemove);
198+
// Actually delete the documents
199+
$deletedDocs = $client->documentsDelete($indexName, $idsToRemove);
206200

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;
210204

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;
215208
}
216-
217-
// Re-fetch $documents now that we've deleted this batch
218-
$response = $client->documentsListPost($indexName, $request);
219-
220-
$results = $response->getResults() ?? [];
221209
}
222210

223211
return $numDeleted;

0 commit comments

Comments
 (0)