Skip to content

Commit fb63ee4

Browse files
authored
Merge pull request #30 from ensi-platform/task-105514-v8
#105514 Получение информации по индексам
2 parents 3e1c451 + 2a0d420 commit fb63ee4

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Ensi\LaravelElasticQuery\Tests\Functional;
4+
5+
use Ensi\LaravelElasticQuery\ElasticClient;
6+
use Ensi\LaravelElasticQuery\Tests\AssertsArray;
7+
use Ensi\LaravelElasticQuery\Tests\Models\ProductsIndex;
8+
use Ensi\LaravelElasticQuery\Tests\Seeds\ProductIndexSeeder;
9+
10+
class ClientTest extends ElasticTestCase
11+
{
12+
use AssertsArray;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
ProductIndexSeeder::run();
19+
}
20+
21+
public function testCatIndices(): void
22+
{
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']);
43+
44+
$this->assertArrayStructure([['index', 'status']], $response);
45+
$this->assertArrayNotHasKey('health', $response[0]);
46+
}
47+
48+
private function newClient(): ElasticClient
49+
{
50+
return resolve(ElasticClient::class);
51+
}
52+
}

tests/Models/ProductsIndex.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ class ProductsIndex extends ElasticIndex
99
protected string $name = 'test_products';
1010

1111
protected string $tiebreaker = 'product_id';
12+
13+
public static function fullName(): string
14+
{
15+
return (new static())->indexName();
16+
}
1217
}

0 commit comments

Comments
 (0)