Skip to content

Commit

Permalink
Merge ad56947 into 319fd0e
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Dec 21, 2015
2 parents 319fd0e + ad56947 commit d0b9e3a
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 84 deletions.
67 changes: 67 additions & 0 deletions api/app/Console/Commands/SearchBuildIndexCommand.php
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;
}
}
66 changes: 66 additions & 0 deletions api/app/Providers/ElasticServiceProvider.php
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 [];
}
}
178 changes: 178 additions & 0 deletions api/app/Services/ElasticSearch.php
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;
}
}
1 change: 1 addition & 0 deletions api/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
*/

$app->register(App\Providers\ApplicationProvider::class);
$app->register(App\Providers\ElasticServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\AuthDriverServiceProvider::class);
$app->register(App\Providers\AccessServiceProvider::class);
Expand Down
2 changes: 1 addition & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"laravel/socialite": "^2.0",
"cloudinary/cloudinary_php": "^1.1",
"venturecraft/revisionable": "^2.0",
"spira/core": "1.0.*",
"spira/core": "1.1.*",
"namshi/jose": "^6.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit d0b9e3a

Please sign in to comment.