Skip to content

Commit

Permalink
Merge pull request #198 from hkulekci/elasticsearch-8-upgrade
Browse files Browse the repository at this point in the history
elasticsearch 8 upgrade
  • Loading branch information
matchish authored Apr 20, 2022
2 parents 8f0fffc + 2c450cc commit a786cc2
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 68 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions src/ElasticSearch/Params/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function ($payload, $model) {
'index' => [
'_index' => $model->searchableAs(),
'_id' => $scoutKey,
'_type' => '_doc',
'routing' => false === empty($routing) ? $routing : $scoutKey,
],
];
Expand All @@ -73,7 +72,6 @@ function ($payload, $model) {
'delete' => [
'_index' => $model->searchableAs(),
'_id' => $scoutKey,
'_type' => '_doc',
'routing' => false === empty($routing) ? $routing : $scoutKey,
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/ElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 8 additions & 8 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/Jobs/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/Jobs/Stages/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/Stages/CreateWriteIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/Stages/RefreshIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/ScoutElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions tests/Feature/ElasticSearchEngineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Product;
use Illuminate\Support\Facades\Artisan;
use Tests\IntegrationTestCase;

final class ElasticSearchEngineTest extends IntegrationTestCase
{
public function test_pass_empty_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('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']);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/ScoutElasticSearchServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Matchish\ScoutElasticSearch;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;
use Tests\TestCase;

class ScoutElasticSearchServiceProviderTest extends TestCase
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Jobs/Stages/CleanUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Jobs/Stages/CreateWriteIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
}

Expand Down
3 changes: 1 addition & 2 deletions tests/Integration/Jobs/Stages/RefreshIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function test_refresh_index(): void
['index' => [
'_index' => 'products',
'_id' => 'id',
'_type' => '_doc',
]],
[
'id' => 1,
Expand All @@ -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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests;

use Elasticsearch\Client;
use Elastic\Elasticsearch\Client;

/**
* Class IntegrationTestCase.
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/ElasticSearch/Params/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Loading

0 comments on commit a786cc2

Please sign in to comment.