Skip to content

Commit 264a5ff

Browse files
Refactored the DeleteJob to use 'deleteObjects' instead of 'deleteBy' (#334)
* Refactored the DeleteJob to use 'deleteObjects' instead of 'deleteBy' This change is to account for the fact that the 'deleteBy' method has been deprecated on the new NeuralSearch infrastructure. We have been advised of this by the Algolia support team and they have stated that it's a requirement in order to use the new infrastructure. * Updated the PHPStan configuration to ignore the 'Model::searchableUsing()' method * Updated tests to account for the updated DeleteJob handling * Added a configuration option for whether or not to use the deprecated 'deleteBy' method
1 parent bf3d787 commit 264a5ff

File tree

5 files changed

+592
-1
lines changed

5 files changed

+592
-1
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ parameters:
66
- src
77
checkMissingIterableValueType: false
88
ignoreErrors:
9-
- '#Illuminate\\Database\\Eloquent\\Model::searchableAs\(\)#'
9+
- '#Illuminate\\Database\\Eloquent\\Model::(searchableAs|searchableUsing)\(\)#'
1010
- '#Algolia\\ScoutExtended\\ScoutExtendedServiceProvider::getModel\(\)#'
1111
- '#Unsafe usage of new static#'

src/Jobs/DeleteJob.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,66 @@ public function handle(SearchClient $client): void
5050
return;
5151
}
5252

53+
// Checking whether or not to use the deprecated `deleteBy` method
54+
// here instead of creating an second job for it so that any existing
55+
// integrations will still function even if they dispatch jobs manually
56+
// or extend this class.
57+
58+
// NOTE: Currently defaulting `scout.algolia.use_deprecated_delete_by` to
59+
// `true` so that there's no change to the existing behaviour.
60+
if (config('scout.algolia.use_deprecated_delete_by', true)) {
61+
$this->handleDeprecatedDeleteBy($client);
62+
} else {
63+
$this->handleDeleteObjects($client);
64+
}
65+
}
66+
67+
/**
68+
* Handle deleting objects.
69+
*
70+
* @param \Algolia\AlgoliaSearch\SearchClient $client
71+
* @return void
72+
*/
73+
protected function handleDeleteObjects(SearchClient $client)
74+
{
75+
$index = $client->initIndex($this->searchables->first()->searchableAs());
76+
77+
// First fetch all object IDs by tags.
78+
$objects = $index->browseObjects([
79+
'attributesToRetrieve' => [
80+
'objectID',
81+
],
82+
'tagFilters' => [
83+
$this->searchables->map(function ($searchable) {
84+
return ObjectIdEncrypter::encrypt($searchable);
85+
})->toArray(),
86+
],
87+
]);
88+
89+
// The ObjectIterator will fetch all pages for us automatically.
90+
$objectIds = [];
91+
foreach ($objects as $object) {
92+
if (isset($object['objectID'])) {
93+
$objectIds[] = $object['objectID'];
94+
}
95+
}
96+
97+
// Then delete the objects using their object IDs.
98+
$result = $index->deleteObjects($objectIds);
99+
100+
if (config('scout.synchronous', false)) {
101+
$result->wait();
102+
}
103+
}
104+
105+
/**
106+
* Handle deleting objects using the deprecated `deleteBy` method.
107+
*
108+
* @param \Algolia\AlgoliaSearch\SearchClient $client
109+
* @return void
110+
*/
111+
protected function handleDeprecatedDeleteBy(SearchClient $client)
112+
{
53113
$index = $client->initIndex($this->searchables->first()->searchableAs());
54114

55115
$result = $index->deleteBy([

0 commit comments

Comments
 (0)