Skip to content

Commit 01e36f7

Browse files
committed
Command helpers - getIndexes() compatibility
1 parent 8d36d01 commit 01e36f7

9 files changed

Lines changed: 656 additions & 3 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"illuminate/events": "^10.0|^11|^12",
2626
"illuminate/support": "^10.0|^11|^12",
2727
"elasticsearch/elasticsearch": "^8.17",
28-
"spatie/ignition": "^1.15"
28+
"spatie/ignition": "^1.15",
29+
"pdphilip/omniterm": "^2"
2930
},
3031
"require-dev": {
3132
"orchestra/testbench": "^10.1||^9.0.0||^8.22.0",

composer.lock

Lines changed: 178 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Commands/IndicesCommand.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PDPhilip\Elasticsearch\Commands;
6+
7+
use Exception;
8+
use Illuminate\Console\Command;
9+
use Illuminate\Support\Facades\DB;
10+
use OmniTerm\HasOmniTerm;
11+
use PDPhilip\Elasticsearch\Connection;
12+
13+
class IndicesCommand extends Command
14+
{
15+
use HasOmniTerm;
16+
17+
protected $signature = 'elastic:indices {--all : Show all indices, not just prefixed} {--connection=elasticsearch}';
18+
19+
protected $description = 'List all Elasticsearch indices';
20+
21+
public function handle(): int
22+
{
23+
$connectionName = $this->option('connection');
24+
$showAll = $this->option('all');
25+
26+
$this->newLine();
27+
$this->omni->roundedBox('Elasticsearch Indices', 'text-cyan-500', 'text-cyan-500');
28+
$this->newLine();
29+
30+
try {
31+
/** @var Connection $connection */
32+
$connection = DB::connection($connectionName);
33+
$schema = $connection->getSchemaBuilder();
34+
35+
if ($showAll) {
36+
$indices = $connection->getPostProcessor()->processTables(
37+
$connection->catIndices(['index' => '*', 'format' => 'json'])
38+
);
39+
} else {
40+
$indices = $schema->getTables();
41+
}
42+
} catch (Exception $e) {
43+
$this->omni->statusError('ERROR', $e->getMessage());
44+
$this->newLine();
45+
46+
return self::FAILURE;
47+
}
48+
49+
$prefix = $connection->getIndexPrefix();
50+
51+
$this->omni->tableRow('Connection', $connectionName);
52+
$this->omni->tableRow('Index Prefix', $prefix ?: '(none)');
53+
if ($showAll) {
54+
$this->omni->tableRow('Filter', 'All indices');
55+
}
56+
$this->newLine();
57+
58+
if (empty($indices)) {
59+
$this->omni->warning('No indices found');
60+
$this->newLine();
61+
62+
return self::SUCCESS;
63+
}
64+
65+
$this->omni->tableHeader('Index', 'Health', 'Docs / Size');
66+
67+
$totalDocs = 0;
68+
69+
foreach ($indices as $index) {
70+
$name = $index['name'];
71+
$health = $index['health'] ?? 'unknown';
72+
$docsCount = (int) ($index['docs_count'] ?? 0);
73+
$storeSize = $index['store_size'] ?? '0b';
74+
75+
$totalDocs += $docsCount;
76+
77+
$displayName = $name;
78+
if (! $showAll && $prefix && str_starts_with($name, $prefix)) {
79+
$displayName = substr($name, strlen($prefix));
80+
}
81+
82+
$details = number_format($docsCount).' docs / '.$storeSize;
83+
84+
match ($health) {
85+
'green' => $this->omni->tableRowSuccess($displayName, $details),
86+
'yellow' => $this->omni->tableRowWarning($displayName, $details),
87+
'red' => $this->omni->tableRowError($displayName, $details),
88+
default => $this->omni->tableRow($displayName, $health, $details),
89+
};
90+
}
91+
92+
$this->newLine();
93+
$this->omni->info(count($indices).' indices, '.number_format($totalDocs).' total documents');
94+
$this->newLine();
95+
96+
return self::SUCCESS;
97+
}
98+
}

0 commit comments

Comments
 (0)