Skip to content

Commit

Permalink
Implemented reindexing for all defined indexed models
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Dec 21, 2015
1 parent 3cb4b01 commit 0da1de0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
37 changes: 37 additions & 0 deletions api/app/Console/Commands/SearchBuildIndexCommand.php
Expand Up @@ -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
{
Expand All @@ -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.
*
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions api/app/Console/Kernel.php
Expand Up @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\ApiaryValidateCommand',
'App\Console\Commands\GenerateKeysCommand',
'App\Console\Commands\CreateUserCommand',
'App\Console\Commands\SearchBuildIndexCommand',
];

/**
Expand Down
14 changes: 14 additions & 0 deletions api/app/Services/ElasticSearch.php
Expand Up @@ -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;

Expand Down Expand Up @@ -145,4 +154,9 @@ protected function getIndexConfig($indexName)
return $settings;
}

public function getIndexedModelClasses()
{
return self::$indexedModels;
}

}
Expand Up @@ -52,7 +52,6 @@ public function up()
->onDelete('set null');
});

AbstractPost::putMapping();
}

/**
Expand Down
2 changes: 0 additions & 2 deletions api/database/migrations/2015_10_21_051428_seed_tags.php
Expand Up @@ -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();
}

/**
Expand Down
19 changes: 12 additions & 7 deletions api/tests/Commands/SearchBuildIndexCommandTest.php
Expand Up @@ -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());
}

}
}
15 changes: 15 additions & 0 deletions api/tests/ElasticSearchTest.php
Expand Up @@ -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);
}

}

}

0 comments on commit 0da1de0

Please sign in to comment.