Skip to content

Commit

Permalink
Merge pull request #68 from matchish/issue-37
Browse files Browse the repository at this point in the history
Throw exception if there are elasticsearch bulk error
  • Loading branch information
matchish authored Nov 4, 2019
2 parents 27a9c7e + d56339c commit 83659ce
Show file tree
Hide file tree
Showing 37 changed files with 99 additions and 72 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [2.0.3] - 2019-11-04
### Fixed
- Throw exception if there are elasticsearch errors on update

## [2.0.2] - 2019-05-10
### Added
- Search amongst multiple models
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "matchish/laravel-scout-elasticsearch",
"description": "This package extends Laravel Scout adding full power of ElasticSearch",
"version": "2.0.2",
"version": "2.0.3",
"keywords": [
"laravel",
"scout",
Expand Down
2 changes: 1 addition & 1 deletion src/ElasticSearch/EloquentHitsIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Matchish\ScoutElasticSearch\ElasticSearch;

use Traversable;
use Laravel\Scout\Builder;
use Laravel\Scout\Searchable;
use Traversable;

/**
* @internal
Expand Down
6 changes: 3 additions & 3 deletions src/ElasticSearch/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Matchish\ScoutElasticSearch\ElasticSearch;

use Laravel\Scout\Builder;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchDSL\Sort\FieldSort;

final class SearchFactory
{
Expand Down
21 changes: 12 additions & 9 deletions src/Engines/ElasticSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Matchish\ScoutElasticSearch\Engines;

use Laravel\Scout\Searchable;
use Laravel\Scout\Engines\Engine;
use ONGR\ElasticsearchDSL\Search;
use Laravel\Scout\Builder as BaseBuilder;
use Illuminate\Database\Eloquent\Collection;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use Laravel\Scout\Builder as BaseBuilder;
use Laravel\Scout\Engines\Engine;
use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk;
use Matchish\ScoutElasticSearch\ElasticSearch\SearchFactory;
use Matchish\ScoutElasticSearch\ElasticSearch\SearchResults;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh;
use Matchish\ScoutElasticSearch\ElasticSearch\EloquentHitsIteratorAggregate;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Search as SearchParams;
use Matchish\ScoutElasticSearch\ElasticSearch\SearchFactory;
use Matchish\ScoutElasticSearch\ElasticSearch\SearchResults;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Search;

final class ElasticSearchEngine extends Engine
{
Expand Down Expand Up @@ -42,7 +42,10 @@ public function update($models)
{
$params = new Bulk();
$params->index($models);
$this->elasticsearch->bulk($params->toArray());
$response = $this->elasticsearch->bulk($params->toArray());
if (array_key_exists('errors', $response) && $response['errors']) {
throw new \Exception('Bulk update error');
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Jobs/ImportStages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Matchish\ScoutElasticSearch\Jobs;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\Jobs\Stages\CleanUp;
use Matchish\ScoutElasticSearch\Jobs\Stages\RefreshIndex;
use Matchish\ScoutElasticSearch\Jobs\Stages\PullFromSource;
use Matchish\ScoutElasticSearch\Jobs\Stages\CreateWriteIndex;
use Matchish\ScoutElasticSearch\Jobs\Stages\PullFromSource;
use Matchish\ScoutElasticSearch\Jobs\Stages\RefreshIndex;
use Matchish\ScoutElasticSearch\Jobs\Stages\SwitchToNewAndRemoveOldIndex;

class ImportStages extends Collection
Expand Down
4 changes: 2 additions & 2 deletions src/Jobs/Stages/CleanUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Matchish\ScoutElasticSearch\Jobs\Stages;

use Elasticsearch\Client;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get as GetAliasParams;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Delete as DeleteIndexParams;

Expand Down
6 changes: 3 additions & 3 deletions src/Jobs/Stages/CreateWriteIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Matchish\ScoutElasticSearch\Jobs\Stages;

use Elasticsearch\Client;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\ElasticSearch\WriteAlias;
use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\ElasticSearch\DefaultAlias;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Create;
use Matchish\ScoutElasticSearch\ElasticSearch\WriteAlias;

/**
* @internal
Expand Down
6 changes: 3 additions & 3 deletions src/Jobs/Stages/PullFromSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Matchish\ScoutElasticSearch\Jobs\Stages;

use Laravel\Scout\Searchable;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Laravel\Scout\Searchable;

/**
* @internal
Expand Down
2 changes: 1 addition & 1 deletion src/Jobs/Stages/SwitchToNewAndRemoveOldIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Matchish\ScoutElasticSearch\Jobs\Stages;

use Elasticsearch\Client;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Get;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Alias\Update;
Expand Down
2 changes: 1 addition & 1 deletion src/Mixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Matchish\ScoutElasticSearch;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Builder;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

final class Mixed
{
Expand Down
2 changes: 1 addition & 1 deletion src/ProgressReportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Matchish\ScoutElasticSearch;

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\NullOutput;

trait ProgressReportable
{
Expand Down
4 changes: 2 additions & 2 deletions src/ScoutElasticSearchServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Matchish\ScoutElasticSearch;

use Elasticsearch\Client;
use Laravel\Scout\EngineManager;
use Illuminate\Support\ServiceProvider;
use Laravel\Scout\EngineManager;
use Laravel\Scout\ScoutServiceProvider;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use Matchish\ScoutElasticSearch\Console\Commands\FlushCommand;
use Matchish\ScoutElasticSearch\Console\Commands\ImportCommand;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;

final class ScoutElasticSearchServiceProvider extends ServiceProvider
{
Expand Down
4 changes: 2 additions & 2 deletions src/Searchable/SearchableListFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Matchish\ScoutElasticSearch\Searchable;

use function in_array;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use function in_array;
use Laravel\Scout\Searchable;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;

final class SearchableListFactory
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FlushCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Tests\Feature;

use App\Product;
use Tests\IntegrationTestCase;
use Illuminate\Support\Facades\Artisan;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Bulk;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Refresh;
use Tests\IntegrationTestCase;

final class FlushCommandTest extends IntegrationTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/ImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Tests\Feature;

use App\Book;
use stdClass;
use App\Product;
use Tests\IntegrationTestCase;
use Illuminate\Support\Facades\Artisan;
use stdClass;
use Symfony\Component\Console\Output\BufferedOutput;
use Tests\IntegrationTestCase;

final class ImportCommandTest extends IntegrationTestCase
{
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,8 +2,8 @@

namespace Matchish\ScoutElasticSearch;

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

class ScoutElasticSearchServiceProviderTest extends TestCase
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Tests\Feature;

use App\Book;
use App\Ticket;
use App\Product;
use Tests\IntegrationTestCase;
use App\Ticket;
use Illuminate\Pagination\Paginator;
use Matchish\ScoutElasticSearch\Mixed;
use Illuminate\Support\Facades\Artisan;
use Matchish\ScoutElasticSearch\Mixed;
use Tests\IntegrationTestCase;

final class SearchTest extends IntegrationTestCase
{
Expand Down
24 changes: 22 additions & 2 deletions tests/Integration/Engines/ElasticSearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Tests\Integration\Engines;

use stdClass;
use App\Product;
use Laravel\Scout\Builder;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\ElasticSearch\Params\Indices\Create;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use stdClass;
use Tests\IntegrationTestCase;

final class ElasticSearchEngineTest extends IntegrationTestCase
{
Expand Down Expand Up @@ -55,6 +57,24 @@ public function test_update()
}
}

public function test_update_throw_exception_on_elasticsearch_error()
{
$this->expectException(\Exception::class);
$models = Product::all();
$models->map(function ($model) {
$model->price = 'bad format';

return $model;
});
$index = Index::fromSearchable($models->first());
$params = new Create(
'products',
$index->config()
);
$this->elasticsearch->indices()->create($params->toArray());
$this->engine->update($models);
}

public function test_delete()
{
$models = Product::all();
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Jobs/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Tests\Integration\Jobs;

use App\Product;
use Tests\IntegrationTestCase;
use Illuminate\Console\OutputStyle;
use Matchish\ScoutElasticSearch\Jobs\Import;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
use Tests\IntegrationTestCase;

class ImportTest extends IntegrationTestCase
{
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 @@ -2,10 +2,10 @@

namespace Tests\Integration\Jobs\Stages;

use stdClass;
use App\Product;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\Jobs\Stages\CleanUp;
use stdClass;
use Tests\IntegrationTestCase;

class CleanUpTest extends IntegrationTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Jobs/Stages/CreateWriteIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use App\Product;
use Elasticsearch\Client;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\Jobs\Stages\CreateWriteIndex;
use Tests\IntegrationTestCase;

final class CreateWriteIndexTest extends IntegrationTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Jobs/Stages/PullFromSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Tests\Integration\Jobs\Stages;

use stdClass;
use App\Product;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\Jobs\Stages\PullFromSource;
use stdClass;
use Tests\IntegrationTestCase;

final class PullFromSourceTest extends IntegrationTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Jobs/Stages/RefreshIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Tests\Integration\Jobs\Stages;

use stdClass;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\Jobs\Stages\RefreshIndex;
use stdClass;
use Tests\IntegrationTestCase;

final class RefreshIndexTest extends IntegrationTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Tests\Integration\Jobs\Stages;

use stdClass;
use App\Product;
use Tests\IntegrationTestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Matchish\ScoutElasticSearch\Jobs\Stages\SwitchToNewAndRemoveOldIndex;
use stdClass;
use Tests\IntegrationTestCase;

final class SwitchToNewAndRemoveOldIndexTest extends IntegrationTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Illuminate\Support\Facades\Artisan;
use Laravel\Scout\ScoutServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use Matchish\ScoutElasticSearch\ElasticSearchServiceProvider;
use Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine;
use Matchish\ScoutElasticSearch\ScoutElasticSearchServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ElasticSearch/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace Tests\Unit\ElasticSearch;

use App\Product;
use Tests\TestCase;
use Matchish\ScoutElasticSearch\ElasticSearch\Index;
use Tests\TestCase;

class IndexTest extends TestCase
{
Expand Down
Loading

0 comments on commit 83659ce

Please sign in to comment.