From acf28f203b48204705ce8439a413f82596206b41 Mon Sep 17 00:00:00 2001 From: Michael Slowik Date: Sat, 23 Jun 2018 16:17:08 +0200 Subject: [PATCH 1/2] Use index instead of types --- src/ElasticsearchEngine.php | 36 ++++++++++++++--------------------- src/ElasticsearchProvider.php | 8 ++++---- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/ElasticsearchEngine.php b/src/ElasticsearchEngine.php index 61c86de..62bf8fb 100644 --- a/src/ElasticsearchEngine.php +++ b/src/ElasticsearchEngine.php @@ -10,13 +10,6 @@ class ElasticsearchEngine extends Engine { - /** - * Index where the models will be saved. - * - * @var string - */ - protected $index; - /** * Elastic where the instance of Elastic|\Elasticsearch\Client is stored. * @@ -30,10 +23,9 @@ class ElasticsearchEngine extends Engine * @param \Elasticsearch\Client $elastic * @return void */ - public function __construct(Elastic $elastic, $index) + public function __construct(Elastic $elastic) { $this->elastic = $elastic; - $this->index = $index; } /** @@ -46,12 +38,11 @@ public function update($models) { $params['body'] = []; - $models->each(function($model) use (&$params) - { + $models->each(function ($model) use (&$params) { $params['body'][] = [ 'update' => [ '_id' => $model->getKey(), - '_index' => $this->index, + '_index' => $model->searchableAs(), '_type' => $model->searchableAs(), ] ]; @@ -74,12 +65,11 @@ public function delete($models) { $params['body'] = []; - $models->each(function($model) use (&$params) - { + $models->each(function ($model) use (&$params) { $params['body'][] = [ 'delete' => [ '_id' => $model->getKey(), - '_index' => $this->index, + '_index' => $model->searchableAs(), '_type' => $model->searchableAs(), ] ]; @@ -118,7 +108,7 @@ public function paginate(Builder $builder, $perPage, $page) 'size' => $perPage, ]); - $result['nbPages'] = $result['hits']['total']/$perPage; + $result['nbPages'] = $result['hits']['total']/$perPage; return $result; } @@ -133,8 +123,7 @@ public function paginate(Builder $builder, $perPage, $page) protected function performSearch(Builder $builder, array $options = []) { $params = [ - 'index' => $this->index, - 'type' => $builder->index ?: $builder->model->searchableAs(), + 'index' => $builder->index ?: $builder->model->searchableAs(), 'body' => [ 'query' => [ 'bool' => [ @@ -157,8 +146,10 @@ protected function performSearch(Builder $builder, array $options = []) } if (isset($options['numericFilters']) && count($options['numericFilters'])) { - $params['body']['query']['bool']['must'] = array_merge($params['body']['query']['bool']['must'], - $options['numericFilters']); + $params['body']['query']['bool']['must'] = array_merge( + $params['body']['query']['bool']['must'], + $options['numericFilters'] + ); } if ($builder->callback) { @@ -218,7 +209,8 @@ public function map($results, $model) ->pluck('_id')->values()->all(); $models = $model->whereIn( - $model->getKeyName(), $keys + $model->getKeyName(), + $keys )->get()->keyBy($model->getKeyName()); return collect($results['hits']['hits'])->map(function ($hit) use ($model, $models) { @@ -249,7 +241,7 @@ protected function sort($builder) return null; } - return collect($builder->orders)->map(function($order) { + return collect($builder->orders)->map(function ($order) { return [$order['column'] => $order['direction']]; })->toArray(); } diff --git a/src/ElasticsearchProvider.php b/src/ElasticsearchProvider.php index 500b854..fefd3b8 100644 --- a/src/ElasticsearchProvider.php +++ b/src/ElasticsearchProvider.php @@ -13,11 +13,11 @@ class ElasticsearchProvider extends ServiceProvider */ public function boot() { - app(EngineManager::class)->extend('elasticsearch', function($app) { - return new ElasticsearchEngine(ElasticBuilder::create() + app(EngineManager::class)->extend('elasticsearch', function ($app) { + return new ElasticsearchEngine( + ElasticBuilder::create() ->setHosts(config('scout.elasticsearch.hosts')) - ->build(), - config('scout.elasticsearch.index') + ->build() ); }); } From 504ac5154354e26307c2dc03ce37c898a079bfcf Mon Sep 17 00:00:00 2001 From: Michael Slowik Date: Sat, 23 Jun 2018 16:17:20 +0200 Subject: [PATCH 2/2] fixed test --- tests/ElasticsearchEngineTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/ElasticsearchEngineTest.php b/tests/ElasticsearchEngineTest.php index d4bc6b5..d3c8791 100644 --- a/tests/ElasticsearchEngineTest.php +++ b/tests/ElasticsearchEngineTest.php @@ -18,7 +18,7 @@ public function test_update_adds_objects_to_index() [ 'update' => [ '_id' => 1, - '_index' => 'scout', + '_index' => 'table', '_type' => 'table', ] ], @@ -29,7 +29,7 @@ public function test_update_adds_objects_to_index() ] ]); - $engine = new ElasticsearchEngine($client, 'scout'); + $engine = new ElasticsearchEngine($client); $engine->update(Collection::make([new ElasticsearchEngineTestModel])); } @@ -41,14 +41,14 @@ public function test_delete_removes_objects_to_index() [ 'delete' => [ '_id' => 1, - '_index' => 'scout', + '_index' => 'table', '_type' => 'table', ] ], ] ]); - $engine = new ElasticsearchEngine($client, 'scout'); + $engine = new ElasticsearchEngine($client); $engine->delete(Collection::make([new ElasticsearchEngineTestModel])); } @@ -56,8 +56,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch() { $client = Mockery::mock('Elasticsearch\Client'); $client->shouldReceive('search')->with([ - 'index' => 'scout', - 'type' => 'table', + 'index' => 'table', 'body' => [ 'query' => [ 'bool' => [ @@ -74,7 +73,7 @@ public function test_search_sends_correct_parameters_to_elasticsearch() ] ]); - $engine = new ElasticsearchEngine($client, 'scout'); + $engine = new ElasticsearchEngine($client); $builder = new Laravel\Scout\Builder(new ElasticsearchEngineTestModel, 'zonda'); $builder->where('foo', 1); $builder->where('bar', [1, 3]);