-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
592 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Spira framework. | ||
* | ||
* @link https://github.com/spira/spira | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Services\ElasticSearch; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Filesystem\Filesystem; | ||
|
||
class SearchBuildIndexCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'search:build-index {--a|addtoindex : Reindex the models in the database}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
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. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$addToIndex = $this->option('addtoindex'); | ||
|
||
if (! $this->elasticSearch->reindexAll($addToIndex)) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Spira framework. | ||
* | ||
* @link https://github.com/spira/spira | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Providers; | ||
|
||
use App\Console\Commands\SearchBuildIndexCommand; | ||
use App\Services\ElasticSearch; | ||
use Elasticsearch\Client; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class ElasticServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->registerClient(); | ||
$this->registerService(); | ||
$this->commands([SearchBuildIndexCommand::class]); | ||
} | ||
|
||
protected function registerClient() | ||
{ | ||
$this->app->singleton(Client::class, function ($app) { | ||
return new Client($this->getClientConfig()); | ||
}); | ||
} | ||
|
||
protected function registerService() | ||
{ | ||
$this->app->singleton(ElasticSearch::class, function ($app) { | ||
return new ElasticSearch($app[Client::class], $this->getDefaultIndexNameFromConfig()); | ||
}); | ||
} | ||
|
||
protected function getDefaultIndexNameFromConfig() | ||
{ | ||
if (config()->has('elasticquent.default_index')) { | ||
return config()->get('elasticquent.default_index'); | ||
} | ||
|
||
return 'default'; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getClientConfig() | ||
{ | ||
if (config()->has('elasticquent.config')) { | ||
return config()->get('elasticquent.config'); | ||
} | ||
|
||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Spira framework. | ||
* | ||
* @link https://github.com/spira/spira | ||
* | ||
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
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; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $defaultIndexName; | ||
|
||
public function __construct(Client $elasticClient, $defaultIndexName = 'default') | ||
{ | ||
$this->elasticClient = $elasticClient; | ||
$this->defaultIndexName = $defaultIndexName; | ||
} | ||
|
||
/** | ||
* Get ElasticSearch Client. | ||
* | ||
* @return Client | ||
*/ | ||
public function getClient() | ||
{ | ||
return $this->elasticClient; | ||
} | ||
|
||
public function getDefaultIndexName() | ||
{ | ||
return $this->defaultIndexName; | ||
} | ||
|
||
/** | ||
* Create index. | ||
* @param IndexedModel|null $model | ||
*/ | ||
public function createIndex(IndexedModel $model = null) | ||
{ | ||
$indexName = $this->getDefaultIndexName(); | ||
|
||
if ($model) { | ||
$indexName = $model->getIndexName(); | ||
} | ||
|
||
$settings = $this->getIndexConfig($indexName); | ||
|
||
$this->getClient()->indices()->create($settings); | ||
} | ||
|
||
/** | ||
* @param IndexedModel|null $model | ||
* @return array | ||
*/ | ||
public function deleteIndex(IndexedModel $model = null) | ||
{ | ||
$indexName = $this->getDefaultIndexName(); | ||
|
||
if ($model) { | ||
$indexName = $model->getIndexName(); | ||
} | ||
|
||
$config = [ | ||
'index' => $indexName, | ||
]; | ||
|
||
return $this->getClient()->indices()->delete($config); | ||
} | ||
|
||
/** | ||
* @param IndexedModel|null $model | ||
* @return array | ||
*/ | ||
public function indexExists(IndexedModel $model = null) | ||
{ | ||
$indexName = $this->getDefaultIndexName(); | ||
|
||
if ($model) { | ||
$indexName = $model->getIndexName(); | ||
} | ||
|
||
$config = [ | ||
'index' => $indexName, | ||
]; | ||
|
||
return $this->getClient()->indices()->exists($config); | ||
} | ||
|
||
/** | ||
* @param $indexName | ||
* @return array | ||
*/ | ||
protected function getIndexConfig($indexName) | ||
{ | ||
$settings = [ | ||
'index' => $indexName, | ||
'body' => [ | ||
'settings' => [ | ||
'analysis' => [ | ||
'filter' => [ | ||
'autocomplete_filter' => [ | ||
'type' => 'edge_ngram', | ||
'min_gram' => 1, | ||
'max_gram' => 20, | ||
], | ||
], | ||
'analyzer' => [ | ||
'autocomplete' => [ | ||
'type' => 'custom', | ||
'tokenizer' => 'standard', | ||
'filter' => [ | ||
'lowercase', | ||
'autocomplete_filter', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
return $settings; | ||
} | ||
|
||
public function getIndexedModelClasses() | ||
{ | ||
return self::$indexedModels; | ||
} | ||
|
||
/** | ||
* @param bool $addToIndex | ||
* @return bool | ||
*/ | ||
public function reindexAll($addToIndex = true) | ||
{ | ||
if ($this->indexExists()) { | ||
$this->deleteIndex(); | ||
} | ||
|
||
$this->createIndex(); | ||
|
||
$indexedModelClasses = $this->getIndexedModelClasses(); | ||
|
||
foreach ($indexedModelClasses as $className) { | ||
|
||
/* @var $className IndexedModel */ | ||
$className::putMapping(); | ||
|
||
if ($addToIndex) { | ||
$className::addAllToIndex(); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.