Skip to content

Commit 5834970

Browse files
authored
Fix document counts in index stats (#155)
1 parent 44a5880 commit 5834970

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

config/services/search-index-adapter/open-search.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ services:
3232

3333
Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\IndexStatsServiceInterface:
3434
class: Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\IndexStatsService
35+
arguments:
36+
$openSearchClient: '@generic-data-index.opensearch-client'
3537

3638
Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\IndexMappingServiceInterface:
3739
class: Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch\IndexMappingService

qodana.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ exclude:
4242
- name: PhpMethodNamingConventionInspection
4343
paths:
4444
- src/Migrations
45+
- name: PhpDqlBuilderUnknownModelInspection
46+
paths:
47+
- src/Repository/IndexQueueRepository.php
48+
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/AssetTypeAdapter.php
49+
- src/Service/SearchIndex/IndexService/ElementTypeAdapter/DataObjectTypeAdapter.php
4550
include:
4651
- name: PhpTaintFunctionInspection
4752
- name: PhpVulnerablePathsInspection

src/SearchIndexAdapter/OpenSearch/IndexStatsService.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\OpenSearch;
1818

1919
use Exception;
20+
use OpenSearch\Client;
2021
use Pimcore\Bundle\GenericDataIndexBundle\Model\Stats\IndexStats;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\Stats\IndexStatsIndex;
2223
use Pimcore\Bundle\GenericDataIndexBundle\Repository\IndexQueueRepository;
@@ -30,9 +31,10 @@ final class IndexStatsService implements IndexStatsServiceInterface
3031
use LoggerAwareTrait;
3132

3233
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,
3638
) {
3739
}
3840

@@ -43,12 +45,31 @@ public function getStats(): IndexStats
4345
$this->searchIndexConfigService->getIndexPrefix() . '*'
4446
);
4547

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+
4666
$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);
4869
$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)
5273
);
5374
}
5475

0 commit comments

Comments
 (0)