diff --git a/composer.json b/composer.json index f1432e9f..993594b4 100644 --- a/composer.json +++ b/composer.json @@ -17,9 +17,9 @@ ], "require": { "php": "^8.0.12|^8.1", - "elasticsearch/elasticsearch": "^7.2", + "elasticsearch/elasticsearch": "^8.0", + "handcraftedinthealps/elasticsearch-dsl": "^8.0", "laravel/scout": "^8.0|^9.0", - "ongr/elasticsearch-dsl": "^7.0", "roave/better-reflection": "^4.3|^5.0" }, "require-dev": { diff --git a/src/ElasticSearch/Params/Bulk.php b/src/ElasticSearch/Params/Bulk.php index c4dca02b..f8b739b5 100644 --- a/src/ElasticSearch/Params/Bulk.php +++ b/src/ElasticSearch/Params/Bulk.php @@ -50,7 +50,6 @@ function ($payload, $model) { 'index' => [ '_index' => $model->searchableAs(), '_id' => $scoutKey, - '_type' => '_doc', 'routing' => false === empty($routing) ? $routing : $scoutKey, ], ]; @@ -73,7 +72,6 @@ function ($payload, $model) { 'delete' => [ '_index' => $model->searchableAs(), '_id' => $scoutKey, - '_type' => '_doc', 'routing' => false === empty($routing) ? $routing : $scoutKey, ], ]; diff --git a/src/ElasticSearchServiceProvider.php b/src/ElasticSearchServiceProvider.php index ef338ed1..ddee3e7e 100644 --- a/src/ElasticSearchServiceProvider.php +++ b/src/ElasticSearchServiceProvider.php @@ -4,8 +4,8 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; -use Elasticsearch\ClientBuilder; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\ClientBuilder; use Illuminate\Support\ServiceProvider; use Matchish\ScoutElasticSearch\ElasticSearch\Config\Config; diff --git a/src/Engines/ElasticSearchEngine.php b/src/Engines/ElasticSearchEngine.php index eebebdc4..6caec734 100644 --- a/src/Engines/ElasticSearchEngine.php +++ b/src/Engines/ElasticSearchEngine.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Engines; -use Elasticsearch\Common\Exceptions\ServerErrorResponseException; +use Elastic\Elasticsearch\Exception\ServerResponseException; use Illuminate\Database\Eloquent\Collection; use Laravel\Scout\Builder; use Laravel\Scout\Builder as BaseBuilder; @@ -21,17 +21,17 @@ final class ElasticSearchEngine extends Engine /** * The ElasticSearch client. * - * @var \Elasticsearch\Client + * @var \Elastic\Elasticsearch\Client */ protected $elasticsearch; /** * Create a new engine instance. * - * @param \Elasticsearch\Client $elasticsearch + * @param \Elastic\Elasticsearch\Client $elasticsearch * @return void */ - public function __construct(\Elasticsearch\Client $elasticsearch) + public function __construct(\Elastic\Elasticsearch\Client $elasticsearch) { $this->elasticsearch = $elasticsearch; } @@ -43,9 +43,9 @@ public function update($models) { $params = new Bulk(); $params->index($models); - $response = $this->elasticsearch->bulk($params->toArray()); + $response = $this->elasticsearch->bulk($params->toArray())->asArray(); if (array_key_exists('errors', $response) && $response['errors']) { - $error = new ServerErrorResponseException(json_encode($response, JSON_PRETTY_PRINT)); + $error = new ServerResponseException(json_encode($response, JSON_PRETTY_PRINT)); throw new \Exception('Bulk update error', $error->getCode(), $error); } } @@ -66,7 +66,7 @@ public function delete($models) public function flush($model) { $indexName = $model->searchableAs(); - $exist = $this->elasticsearch->indices()->exists(['index' => $indexName]); + $exist = $this->elasticsearch->indices()->exists(['index' => $indexName])->asBool(); if ($exist) { $body = (new Search())->addQuery(new MatchAllQuery())->toArray(); $params = new SearchParams($indexName, $body); @@ -185,6 +185,6 @@ private function performSearch(BaseBuilder $builder, $options = []) $indexName = $builder->index ?: $model->searchableAs(); $params = new SearchParams($indexName, $searchBody->toArray()); - return $this->elasticsearch->search($params->toArray()); + return $this->elasticsearch->search($params->toArray())->asArray(); } } diff --git a/src/Jobs/Import.php b/src/Jobs/Import.php index 4f090af6..7aa440f0 100644 --- a/src/Jobs/Import.php +++ b/src/Jobs/Import.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Illuminate\Bus\Queueable; use Illuminate\Support\Collection; use Matchish\ScoutElasticSearch\ProgressReportable; diff --git a/src/Jobs/Stages/CleanUp.php b/src/Jobs/Stages/CleanUp.php index 16d560c6..2f6af041 100644 --- a/src/Jobs/Stages/CleanUp.php +++ b/src/Jobs/Stages/CleanUp.php @@ -2,8 +2,8 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; -use Elasticsearch\Common\Exceptions\Missing404Exception; +use Elastic\Elasticsearch\Client; +use Elastic\Elasticsearch\Exception\ClientResponseException; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get as GetAliasParams; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Delete as DeleteIndexParams; use Matchish\ScoutElasticSearch\Searchable\ImportSource; @@ -31,9 +31,8 @@ public function handle(Client $elasticsearch): void $source = $this->source; $params = GetAliasParams::anyIndex($source->searchableAs()); try { - /** @var array $response */ - $response = $elasticsearch->indices()->getAlias($params->toArray()); - } catch (Missing404Exception $e) { + $response = $elasticsearch->indices()->getAlias($params->toArray())->asArray(); + } catch (ClientResponseException $e) { $response = []; } foreach ($response as $indexName => $data) { diff --git a/src/Jobs/Stages/CreateWriteIndex.php b/src/Jobs/Stages/CreateWriteIndex.php index b74ebf27..a19dd442 100644 --- a/src/Jobs/Stages/CreateWriteIndex.php +++ b/src/Jobs/Stages/CreateWriteIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\DefaultAlias; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Create; diff --git a/src/Jobs/Stages/RefreshIndex.php b/src/Jobs/Stages/RefreshIndex.php index 32153253..86a46d7e 100644 --- a/src/Jobs/Stages/RefreshIndex.php +++ b/src/Jobs/Stages/RefreshIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh; diff --git a/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php b/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php index d4b18661..8602af4d 100644 --- a/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php +++ b/src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch\Jobs\Stages; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get; use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Update; @@ -36,7 +36,7 @@ public function handle(Client $elasticsearch): void { $source = $this->source; $params = Get::anyIndex($source->searchableAs()); - $response = $elasticsearch->indices()->getAlias($params->toArray()); + $response = $elasticsearch->indices()->getAlias($params->toArray())->asArray(); $params = new Update(); foreach ($response as $indexName => $alias) { diff --git a/src/ScoutElasticSearchServiceProvider.php b/src/ScoutElasticSearchServiceProvider.php index 4abbbb0c..2fd7f755 100644 --- a/src/ScoutElasticSearchServiceProvider.php +++ b/src/ScoutElasticSearchServiceProvider.php @@ -4,7 +4,7 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Illuminate\Support\ServiceProvider; use Laravel\Scout\EngineManager; use Laravel\Scout\ScoutServiceProvider; diff --git a/tests/Feature/ElasticSearchEngineTest.php b/tests/Feature/ElasticSearchEngineTest.php new file mode 100644 index 00000000..edb258ae --- /dev/null +++ b/tests/Feature/ElasticSearchEngineTest.php @@ -0,0 +1,47 @@ +states(['iphone'])->create(); + Product::setEventDispatcher($dispatcher); + + Artisan::call('scout:import'); + + $results = Product::search('Quia', static function ($client, $body) { + return $client->search(['index' => 'products', 'body' => $body->toArray()]); + })->raw(); + $this->assertEmpty($results['hits']['hits']); + } + + public function test_pass_with_response(): void + { + $dispatcher = Product::getEventDispatcher(); + Product::unsetEventDispatcher(); + + $productsAmount = random_int(1, 5); + factory(Product::class, $productsAmount)->states(['iphone'])->create(); + Product::setEventDispatcher($dispatcher); + + Artisan::call('scout:import'); + + $results = Product::search('iphone', static function ($client, $body) { + return $client->search(['index' => 'products', 'body' => $body->toArray()]); + })->raw(); + + $this->assertNotEmpty($results['hits']['hits']); + } +} diff --git a/tests/Feature/ImportCommandTest.php b/tests/Feature/ImportCommandTest.php index 49cf3ba3..9b0c9147 100644 --- a/tests/Feature/ImportCommandTest.php +++ b/tests/Feature/ImportCommandTest.php @@ -142,7 +142,7 @@ public function test_remove_old_index_after_switching_to_new(): void Artisan::call('scout:import'); - $this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old']), 'Old index must be deleted'); + $this->assertFalse($this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(), 'Old index must be deleted'); } public function test_progress_report() diff --git a/tests/Feature/ScoutElasticSearchServiceProviderTest.php b/tests/Feature/ScoutElasticSearchServiceProviderTest.php index d4c4cde3..60a02816 100644 --- a/tests/Feature/ScoutElasticSearchServiceProviderTest.php +++ b/tests/Feature/ScoutElasticSearchServiceProviderTest.php @@ -2,7 +2,7 @@ namespace Matchish\ScoutElasticSearch; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Tests\TestCase; class ScoutElasticSearchServiceProviderTest extends TestCase diff --git a/tests/Integration/Jobs/Stages/CleanUpTest.php b/tests/Integration/Jobs/Stages/CleanUpTest.php index fca0d015..bcb3cdca 100644 --- a/tests/Integration/Jobs/Stages/CleanUpTest.php +++ b/tests/Integration/Jobs/Stages/CleanUpTest.php @@ -27,8 +27,8 @@ public function test_remove_write_index() $stage = new CleanUp(DefaultImportSourceFactory::from(Product::class)); $stage->handle($this->elasticsearch); - $writeIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new']); - $readIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old']); + $writeIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new'])->asBool(); + $readIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(); $this->assertFalse($writeIndexExist); $this->assertTrue($readIndexExist); diff --git a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php index cf9325d5..50b42001 100644 --- a/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php +++ b/tests/Integration/Jobs/Stages/CreateWriteIndexTest.php @@ -5,7 +5,7 @@ namespace Tests\Integration\Jobs\Stages; use App\Product; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Matchish\ScoutElasticSearch\ElasticSearch\Index; use Matchish\ScoutElasticSearch\Jobs\Stages\CreateWriteIndex; use Matchish\ScoutElasticSearch\Searchable\DefaultImportSourceFactory; @@ -15,10 +15,11 @@ final class CreateWriteIndexTest extends IntegrationTestCase { public function test_create_write_index(): void { + /** @var Client $elasticsearch */ $elasticsearch = $this->app->make(Client::class); $stage = new CreateWriteIndex(DefaultImportSourceFactory::from(Product::class), Index::fromSource(DefaultImportSourceFactory::from(Product::class))); $stage->handle($elasticsearch); - $response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products']); + $response = $elasticsearch->indices()->getAlias(['index' => '*', 'name' => 'products'])->asArray(); $this->assertTrue($this->containsWriteIndex($response, 'products')); } diff --git a/tests/Integration/Jobs/Stages/RefreshIndexTest.php b/tests/Integration/Jobs/Stages/RefreshIndexTest.php index deaec211..902f62a5 100644 --- a/tests/Integration/Jobs/Stages/RefreshIndexTest.php +++ b/tests/Integration/Jobs/Stages/RefreshIndexTest.php @@ -21,7 +21,6 @@ public function test_refresh_index(): void ['index' => [ '_index' => 'products', '_id' => 'id', - '_type' => '_doc', ]], [ 'id' => 1, @@ -40,7 +39,7 @@ public function test_refresh_index(): void ], ], ]; - $response = $this->elasticsearch->search($params); + $response = $this->elasticsearch->search($params)->asArray(); $this->assertEquals(1, $response['hits']['total']['value']); } } diff --git a/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php b/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php index fd23af70..fb906ca9 100644 --- a/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php +++ b/tests/Integration/Jobs/Stages/SwitchToNewAndRemoveOldIndexTest.php @@ -27,9 +27,9 @@ public function test_switch_to_new_and_remove_old_index(): void $stage = new SwitchToNewAndRemoveOldIndex(DefaultImportSourceFactory::from(Product::class), new Index('products_new')); $stage->handle($this->elasticsearch); - $newIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new']); - $oldIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old']); - $alias = $this->elasticsearch->indices()->getAlias(['index' => 'products_new']); + $newIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_new'])->asBool(); + $oldIndexExist = $this->elasticsearch->indices()->exists(['index' => 'products_old'])->asBool(); + $alias = $this->elasticsearch->indices()->getAlias(['index' => 'products_new'])->asArray(); $this->assertTrue($newIndexExist); $this->assertFalse($oldIndexExist); diff --git a/tests/IntegrationTestCase.php b/tests/IntegrationTestCase.php index 3f981ca2..0929f84e 100644 --- a/tests/IntegrationTestCase.php +++ b/tests/IntegrationTestCase.php @@ -2,7 +2,7 @@ namespace Tests; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; /** * Class IntegrationTestCase. diff --git a/tests/Unit/ElasticSearch/Params/BulkTest.php b/tests/Unit/ElasticSearch/Params/BulkTest.php index f4e1ff6e..d6055c26 100644 --- a/tests/Unit/ElasticSearch/Params/BulkTest.php +++ b/tests/Unit/ElasticSearch/Params/BulkTest.php @@ -17,7 +17,7 @@ public function test_delete() $params = $bulk->toArray(); $this->assertEquals([ - 'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]]], + 'body' => [['delete' => ['_index' => 'products', '_id' => 2, 'routing' => 2]]], ], $params); } @@ -31,7 +31,7 @@ public function test_delete_with_custom_key_name() $params = $bulk->toArray(); $this->assertEquals([ - 'body' => [['delete' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']]], + 'body' => [['delete' => ['_index' => 'products', '_id' => 'Scout', 'routing' => 'Scout']]], ], $params); } @@ -45,7 +45,7 @@ public function test_index() $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]], + ['index' => ['_index' => 'products', '_id' => 2, 'routing' => 2]], ['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); @@ -62,7 +62,7 @@ public function test_index_with_custom_key_name() $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 'Scout', 'routing' => 'Scout']], + ['index' => ['_index' => 'products', '_id' => 'Scout', 'routing' => 'Scout']], ['title' => 'Scout', 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); @@ -78,7 +78,7 @@ public function test_push_soft_delete_meta_data() $params = $bulk->toArray(); $this->assertEquals([ 'body' => [ - ['index' => ['_index' => 'products', '_type' => '_doc', '_id' => 2, 'routing' => 2]], + ['index' => ['_index' => 'products', '_id' => 2, 'routing' => 2]], ['title' => 'Scout', '__soft_deleted' => 0, 'id' => 2, '__class_name' => 'App\Product'], ], ], $params); diff --git a/tests/Unit/Engines/ElasticSearchEngineTest.php b/tests/Unit/Engines/ElasticSearchEngineTest.php index 989bec6a..db5e5433 100644 --- a/tests/Unit/Engines/ElasticSearchEngineTest.php +++ b/tests/Unit/Engines/ElasticSearchEngineTest.php @@ -3,10 +3,9 @@ namespace Tests\Unit\Engines; use App\Product; -use Elasticsearch\Client; +use Elastic\Elasticsearch\Client; use Laravel\Scout\Builder; use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine; -use Mockery as m; use Tests\TestCase; class ElasticSearchEngineTest extends TestCase @@ -19,30 +18,6 @@ public function test_map_ids() $this->assertEquals([1, 15], $ids->all()); } - public function test_pass_client_to_callback() - { - $client = m::mock(Client::class); - $engine = new ElasticSearchEngine($client); - $query = 'zonda'; - $client->shouldReceive('search')->once()->withNoArgs(); - $builder = new Builder(new Product(), $query, function ($client, $query) { - return $client->search(); - }); - $engine->search($builder); - } - - public function test_pass_search_builder_to_callback() - { - $client = m::mock(Client::class); - $engine = new ElasticSearchEngine($client); - $client->shouldReceive('search')->once()->with(m::type('array')); - $query = 'zonda'; - $builder = new Builder(new Product(), $query, function ($client, $query) { - return $client->search($query->toArray()); - }); - $engine->search($builder); - } - public function test_pass_query_to_callback_before_executing() { $builder = new Builder(new Product(), 'zonga');