Skip to content

Commit 2a0d420

Browse files
committed
#105514 Метод получения информации по индексам
1 parent 18377a1 commit 2a0d420

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/ElasticClient.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Elastic\Elasticsearch\ClientBuilder;
77
use Ensi\LaravelElasticQuery\Debug\QueryLog;
88
use Ensi\LaravelElasticQuery\Debug\QueryLogRecord;
9+
use Illuminate\Support\Arr;
910
use Illuminate\Support\Collection;
1011

1112
class ElasticClient
@@ -73,15 +74,32 @@ public function documentDelete(string $index, int|string $id): array
7374

7475
public function catIndices(string $indexName, ?array $getFields = null): array
7576
{
76-
$params = ['index' => "$indexName*"];
77-
if ($getFields) {
78-
$params['h'] = $getFields;
77+
$response = $this->client
78+
->indices()
79+
->stats(['index' => "$indexName*"])
80+
->asArray();
81+
82+
$results = [];
83+
foreach ($response['indices'] as $indexName => $stat) {
84+
$item = [
85+
'index' => $indexName,
86+
'health' => $stat['health'],
87+
'status' => $stat['status'],
88+
'uuid' => $stat['uuid'],
89+
'pri' => Arr::get($stat, 'primaries.shard_stats.total_count'),
90+
'rep' => Arr::get($stat, 'total.shard_stats.total_count'),
91+
'docs.count' => Arr::get($stat, 'total.docs.count'),
92+
'docs.deleted' => Arr::get($stat, 'total.docs.deleted'),
93+
'store.size' => Arr::get($stat, 'total.store.size_in_bytes'),
94+
'pri.store.size' => Arr::get($stat, 'primaries.store.size_in_bytes'),
95+
];
96+
97+
$results[] = !$getFields
98+
? $item
99+
: Arr::only($item, $getFields);
79100
}
80101

81-
return $this->client
82-
->cat()
83-
->indices($params)
84-
->asArray();
102+
return $results;
85103
}
86104

87105
public function indicesDelete(string $indexName): array

tests/Functional/ClientTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Ensi\LaravelElasticQuery\Tests\Functional;
44

55
use Ensi\LaravelElasticQuery\ElasticClient;
6+
use Ensi\LaravelElasticQuery\Tests\AssertsArray;
67
use Ensi\LaravelElasticQuery\Tests\Models\ProductsIndex;
78
use Ensi\LaravelElasticQuery\Tests\Seeds\ProductIndexSeeder;
89

910
class ClientTest extends ElasticTestCase
1011
{
12+
use AssertsArray;
13+
1114
protected function setUp(): void
1215
{
1316
parent::setUp();
@@ -17,8 +20,29 @@ protected function setUp(): void
1720

1821
public function testCatIndices(): void
1922
{
20-
$results = $this->newClient()->catIndices(ProductsIndex::fullName());
23+
$response = $this->newClient()->catIndices(ProductsIndex::fullName());
24+
25+
$this->assertGreaterThanOrEqual(1, count($response));
26+
$this->assertArrayStructure([[
27+
'index',
28+
'status',
29+
'health',
30+
'uuid',
31+
'pri',
32+
'rep',
33+
'docs.count',
34+
'docs.deleted',
35+
'store.size',
36+
'pri.store.size',
37+
]], $response);
38+
}
39+
40+
public function testCatIndicesOnlySpecifiedFields(): void
41+
{
42+
$response = $this->newClient()->catIndices(ProductsIndex::fullName(), ['index', 'status']);
2143

44+
$this->assertArrayStructure([['index', 'status']], $response);
45+
$this->assertArrayNotHasKey('health', $response[0]);
2246
}
2347

2448
private function newClient(): ElasticClient

0 commit comments

Comments
 (0)