Skip to content

Commit 041b6f3

Browse files
committed
Auto create index
1 parent 9c49d5a commit 041b6f3

5 files changed

Lines changed: 46 additions & 12 deletions

File tree

src/Commands/IndicesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function handle(): int
2424
$showAll = $this->option('all');
2525

2626
$this->newLine();
27-
$this->omni->roundedBox('Elasticsearch Indices', 'text-cyan-500', 'text-cyan-500');
27+
$this->omni->titleBar('Elasticsearch Indices', 'teal');
2828
$this->newLine();
2929

3030
try {

src/Commands/ShowCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function handle(): int
2525
$connectionName = $this->option('connection');
2626

2727
$this->newLine();
28-
$this->omni->roundedBox('Index: '.$indexName, 'text-cyan-500', 'text-cyan-500');
28+
$this->omni->titleBar('Index: '.$indexName, 'sky');
2929
$this->newLine();
3030

3131
try {

src/Commands/StatusCommand.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function handle(): int
2323
$connectionName = $this->option('connection');
2424

2525
$this->newLine();
26-
$this->omni->roundedBox('Elasticsearch Status', 'text-cyan-500', 'text-cyan-500');
26+
$this->omni->titleBar('Elasticsearch Status', 'cyan');
2727
$this->newLine();
2828

2929
try {
@@ -58,8 +58,7 @@ public function handle(): int
5858
$this->omni->tableRow('Index Prefix', $prefix);
5959
$this->newLine();
6060

61-
$this->omni->newLoader('dots');
62-
$result = $this->omni->runTask('Connecting to Elasticsearch', function () use ($connection) {
61+
$result = $this->omni->task('Connecting to Elasticsearch', function () use ($connection) {
6362
try {
6463
$info = $connection->getClientInfo();
6564

@@ -84,11 +83,11 @@ public function handle(): int
8483
}
8584
});
8685

87-
if (empty($result['data'])) {
86+
if (! $result || $result->isError()) {
8887
$this->newLine();
8988
$this->omni->statusError(
9089
'CONNECTION FAILED',
91-
$result['details'] ?? 'Unable to connect',
90+
$result ? $result->details : 'Unable to connect',
9291
[
9392
'Check that Elasticsearch is running',
9493
'Verify hosts, credentials, and auth_type in config/database.php',
@@ -99,8 +98,8 @@ public function handle(): int
9998
return self::FAILURE;
10099
}
101100

102-
$info = $result['data']['info'];
103-
$license = $result['data']['license'];
101+
$info = $result->data['info'];
102+
$license = $result->data['license'];
104103

105104
$this->newLine();
106105
$this->omni->tableHeader('Cluster', 'Value');

src/Connection.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private function sanitizeConfig(): void
128128
'meta_header' => null,
129129
'default_limit' => null,
130130
'allow_id_sort' => false,
131+
'auto_create_index' => true,
131132
],
132133
],
133134
$this->config
@@ -711,12 +712,44 @@ protected function addClientParams(array $params): array
711712
protected function runQueryCallback($query, $bindings, Closure $callback)
712713
{
713714
try {
714-
$result = $callback($query, $bindings);
715+
return $callback($query, $bindings);
716+
} catch (ClientResponseException $e) {
717+
if ($this->shouldAutoCreateIndex($e, $query)) {
718+
return $callback($query, $bindings);
719+
}
720+
throw new QueryException($e, $query);
715721
} catch (Exception $e) {
716722
throw new QueryException($e, $query);
717723
}
724+
}
718725

719-
return $result;
726+
private function shouldAutoCreateIndex(ClientResponseException $e, mixed $query): bool
727+
{
728+
if (! ($this->config['options']['auto_create_index'] ?? true)) {
729+
return false;
730+
}
731+
732+
if ($e->getCode() !== 404) {
733+
return false;
734+
}
735+
736+
$body = json_decode((string) $e->getResponse()->getBody(), true);
737+
if (($body['error']['type'] ?? '') !== 'index_not_found_exception') {
738+
return false;
739+
}
740+
741+
$index = is_array($query) ? ($query['index'] ?? null) : null;
742+
if (! $index) {
743+
return false;
744+
}
745+
746+
try {
747+
$this->connection->createIndex($index, []);
748+
749+
return true;
750+
} catch (Exception) {
751+
return false;
752+
}
720753
}
721754

722755
/**

tests/CommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
declare(strict_types=1);
44

5+
use PDPhilip\Elasticsearch\Tests\Models\User;
6+
57
it('runs elastic:status successfully', function () {
68
$this->artisan('elastic:status')
79
->assertSuccessful();
@@ -19,7 +21,7 @@
1921

2022
it('runs elastic:show on an existing index', function () {
2123
// Ensure the users index exists
22-
\PDPhilip\Elasticsearch\Tests\Models\User::executeSchema();
24+
User::executeSchema();
2325

2426
$this->artisan('elastic:show', ['index' => 'users'])
2527
->assertSuccessful();

0 commit comments

Comments
 (0)