diff --git a/src/ElasticsearchEngine.php b/src/ElasticsearchEngine.php index 46f0510..c78cb83 100644 --- a/src/ElasticsearchEngine.php +++ b/src/ElasticsearchEngine.php @@ -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); } diff --git a/tests/ElasticsearchEngineTest.php b/tests/ElasticsearchEngineTest.php index 0508b2a..283bb27 100644 --- a/tests/ElasticsearchEngineTest.php +++ b/tests/ElasticsearchEngineTest.php @@ -80,6 +80,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');