Skip to content

Commit

Permalink
Merge branch 'kronthto/feature/search-callback' (ErickTamayo#56) into…
Browse files Browse the repository at this point in the history
… extended
  • Loading branch information
kronthto committed May 16, 2017
2 parents 1cb09c3 + 9dbde2d commit ad5ffaa
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 ad5ffaa

Please sign in to comment.