Skip to content

Commit 49f76a7

Browse files
authored
Override attributesToRetrieve config to ensure search result is complete (#751)
1 parent 9f64e8e commit 49f76a7

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Builder
7979
/**
8080
* Extra options that should be applied to the search.
8181
*
82-
* @var int
82+
* @var array
8383
*/
8484
public $options = [];
8585

src/Engines/MeilisearchEngine.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ protected function performSearch(Builder $builder, array $searchParams = [])
143143

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

146+
if (array_key_exists('attributesToRetrieve', $searchParams)) {
147+
$searchParams['attributesToRetrieve'] = array_merge(
148+
[$builder->model->getScoutKeyName()],
149+
$searchParams['attributesToRetrieve'],
150+
);
151+
}
152+
146153
if ($builder->callback) {
147154
$result = call_user_func(
148155
$builder->callback,

tests/Unit/MeilisearchEngineTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ public function test_search_sends_correct_parameters_to_meilisearch()
130130
$engine->search($builder);
131131
}
132132

133+
public function test_search_includes_at_least_scoutKeyName_in_attributesToRetrieve_on_builder_options()
134+
{
135+
$client = m::mock(Client::class);
136+
$client->shouldReceive('index')->with('table')->andReturn($index = m::mock(Indexes::class));
137+
$index->shouldReceive('search')->with('mustang', [
138+
'filter' => 'foo=1 AND bar=2',
139+
'attributesToRetrieve' => ['id', 'foo'],
140+
]);
141+
142+
$engine = new MeilisearchEngine($client);
143+
$builder = new Builder(new SearchableModel(), 'mustang', function ($meilisearch, $query, $options) {
144+
$options['filter'] = 'foo=1 AND bar=2';
145+
146+
return $meilisearch->search($query, $options);
147+
});
148+
$builder->options = ['attributesToRetrieve' => ['foo']];
149+
$engine->search($builder);
150+
}
151+
133152
public function test_submitting_a_callable_search_with_search_method_returns_array()
134153
{
135154
$builder = new Builder(

0 commit comments

Comments
 (0)