17
17
namespace Pimcore \Bundle \GenericDataIndexBundle \SearchIndexAdapter \OpenSearch ;
18
18
19
19
use Exception ;
20
+ use OpenSearch \Client ;
20
21
use Pimcore \Bundle \GenericDataIndexBundle \Model \Stats \IndexStats ;
21
22
use Pimcore \Bundle \GenericDataIndexBundle \Model \Stats \IndexStatsIndex ;
22
23
use Pimcore \Bundle \GenericDataIndexBundle \Repository \IndexQueueRepository ;
@@ -30,9 +31,10 @@ final class IndexStatsService implements IndexStatsServiceInterface
30
31
use LoggerAwareTrait;
31
32
32
33
public function __construct (
33
- protected readonly SearchIndexConfigServiceInterface $ searchIndexConfigService ,
34
- protected readonly IndexQueueRepository $ indexQueueRepository ,
35
- protected readonly SearchIndexServiceInterface $ openSearchService ,
34
+ private readonly SearchIndexConfigServiceInterface $ searchIndexConfigService ,
35
+ private readonly IndexQueueRepository $ indexQueueRepository ,
36
+ private readonly SearchIndexServiceInterface $ openSearchService ,
37
+ private readonly Client $ openSearchClient ,
36
38
) {
37
39
}
38
40
@@ -43,12 +45,31 @@ public function getStats(): IndexStats
43
45
$ this ->searchIndexConfigService ->getIndexPrefix () . '* '
44
46
);
45
47
48
+ $ aggregationResult = $ this ->openSearchClient ->search ([
49
+ 'index ' => $ this ->searchIndexConfigService ->getIndexPrefix () . '* ' ,
50
+ 'body ' => [
51
+ 'size ' => 0 ,
52
+ 'aggs ' => [
53
+ 'indices ' => [
54
+ 'terms ' => [
55
+ 'field ' => '_index ' ,
56
+ 'size ' => 10000 ,
57
+ 'order ' => [
58
+ '_key ' => 'asc ' ,
59
+ ],
60
+ ],
61
+ ],
62
+ ],
63
+ ],
64
+ ]);
65
+
46
66
$ indices = [];
47
- foreach ($ allStats ['indices ' ] as $ indexName => $ index ) {
67
+ foreach ($ aggregationResult ['aggregations ' ]['indices ' ]['buckets ' ] as $ bucket ) {
68
+ $ sizeInBytes = (int )($ allStats ['indices ' ][$ bucket ['key ' ]]['total ' ]['store ' ]['size_in_bytes ' ] ?? 0 );
48
69
$ indices [] = new IndexStatsIndex (
49
- indexName: $ indexName ,
50
- itemsCount: $ index [ ' total ' ][ ' docs ' ][ ' count ' ],
51
- sizeInKb: round ((( int ) $ index [ ' total ' ][ ' store ' ][ ' size_in_bytes ' ] / 1024 ), 2 )
70
+ indexName: $ bucket [ ' key ' ] ,
71
+ itemsCount: $ bucket [ ' doc_count ' ],
72
+ sizeInKb: round (($ sizeInBytes / 1024 ), 2 )
52
73
);
53
74
}
54
75
0 commit comments