Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Commit

Permalink
Execute the callback that can be passed to the Builder in performSear…
Browse files Browse the repository at this point in the history
…ch (#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
  • Loading branch information
kronthto authored and ErickTamayo committed Aug 21, 2017
1 parent 25be726 commit 85bc852
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ElasticsearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ protected function performSearch(Builder $builder, array $options = [])
$options['numericFilters']);
}

if ($builder->callback) {
return call_user_func(
$builder->callback,
$this->elastic,
$builder->query,
$params
);
}

return $this->elastic->search($params);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/ElasticsearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ public function test_search_sends_correct_parameters_to_elasticsearch()
$engine->search($builder);
}

public function test_builder_callback_can_manipulate_search_parameters_to_elasticsearch()
{
/** @var \Elasticsearch\Client|\Mockery\MockInterface $client */
$client = Mockery::mock(\Elasticsearch\Client::class);
$client->shouldReceive('search')->with('modified_by_callback');

$engine = new ElasticsearchEngine($client, 'scout');
$builder = new Laravel\Scout\Builder(
new ElasticsearchEngineTestModel(),
'huayra',
function (\Elasticsearch\Client $client, $query, $params) {
$this->assertNotEmpty($params);
$this->assertEquals('huayra', $query);
$params = 'modified_by_callback';

return $client->search($params);
}
);

$engine->search($builder);
}

public function test_map_correctly_maps_results_to_models()
{
$client = Mockery::mock('Elasticsearch\Client');
Expand Down

0 comments on commit 85bc852

Please sign in to comment.