Skip to content

Commit

Permalink
feat: add clearAll flag (Fixes #55)
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Dec 11, 2022
1 parent befacf2 commit f6d57dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ import but only need the active ones you can trigger the task as follows:
If you do not have access to a CLI (i.e Silverstripe Cloud) then you can also
bulk reindex via a queued job `AlgoliaReindexAllJob`.

### Optional

`forceAll` forces every Silverstripe record to be re-synced.

```
./vendor/bin/sake dev/tasks/AlgoliaReindex "flush=1&forceAll=1"
```

`clearAll` truncates the search index before reindexing.

```
./vendor/bin/sake dev/tasks/AlgoliaReindex "flush=1&clearAll=1&forceAll=1"
```

### Customising the indexed attributes (fields)

Expand Down
12 changes: 11 additions & 1 deletion src/Tasks/AlgoliaReindex.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public function run($request)
$filter = 'AlgoliaIndexed IS NULL';
}

/** @var AlgoliaService */
$algoliaService = Injector::inst()->create(AlgoliaService::class);

if ($request->getVar('clearAll')) {
$indexes = $algoliaService->initIndexes();

foreach ($indexes as $indexName => $index) {
$index->clearObjects();
}
}

if ($targetClass) {
$items = $this->getItems($targetClass, $filter);

Expand All @@ -78,7 +89,6 @@ public function run($request)
);
}
} else {
$algoliaService = Injector::inst()->create(AlgoliaService::class);

// find all classes we have to index and do so
foreach ($algoliaService->indexes as $index) {
Expand Down

0 comments on commit f6d57dd

Please sign in to comment.