Skip to content

Commit

Permalink
Override attributesToRetrieve config to ensure search result is compl…
Browse files Browse the repository at this point in the history
…ete (#751)
  • Loading branch information
mmachatschek authored Jul 4, 2023
1 parent 9f64e8e commit 49f76a7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Builder
/**
* Extra options that should be applied to the search.
*
* @var int
* @var array
*/
public $options = [];

Expand Down
7 changes: 7 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ protected function performSearch(Builder $builder, array $searchParams = [])

$searchParams = array_merge($builder->options, $searchParams);

if (array_key_exists('attributesToRetrieve', $searchParams)) {
$searchParams['attributesToRetrieve'] = array_merge(
[$builder->model->getScoutKeyName()],
$searchParams['attributesToRetrieve'],
);
}

if ($builder->callback) {
$result = call_user_func(
$builder->callback,
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ public function test_search_sends_correct_parameters_to_meilisearch()
$engine->search($builder);
}

public function test_search_includes_at_least_scoutKeyName_in_attributesToRetrieve_on_builder_options()
{
$client = m::mock(Client::class);
$client->shouldReceive('index')->with('table')->andReturn($index = m::mock(Indexes::class));
$index->shouldReceive('search')->with('mustang', [
'filter' => 'foo=1 AND bar=2',
'attributesToRetrieve' => ['id', 'foo'],
]);

$engine = new MeilisearchEngine($client);
$builder = new Builder(new SearchableModel(), 'mustang', function ($meilisearch, $query, $options) {
$options['filter'] = 'foo=1 AND bar=2';

return $meilisearch->search($query, $options);
});
$builder->options = ['attributesToRetrieve' => ['foo']];
$engine->search($builder);
}

public function test_submitting_a_callable_search_with_search_method_returns_array()
{
$builder = new Builder(
Expand Down

0 comments on commit 49f76a7

Please sign in to comment.