Skip to content

Commit

Permalink
Ignore metadata when skipping records
Browse files Browse the repository at this point in the history
When skipping records during updates on Algolia items, we should ignore any metadata as this shouldn't be a factor on wether to index or not. Only records which have at least one key value pair for indexing should be indexed.

This fixes a bug where empty records with soft deleted enabled were still being updated: laravel#350
  • Loading branch information
driesvints committed Apr 1, 2019
1 parent 185c6f3 commit a76269e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public function update($models)
}

$objects = $models->map(function ($model) {
$array = array_merge(
$model->toSearchableArray(), $model->scoutMetadata()
);

if (empty($array)) {
if (empty($searchableData = $model->toSearchableArray())) {
return;
}

return array_merge(['objectID' => $model->getScoutKey()], $array);
return array_merge(
['objectID' => $model->getScoutKey()],
$searchableData,
$model->scoutMetadata()
);
})->filter()->values()->all();

if (! empty($objects)) {
Expand Down
28 changes: 28 additions & 0 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,32 @@ public function test_update_empty_searchable_array_does_not_add_objects_to_index
$engine = new AlgoliaEngine($client);
$engine->update(Collection::make([new EmptyTestModel]));
}

public function test_update_empty_searchable_array_from_soft_deleted_model_does_not_add_objects_to_index()
{
$client = m::mock('Algolia\AlgoliaSearch\SearchClient');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = m::mock('StdClass'));
$index->shouldNotReceive('saveObjects');

$engine = new AlgoliaEngine($client, true);
$engine->update(Collection::make([new SoftDeletedEmptySearchableModel]));
}
}

class SoftDeletedEmptySearchableModel extends EmptyTestModel
{
public function toSearchableArray()
{
return [];
}

public function pushSoftDeleteMetadata()
{
//
}

public function scoutMetadata()
{
return ['__soft_deleted' => 1];
}
}

0 comments on commit a76269e

Please sign in to comment.