Skip to content

Commit 85bc852

Browse files
kronthtoErickTamayo
authored andcommitted
Execute the callback that can be passed to the Builder in performSearch (ErickTamayo#56)
Context: laravel/scout#111 Implementation analogical to the official AlgoliaEngine implementation: https://github.com/laravel/scout/blob/v3.0.3/src/Engines/AlgoliaEngine.php#L112-L119
1 parent 25be726 commit 85bc852

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/ElasticsearchEngine.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ protected function performSearch(Builder $builder, array $options = [])
154154
$options['numericFilters']);
155155
}
156156

157+
if ($builder->callback) {
158+
return call_user_func(
159+
$builder->callback,
160+
$this->elastic,
161+
$builder->query,
162+
$params
163+
);
164+
}
165+
157166
return $this->elastic->search($params);
158167
}
159168

tests/ElasticsearchEngineTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
8282
$engine->search($builder);
8383
}
8484

85+
public function test_builder_callback_can_manipulate_search_parameters_to_elasticsearch()
86+
{
87+
/** @var \Elasticsearch\Client|\Mockery\MockInterface $client */
88+
$client = Mockery::mock(\Elasticsearch\Client::class);
89+
$client->shouldReceive('search')->with('modified_by_callback');
90+
91+
$engine = new ElasticsearchEngine($client, 'scout');
92+
$builder = new Laravel\Scout\Builder(
93+
new ElasticsearchEngineTestModel(),
94+
'huayra',
95+
function (\Elasticsearch\Client $client, $query, $params) {
96+
$this->assertNotEmpty($params);
97+
$this->assertEquals('huayra', $query);
98+
$params = 'modified_by_callback';
99+
100+
return $client->search($params);
101+
}
102+
);
103+
104+
$engine->search($builder);
105+
}
106+
85107
public function test_map_correctly_maps_results_to_models()
86108
{
87109
$client = Mockery::mock('Elasticsearch\Client');

0 commit comments

Comments
 (0)