Skip to content

Commit bd5a483

Browse files
authored
Improve delete create index (#63)
* Add ability to delete an index on configure or indexing * ShouldClear always needs the request
1 parent e14a5f0 commit bd5a483

File tree

6 files changed

+104
-30
lines changed

6 files changed

+104
-30
lines changed

src/Indexes/ElasticIndex.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
use Firesphere\ElasticSearch\Services\ElasticCoreService;
1919
use Firesphere\ElasticSearch\Traits\IndexTraits\BaseIndexTrait;
2020
use Firesphere\SearchBackend\Indexes\CoreIndex;
21+
use Firesphere\SearchBackend\Traits\LoggerTrait;
2122
use Firesphere\SearchBackend\Traits\QueryTraits\QueryFilterTrait;
2223
use LogicException;
24+
use Psr\Container\NotFoundExceptionInterface;
25+
use SilverStripe\Control\HTTPRequest;
2326
use SilverStripe\Core\Config\Configurable;
2427
use SilverStripe\Core\Extensible;
2528
use SilverStripe\Core\Injector\Injectable;
@@ -41,6 +44,7 @@ abstract class ElasticIndex extends CoreIndex
4144
use Injectable;
4245
use QueryFilterTrait;
4346
use BaseIndexTrait;
47+
use LoggerTrait;
4448

4549
/**
4650
* @var array
@@ -86,6 +90,41 @@ public function init()
8690
$this->initFromConfig($config);
8791
}
8892

93+
94+
/**
95+
* @param HTTPRequest|null $request
96+
* @return bool
97+
* @throws ClientResponseException
98+
* @throws MissingParameterException
99+
* @throws NotFoundExceptionInterface
100+
* @throws ServerResponseException
101+
*/
102+
public function deleteIndex(HTTPRequest $request): bool
103+
{
104+
$deleteResult = false;
105+
if ($this->shouldClear($request) && $this->indexExists()) {
106+
$this->getLogger()->info(sprintf('Clearing index %s', $this->getIndexName()));
107+
$deleteResult = $this->client
108+
->indices()
109+
->delete(['index' => $this->getIndexName()])
110+
->asBool();
111+
}
112+
113+
return $deleteResult;
114+
}
115+
116+
/**
117+
* @param HTTPRequest $request
118+
* @return bool
119+
*/
120+
private function shouldClear(HTTPRequest $request): bool
121+
{
122+
$var = $request->getVar('clear');
123+
124+
return !empty($var);
125+
}
126+
127+
89128
/**
90129
* @return bool
91130
* @throws ClientResponseException

src/Tasks/ElasticConfigureTask.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
1919
use Firesphere\ElasticSearch\Services\ElasticCoreService;
2020
use Firesphere\SearchBackend\Helpers\FieldResolver;
21+
use Firesphere\SearchBackend\Indexes\CoreIndex;
2122
use Firesphere\SearchBackend\Traits\LoggerTrait;
2223
use Psr\Container\NotFoundExceptionInterface;
2324
use SilverStripe\Control\HTTPRequest;
@@ -36,14 +37,23 @@ class ElasticConfigureTask extends BuildTask
3637
{
3738
use LoggerTrait;
3839

39-
/**
40-
* @var bool[]
41-
*/
42-
public $result;
4340
/**
4441
* @var string URLSegment
4542
*/
4643
private static $segment = 'ElasticConfigureTask';
44+
/**
45+
* DBHTML and DBText etc. should never be made sortable
46+
* It doesn't make sense for large text objects
47+
* @var string[]
48+
*/
49+
private static $unSsortables = [
50+
'HTML',
51+
'Text'
52+
];
53+
/**
54+
* @var bool[]
55+
*/
56+
public $result;
4757
/**
4858
* @var string Title
4959
*/
@@ -57,16 +67,6 @@ class ElasticConfigureTask extends BuildTask
5767
*/
5868
protected $service;
5969

60-
/**
61-
* DBHTML and DBText etc. should never be made sortable
62-
* It doesn't make sense for large text objects
63-
* @var string[]
64-
*/
65-
private static $unSsortables = [
66-
'HTML',
67-
'Text'
68-
];
69-
7070
/**
7171
* @throws NotFoundExceptionInterface
7272
*/
@@ -95,13 +95,8 @@ public function run($request)
9595
try {
9696
/** @var ElasticIndex $instance */
9797
$instance = Injector::inst()->get($index, false);
98-
99-
if ($request->getVar('clear') && $instance->indexExists()) {
100-
$this->getLogger()->info(sprintf('Clearing index %s', $instance->getIndexName()));
101-
$deleteResult = $this->service->getClient()->indices()->delete(['index' => $instance->getIndexName()]);
102-
$result[] = $deleteResult->asBool();
103-
}
104-
98+
// If delete in advance, do so
99+
$instance->deleteIndex($request);
105100
$configResult = $this->configureIndex($instance);
106101
$result[] = $configResult->asBool();
107102
} catch (Exception $error) {
@@ -123,26 +118,26 @@ public function run($request)
123118
}
124119

125120
/**
126-
* Update/create a store
127-
* @param ElasticIndex $instance
121+
* Update/create a single index.
122+
* @param ElasticIndex $index
128123
* @return Elasticsearch
129124
* @throws ClientResponseException
130125
* @throws MissingParameterException
131-
* @throws ServerResponseException
132126
* @throws NotFoundExceptionInterface
127+
* @throws ServerResponseException
133128
*/
134-
protected function configureIndex($instance): Elasticsearch
129+
public function configureIndex(CoreIndex $index): Elasticsearch
135130
{
136-
$indexName = $instance->getIndexName();
131+
$indexName = $index->getIndexName();
137132

138-
$instanceConfig = $this->createConfigForIndex($instance);
133+
$instanceConfig = $this->createConfigForIndex($index);
139134

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

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

145-
$method = $this->getMethod($instance);
140+
$method = $this->getMethod($index);
146141
$msg = "%s index %s";
147142
$msgType = 'Updating';
148143
if ($method === 'create') {

src/Tasks/ElasticIndexTask.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
namespace Firesphere\ElasticSearch\Tasks;
1111

12+
use Elastic\Elasticsearch\Exception\ClientResponseException;
1213
use Elastic\Elasticsearch\Exception\HttpClientException;
14+
use Elastic\Elasticsearch\Exception\MissingParameterException;
15+
use Elastic\Elasticsearch\Exception\ServerResponseException;
1316
use Exception;
1417
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
1518
use Firesphere\ElasticSearch\Services\ElasticCoreService;
@@ -103,10 +106,13 @@ public function __construct()
103106

104107
/**
105108
* @param HTTPRequest $request
106-
* @return int|void
109+
* @return bool|int
110+
* @throws HttpClientException
107111
* @throws HttpException
108112
* @throws NotFoundExceptionInterface
109-
* @throws HttpClientException
113+
* @throws ClientResponseException
114+
* @throws MissingParameterException
115+
* @throws ServerResponseException
110116
*/
111117
public function run($request)
112118
{
@@ -124,6 +130,10 @@ public function run($request)
124130
if (!count($classes)) {
125131
continue;
126132
}
133+
// If clearing, also configure
134+
if ($index->deleteIndex($request)) {
135+
(new ElasticConfigureTask())->configureIndex($index);
136+
}
127137

128138
// Get the groups
129139
$groups = $this->indexClassForIndex($classes, $isGroup, $group);

tests/unit/Indexes/ElasticIndexTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\src\SearchIndex;
66
use Elastic\Elasticsearch\Client;
77
use Firesphere\ElasticSearch\Indexes\ElasticIndex;
8+
use Firesphere\ElasticSearch\Tasks\ElasticIndexTask;
9+
use SilverStripe\Control\HTTPRequest;
810
use SilverStripe\Core\Config\Config;
911
use SilverStripe\Dev\SapphireTest;
1012

@@ -119,4 +121,17 @@ public function testIndexExists()
119121
{
120122
$this->assertNotNull($this->index->indexExists());
121123
}
124+
125+
public function testNoClearIndex()
126+
{
127+
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
128+
$isCleared = $this->index->deleteIndex($request);
129+
$this->assertFalse($isCleared);
130+
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
131+
$isCleared = $this->index->deleteIndex($request);
132+
$this->assertTrue($isCleared);
133+
134+
// Ensure everything is back in place.
135+
(new ElasticIndexTask())->run($request);
136+
}
122137
}

tests/unit/Tasks/ElasticConfigureTaskTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Firesphere\ElasticSearch\Tests\unit\Tasks;
44

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

2930
}
31+
32+
public function testConfigureIndex()
33+
{
34+
$index = new SearchIndex();
35+
$task = new ElasticConfigureTask();
36+
$result = $task->configureIndex($index);
37+
38+
$this->assertTrue($result->asBool());
39+
}
3040
}

tests/unit/Tasks/ElasticIndexTaskTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function testRun()
3535
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask');
3636
$result = $task->run($request);
3737

38+
$this->assertGreaterThan(0, $result);
39+
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
40+
$request = new HTTPRequest('GET', 'dev/tasks/ElasticIndexTask', ['clear' => true]);
41+
$result = $task->run($request);
42+
3843
$this->assertGreaterThan(0, $result);
3944
$this->assertinstanceOf(ElasticIndex::class, $task->getIndex());
4045
}

0 commit comments

Comments
 (0)