From 0da1de05faf1dd1fffe134e1584bee14733ca7cd Mon Sep 17 00:00:00 2001 From: Zak Henry Date: Mon, 21 Dec 2015 16:00:18 +1100 Subject: [PATCH] Implemented reindexing for all defined indexed models --- .../Commands/SearchBuildIndexCommand.php | 37 +++++++++++++++++++ api/app/Console/Kernel.php | 1 + api/app/Services/ElasticSearch.php | 14 +++++++ .../2015_07_21_144551_create_posts_table.php | 1 - .../2015_10_21_051428_seed_tags.php | 2 - .../Commands/SearchBuildIndexCommandTest.php | 19 ++++++---- api/tests/ElasticSearchTest.php | 15 ++++++++ 7 files changed, 79 insertions(+), 10 deletions(-) diff --git a/api/app/Console/Commands/SearchBuildIndexCommand.php b/api/app/Console/Commands/SearchBuildIndexCommand.php index 25e7a613..95a9c64d 100644 --- a/api/app/Console/Commands/SearchBuildIndexCommand.php +++ b/api/app/Console/Commands/SearchBuildIndexCommand.php @@ -10,8 +10,10 @@ namespace App\Console\Commands; +use App\Services\ElasticSearch; use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; +use Spira\Core\Model\Model\IndexedModel; class SearchBuildIndexCommand extends Command { @@ -29,6 +31,26 @@ class SearchBuildIndexCommand extends Command */ protected $description = '(re)Build search index'; + + /** + * ElasticSearch Service. + * + * @var ElasticSearch + */ + protected $elasticSearch; + + /** + * Create a new command instance. + * + * @param Filesystem $file + */ + public function __construct(ElasticSearch $elasticSearch) + { + parent::__construct(); + + $this->elasticSearch = $elasticSearch; + } + /** * Execute the console command. * @@ -37,6 +59,21 @@ class SearchBuildIndexCommand extends Command public function handle() { + if ($this->elasticSearch->indexExists()){ + $this->elasticSearch->deleteIndex(); + } + + $this->elasticSearch->createIndex(); + + $indexedModelClasses = $this->elasticSearch->getIndexedModelClasses(); + + foreach ($indexedModelClasses as $className){ + + /** @var $className IndexedModel */ + $className::putMapping(); + $className::addAllToIndex(); + } + return 0; } } diff --git a/api/app/Console/Kernel.php b/api/app/Console/Kernel.php index 7e974fc5..e2ffdc1d 100644 --- a/api/app/Console/Kernel.php +++ b/api/app/Console/Kernel.php @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel 'App\Console\Commands\ApiaryValidateCommand', 'App\Console\Commands\GenerateKeysCommand', 'App\Console\Commands\CreateUserCommand', + 'App\Console\Commands\SearchBuildIndexCommand', ]; /** diff --git a/api/app/Services/ElasticSearch.php b/api/app/Services/ElasticSearch.php index 0e29c6d2..867a5eb8 100644 --- a/api/app/Services/ElasticSearch.php +++ b/api/app/Services/ElasticSearch.php @@ -10,11 +10,20 @@ namespace App\Services; +use App\Models\Tag; +use App\Models\User; +use App\Models\Article; use Elasticsearch\Client; use Spira\Core\Model\Model\IndexedModel; class ElasticSearch { + private static $indexedModels = [ + User::class, + Article::class, + Tag::class, + ]; + /** @var \Cloudinary */ protected $elasticClient; @@ -145,4 +154,9 @@ protected function getIndexConfig($indexName) return $settings; } + public function getIndexedModelClasses() + { + return self::$indexedModels; + } + } diff --git a/api/database/migrations/2015_07_21_144551_create_posts_table.php b/api/database/migrations/2015_07_21_144551_create_posts_table.php index 4ffcf416..49ebac77 100644 --- a/api/database/migrations/2015_07_21_144551_create_posts_table.php +++ b/api/database/migrations/2015_07_21_144551_create_posts_table.php @@ -52,7 +52,6 @@ public function up() ->onDelete('set null'); }); - AbstractPost::putMapping(); } /** diff --git a/api/database/migrations/2015_10_21_051428_seed_tags.php b/api/database/migrations/2015_10_21_051428_seed_tags.php index 0082a7d9..4c20b290 100644 --- a/api/database/migrations/2015_10_21_051428_seed_tags.php +++ b/api/database/migrations/2015_10_21_051428_seed_tags.php @@ -141,8 +141,6 @@ public function up() DB::table(Tag::getTableName())->insert($tagInserts['tag_inserts']); DB::table(CreateTagTagTable::TABLE_NAME)->insert($tagInserts['tag_relationship_inserts']); - - Tag::reindex(); } /** diff --git a/api/tests/Commands/SearchBuildIndexCommandTest.php b/api/tests/Commands/SearchBuildIndexCommandTest.php index b6a9cb20..b597a55e 100644 --- a/api/tests/Commands/SearchBuildIndexCommandTest.php +++ b/api/tests/Commands/SearchBuildIndexCommandTest.php @@ -9,28 +9,33 @@ */ use Mockery as m; +use App\Services\ElasticSearch; use App\Console\Commands\SearchBuildIndexCommand; /** * Class SearchBuildIndexCommandTest * @group commands - * @group testing */ class SearchBuildIndexCommandTest extends TestCase { public function testSearchBuildIndexCommand() { - $esMock = m::mock(\Elasticsearch\Client::class); - $esMock->shouldReceive('exists')->andReturn(true) - ->shouldReceive('put')->andReturn(true); + $esMock = m::mock(ElasticSearch::class); + $esMock->shouldReceive('indexExists')->andReturn(true); + $esMock->shouldReceive('createIndex'); + $esMock->shouldReceive('deleteIndex'); - $this->app->instance(\Elasticsearch\Client::class, $esMock); + $indexedModelMock = m::mock('alias:IndexedModelMock'); + $indexedModelMock->shouldReceive('putMapping')->once(); + $indexedModelMock->shouldReceive('addAllToIndex')->once(); + + $esMock->shouldReceive('getIndexedModelClasses')->andReturn(['IndexedModelMock']); /** @var SearchBuildIndexCommand $cmd */ - $cmd = $this->app->make(SearchBuildIndexCommand::class); + $cmd = $this->app->make(SearchBuildIndexCommand::class, [$esMock]); $this->assertEquals(0, $cmd->handle()); } -} +} \ No newline at end of file diff --git a/api/tests/ElasticSearchTest.php b/api/tests/ElasticSearchTest.php index 3a8b3369..0db3c79d 100644 --- a/api/tests/ElasticSearchTest.php +++ b/api/tests/ElasticSearchTest.php @@ -119,4 +119,19 @@ public function getIndexName() }); } + public function testGetIndexedModels() + { + $elasticSearchMock = Mockery::mock(\Elasticsearch\Client::class); + $elasticSearchService = new \App\Services\ElasticSearch($elasticSearchMock); + + $classes = $elasticSearchService->getIndexedModelClasses(); + + $this->assertInternalType('array', $classes); + + foreach($classes as $className){ + $this->assertInstanceOf(\Spira\Core\Model\Model\IndexedModel::class, new $className); + } + + } + }