diff --git a/phpstan.neon b/phpstan.neon index 3298475..5c8fe75 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -19,7 +19,6 @@ parameters: - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model::scoutMetadata\(\).#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model::searchableAs\(\).#' - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model::toSearchableArray\(\).#' - - '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder::unsearchable\(\).#' - '#Call to an undefined method Illuminate\\Support\\HigherOrderCollectionProxy::pushSoftDeleteMetadata\(\).#' - '#Call to an undefined method Illuminate\\Support\\HigherOrderCollectionProxy::getScoutKey\(\).#' - '#Parameter \#1 ...\$arrays of function array_merge expects array, int given.#' diff --git a/src/Engines/OpenSearchEngine.php b/src/Engines/OpenSearchEngine.php index 0bd56fc..f50f32c 100644 --- a/src/Engines/OpenSearchEngine.php +++ b/src/Engines/OpenSearchEngine.php @@ -285,9 +285,14 @@ public function getTotalCount($results): int */ public function flush($model): void { - $model->newQuery() - ->orderBy($model->getKeyName()) - ->unsearchable(); + $this->client->deleteByQuery([ + 'index' => $model->searchableAs(), + 'body' => [ + 'query' => [ + 'match_all' => new \stdClass(), + ], + ], + ]); } /** diff --git a/tests/OpenSearchEngineTest.php b/tests/OpenSearchEngineTest.php index 1f3f70a..a8a446b 100644 --- a/tests/OpenSearchEngineTest.php +++ b/tests/OpenSearchEngineTest.php @@ -14,7 +14,6 @@ use Laravel\Scout\Jobs\RemoveFromSearch; use Mockery as m; use OpenSearch\Client; -use OpenSearch\ClientBuilder; use Zing\LaravelScout\OpenSearch\Engines\OpenSearchEngine; use Zing\LaravelScout\OpenSearch\Tests\Fixtures\CustomKeySearchableModel; use Zing\LaravelScout\OpenSearch\Tests\Fixtures\EmptySearchableModel; @@ -572,19 +571,23 @@ public function testAModelIsRemovedWithACustomAlgoliaKey(): void public function testFlushAModelWithACustomAlgoliaKey(): void { - $builder = m::mock(Builder::class); - $builder->shouldReceive('unsearchable') - ->once() - ->withNoArgs(); $model = m::mock(CustomKeySearchableModel::class); - $model->shouldReceive('getKeyName') + $model->shouldReceive('searchableAs') + ->once() ->withNoArgs() ->andReturn('table'); - $model->shouldReceive('newQuery->orderBy') - ->with('table') - ->andReturn($builder); - - $engine = new OpenSearchEngine(ClientBuilder::fromConfig([])); + $client = m::mock(Client::class); + $client->shouldReceive('deleteByQuery') + ->with([ + 'index' => 'table', + 'body' => [ + 'query' => [ + 'match_all' => new \stdClass(), + ], + ], + ]) + ->andReturn('table'); + $engine = new OpenSearchEngine($client); $engine->flush($model); }