Skip to content

Commit

Permalink
Improve delete create index (#63)
Browse files Browse the repository at this point in the history
* Add ability to delete an index on configure or indexing
* ShouldClear always needs the request
  • Loading branch information
Firesphere committed Oct 19, 2023
1 parent e14a5f0 commit bd5a483
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 30 deletions.
39 changes: 39 additions & 0 deletions src/Indexes/ElasticIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Firesphere\ElasticSearch\Traits\IndexTraits\BaseIndexTrait;
use Firesphere\SearchBackend\Indexes\CoreIndex;
use Firesphere\SearchBackend\Traits\LoggerTrait;
use Firesphere\SearchBackend\Traits\QueryTraits\QueryFilterTrait;
use LogicException;
use Psr\Container\NotFoundExceptionInterface;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
Expand All @@ -41,6 +44,7 @@ abstract class ElasticIndex extends CoreIndex
use Injectable;
use QueryFilterTrait;
use BaseIndexTrait;
use LoggerTrait;

/**
* @var array
Expand Down Expand Up @@ -86,6 +90,41 @@ public function init()
$this->initFromConfig($config);
}


/**
* @param HTTPRequest|null $request
* @return bool
* @throws ClientResponseException
* @throws MissingParameterException
* @throws NotFoundExceptionInterface
* @throws ServerResponseException
*/
public function deleteIndex(HTTPRequest $request): bool
{
$deleteResult = false;
if ($this->shouldClear($request) && $this->indexExists()) {
$this->getLogger()->info(sprintf('Clearing index %s', $this->getIndexName()));
$deleteResult = $this->client
->indices()
->delete(['index' => $this->getIndexName()])
->asBool();
}

return $deleteResult;
}

/**
* @param HTTPRequest $request
* @return bool
*/
private function shouldClear(HTTPRequest $request): bool
{
$var = $request->getVar('clear');

return !empty($var);
}


/**
* @return bool
* @throws ClientResponseException
Expand Down
51 changes: 23 additions & 28 deletions src/Tasks/ElasticConfigureTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Firesphere\SearchBackend\Helpers\FieldResolver;
use Firesphere\SearchBackend\Indexes\CoreIndex;
use Firesphere\SearchBackend\Traits\LoggerTrait;
use Psr\Container\NotFoundExceptionInterface;
use SilverStripe\Control\HTTPRequest;
Expand All @@ -36,14 +37,23 @@ class ElasticConfigureTask extends BuildTask
{
use LoggerTrait;

/**
* @var bool[]
*/
public $result;
/**
* @var string URLSegment
*/
private static $segment = 'ElasticConfigureTask';
/**
* DBHTML and DBText etc. should never be made sortable
* It doesn't make sense for large text objects
* @var string[]
*/
private static $unSsortables = [
'HTML',
'Text'
];
/**
* @var bool[]
*/
public $result;
/**
* @var string Title
*/
Expand All @@ -57,16 +67,6 @@ class ElasticConfigureTask extends BuildTask
*/
protected $service;

/**
* DBHTML and DBText etc. should never be made sortable
* It doesn't make sense for large text objects
* @var string[]
*/
private static $unSsortables = [
'HTML',
'Text'
];

/**
* @throws NotFoundExceptionInterface
*/
Expand Down Expand Up @@ -95,13 +95,8 @@ public function run($request)
try {
/** @var ElasticIndex $instance */
$instance = Injector::inst()->get($index, false);

if ($request->getVar('clear') && $instance->indexExists()) {
$this->getLogger()->info(sprintf('Clearing index %s', $instance->getIndexName()));
$deleteResult = $this->service->getClient()->indices()->delete(['index' => $instance->getIndexName()]);
$result[] = $deleteResult->asBool();
}

// If delete in advance, do so
$instance->deleteIndex($request);
$configResult = $this->configureIndex($instance);
$result[] = $configResult->asBool();
} catch (Exception $error) {
Expand All @@ -123,26 +118,26 @@ public function run($request)
}

/**
* Update/create a store
* @param ElasticIndex $instance
* Update/create a single index.
* @param ElasticIndex $index
* @return Elasticsearch
* @throws ClientResponseException
* @throws MissingParameterException
* @throws ServerResponseException
* @throws NotFoundExceptionInterface
* @throws ServerResponseException
*/
protected function configureIndex($instance): Elasticsearch
public function configureIndex(CoreIndex $index): Elasticsearch
{
$indexName = $instance->getIndexName();
$indexName = $index->getIndexName();

$instanceConfig = $this->createConfigForIndex($instance);
$instanceConfig = $this->createConfigForIndex($index);

$mappings = $this->convertForJSON($instanceConfig);

$body = ['index' => $indexName];
$client = $this->service->getClient();

$method = $this->getMethod($instance);
$method = $this->getMethod($index);
$msg = "%s index %s";
$msgType = 'Updating';
if ($method === 'create') {
Expand Down
14 changes: 12 additions & 2 deletions src/Tasks/ElasticIndexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

namespace Firesphere\ElasticSearch\Tasks;

use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Elasticsearch\Exception\HttpClientException;
use Elastic\Elasticsearch\Exception\MissingParameterException;
use Elastic\Elasticsearch\Exception\ServerResponseException;
use Exception;
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
Expand Down Expand Up @@ -103,10 +106,13 @@ public function __construct()

/**
* @param HTTPRequest $request
* @return int|void
* @return bool|int
* @throws HttpClientException
* @throws HttpException
* @throws NotFoundExceptionInterface
* @throws HttpClientException
* @throws ClientResponseException
* @throws MissingParameterException
* @throws ServerResponseException
*/
public function run($request)
{
Expand All @@ -124,6 +130,10 @@ public function run($request)
if (!count($classes)) {
continue;
}
// If clearing, also configure
if ($index->deleteIndex($request)) {
(new ElasticConfigureTask())->configureIndex($index);
}

// Get the groups
$groups = $this->indexClassForIndex($classes, $isGroup, $group);
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/Indexes/ElasticIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\src\SearchIndex;
use Elastic\Elasticsearch\Client;
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
use Firesphere\ElasticSearch\Tasks\ElasticIndexTask;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;

Expand Down Expand Up @@ -119,4 +121,17 @@ public function testIndexExists()
{
$this->assertNotNull($this->index->indexExists());
}

public function testNoClearIndex()
{
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
$isCleared = $this->index->deleteIndex($request);
$this->assertFalse($isCleared);
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
$isCleared = $this->index->deleteIndex($request);
$this->assertTrue($isCleared);

// Ensure everything is back in place.
(new ElasticIndexTask())->run($request);
}
}
10 changes: 10 additions & 0 deletions tests/unit/Tasks/ElasticConfigureTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Firesphere\ElasticSearch\Tests\unit\Tasks;

use App\src\SearchIndex;
use Firesphere\ElasticSearch\Services\ElasticCoreService;
use Firesphere\ElasticSearch\Tasks\ElasticConfigureTask;
use SilverStripe\Control\HTTPRequest;
Expand All @@ -27,4 +28,13 @@ public function testRun()
$this->assertNotContains(false, $task->result);

}

public function testConfigureIndex()
{
$index = new SearchIndex();
$task = new ElasticConfigureTask();
$result = $task->configureIndex($index);

$this->assertTrue($result->asBool());
}
}
5 changes: 5 additions & 0 deletions tests/unit/Tasks/ElasticIndexTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function testRun()
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
$result = $task->run($request);

$this->assertGreaterThan(0, $result);
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
$result = $task->run($request);

$this->assertGreaterThan(0, $result);
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
}
Expand Down

0 comments on commit bd5a483

Please sign in to comment.