Skip to content

Commit

Permalink
Flush records by deleteByQuery (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
zingimmick committed Jun 1, 2023
1 parent 016df26 commit 96573c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<Illuminate\\Database\\Eloquent\\Model>::unsearchable\(\).#'
- '#Call to an undefined method Illuminate\\Support\\HigherOrderCollectionProxy<string, Illuminate\\Database\\Eloquent\\Model, Illuminate\\Database\\Eloquent\\Collection>::pushSoftDeleteMetadata\(\).#'
- '#Call to an undefined method Illuminate\\Support\\HigherOrderCollectionProxy<string, Illuminate\\Database\\Eloquent\\Model, Illuminate\\Database\\Eloquent\\Collection>::getScoutKey\(\).#'
- '#Parameter \#1 ...\$arrays of function array_merge expects array, int given.#'
11 changes: 8 additions & 3 deletions src/Engines/OpenSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
],
]);
}

/**
Expand Down
25 changes: 14 additions & 11 deletions tests/OpenSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 96573c6

Please sign in to comment.